Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Input file parsing to insert into DB

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Input file parsing to insert into DB

    Hi,

    I have a sample input file as follows:

    ++++++++++ 2009-09-28 15:10:58 ++++++++++++++
    ++++++++ DAILY SCORES +++++++++++++++++
    2009-09-28 15:10:58|AAAA|0
    2009-09-28 15:10:58|BBBBB|0
    2009-09-28 15:10:58|ACCCC|0
    ++++++++ MONTHLY SCORES +++++++++++++++++
    2009-09-28 15:10:58|AAAA|30
    2009-09-28 15:10:58|BBBBB|50
    2009-09-28 15:10:58|ACCCC|60

    I should ignore first line and then look for "DAILY SCORES" or "MONTHLY SCORES"string. This determines which MySql table has to be used.
    Then read the following lines delimited by pipe and insert them into MySql table

    Table schema is as follows:
    ------------------------------------------------------------
    dateandtimestamp | program | score
    ------------------------------------------------------------

    Please suggest the best approach to do this.

    Thanks,
    Mona.


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Input file parsing to insert into DB

    1. Loop through each line in the file
    2. Do some checks to see if it contains DAILY SCORES or MONTHLY SCORES
    3 .Do a split on each line after the above on the pipe |
    4. Create your object or just simply insert into your database

    Fairly simple I guess. Is this just some program you'd like to be run every so often on a certain file/folder then I take it?

    // Json

  3. #3
    Junior Member
    Join Date
    Sep 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Input file parsing to insert into DB

    Thanks for your reply. I am looking for more elaborated answer. Can you please help me with the code? Yes, this program has to be run every one hour and each time there is going to be a new file with updated scores.

  4. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Input file parsing to insert into DB

    I created this in a couple of minutes as a test, it shows you the string reading and parsing but no database connection stuff.

    This code opens a file called c:/test.txt but you can change that to whatever you wish.

    package uk.co.cdl.testing;
     
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
     
    public class ReadAndParseFile {
     
        public enum State {
            PARSING,
            DAILY_SCORES,
            MONTHLY_SCORES;
        }
     
        private static State state = State.PARSING;
     
        public static void main(String... arguments) throws IOException {
            final String pathToFile = "c:/test.txt";
            final BufferedReader bufferedReader = new BufferedReader(new FileReader(new File(pathToFile)));
            String line = null;
     
            while ((line = bufferedReader.readLine()) != null) {
                if (line.contains("DAILY SCORES")) {
                    state = State.DAILY_SCORES;
                } else if (line.contains("MONTHLY SCORES")) {
                    state = State.MONTHLY_SCORES;
                }
     
                addToDataBase(line);
            }
     
            if (bufferedReader != null) {
                bufferedReader.close();
            }
        }
     
        private static void addToDataBase(final String line) {
     
            if (!State.PARSING.equals(state)) {
                final String[] split = line.split("\\|");
     
                if (split.length >= 3) {
                    final String date = split[0];
                    final String program = split[1];
                    final String score = split[2];
     
                    if (State.DAILY_SCORES.equals(state)) {
                        // TODO: Add the scores to the daily scores table
                    } else if (State.MONTHLY_SCORES.equals(state)) {
                        // TODO: Add the scores to the monthly scores table
                    }
     
                    System.out.println("Found line with date [" + date + "] program [" + program + "] score [" + score + "] on state [" + state.toString() + "]");
                }
            }
        }
    }

    My output.

    Found line with date [2009-09-28 15:10:58] program [AAAA] score [0] on state [DAILY_SCORES]
    Found line with date [2009-09-28 15:10:58] program [BBBBB] score [0] on state [DAILY_SCORES]
    Found line with date [2009-09-28 15:10:58] program [ACCCC] score [0] on state [DAILY_SCORES]
    Found line with date [2009-09-28 15:10:58] program [AAAA] score [30] on state [MONTHLY_SCORES]
    Found line with date [2009-09-28 15:10:58] program [BBBBB] score [50] on state [MONTHLY_SCORES]
    Found line with date [2009-09-28 15:10:58] program [ACCCC] score [60] on state [MONTHLY_SCORES]
    Let us know if you need more help.

    // Json

Similar Threads

  1. Insert sort algorithm
    By Alysosh in forum Algorithms & Recursion
    Replies: 1
    Last Post: May 26th, 2009, 09:28 AM
  2. Object creation from a input file and storing in an Array list
    By LabX in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: May 14th, 2009, 03:52 AM
  3. Printing xml to the console from .wmdb without printing junks
    By John in forum File I/O & Other I/O Streams
    Replies: 9
    Last Post: April 24th, 2009, 03:44 AM
  4. [SOLVED] Parsing ID3 tags from mp3
    By John in forum Java Theory & Questions
    Replies: 14
    Last Post: April 16th, 2009, 01:36 PM
  5. Program to mask input
    By sah in forum Java Theory & Questions
    Replies: 1
    Last Post: January 26th, 2009, 06:43 PM