using some values from a txt file and comparing info from DB and updating the DB

Posted on 2005-04-27
Last Modified: 2010-03-31

''  I only have these many points left ''
I want to
    read a property file
    read an input txt file
    connect to the DB and load values into a collection
    compare the value with the txt file and if OK update a table and if not Ok update another table.
 I need to run it on Unix with Oracle DB


I would like to know how to load these property file say p.prt which has the below values
STDFILE =/std/

then read a file say test.txt from an input dir compare it with a std .xls file from the std dir for spaces and types

say test.txt file
John                         9999 22 USA     100.00
Kim                           8888 21  UK      800.00  
say std.xls file is as follows
description size  type
Name        30     A
id               4      A
age             3     A
country        3     A
amount      10     N

'A' for alphanumeric and 'N' for Numeric

and if the entire file pattern is not OK it should write to a log file and if OK for spaces and types then the log file should be updated as the file is OK  then

 read each record from the text.txt file  

connect to the ORACLE DB using the above property file

and read some values into a collection say
'select , m.age , from member m , address a where ='

and then compare the collection DB values with the test.txt record values for id, age and country and if the test.txt has a record which matches then

update  a  table uptblMatch with all the values which are matching from the  test.txt file

and the records which don't match I have to update in another table uptblMismatch in DB.
Question by:Richards
    LVL 86

    Expert Comment

    And your question is ..?
    LVL 16

    Expert Comment

    To read property file for easy way, you can use java.util.Properties class.

      // Create Properties object
      java.util.Properties prop = new java.util.Properties();
      // Load property list from file into the Properties
      prop.load(new FileInputStream("p.prt"));

      // Get properties
      String inputDir = prop.getProperty("INPUTDIR");
      String stdDir = prop.getProperty("STDFILE");

    To read a text file

      String filePath = inputDir + "test.txt";
      BufferedReader br;
        br = new BufferedReader( new FileReader( filePath ) );
        String line;
        while( ( line=br.readLine() ) != null )
          // Process current line
      catch(IOException e)
        try{ br.close(); }catch(Exception ex){}

    To read excel file, you need to use Third Party package because it's not included in standard java package.
    One of them is POI-HSSF project, you can download it for free here

    And here is where you can get tutorial about how to connect to Oracle, and do select, insert, update delete

    Author Comment

    Thanks !
    How do we go around using the record set values stored in say result and then compare it with the entire input file data and if found and OK I need to update a table say  uptblMatch and if the input file record does not match any of the result then I need to update another table say  uptblMismatch

    and read some values into a collection say
    sqlquery = 'select , m.age , from member m , address a where =',

    result = executeSQL(sqlquery);
    LVL 16

    Accepted Solution

    To compare file and db, I would suggest you to read each line of file, then get a field of that line to search in db because seaching in db is better performance than searching in file.

      String filePath = inputDir + "test.txt";

      // Create statement object from database connection
      Statement stmt = conn.createStatement();
      ResultSet rs = null;

      BufferedReader br;
        br = new BufferedReader( new FileReader( filePath ) );
        String line;
        String[] array = null, sql = null;
        while( ( line=br.readLine() ) != null )
                array = line.split("\s"); // [0:name, 1:id, 2:age, 3:country, 4:amount]

                // Use 'id' field to search in db
                sql = "select * from member where id="+array[1];
                rs = stmt.executeQuery(sql);
                boolean found =;

                  // found id in db, update uptblMatch
                  // not found id in db, update uptblMismatch
      catch(Exception e)
        try{ br.close(); }catch(Exception ex){}

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Problem to setup 18 67
    factory design pattern vs abstract factoy design pattern 2 65
    HashMap Vs TreeMap 12 34
    solarwind tftp server 2 20
    After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
    Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now