Solved

Loading contents of a *.CSV file into a database

Posted on 2004-10-30
160 Views
Last Modified: 2010-04-01
I have a *.csv file whose contents I want to add into a database. How can I possibly do this using Javascript or JSP?

Monk
0
Question by:VbMonk
    8 Comments
     
    LVL 13

    Expert Comment

    by:petmagdy
    From JSP these are summary steps:

    1- using java.io, read the CSV line by Line, each line into a String
    2- if it is comma delimited, for each line use:

    String[] fields = String.split("[,]');

    u will get what represents the Single Record fields in the fields String Array

    3- For each line use JDBC to insert into dataase, by a SQL statment like this:

    insert into mytable values( {ur fields} );

    0
     
    LVL 9

    Accepted Solution

    by:
    ALL Points to petmagdy, but I feel the urge to elaborate a bit:

    >"1- using java.io, read the CSV line by Line, each line into a String"
    See here: http://www.javaalmanac.com/egs/java.io/ReadLinesFromFile.html
    You simply append the text to a String.

    >"2- if it is comma delimited"
    Try using the StringTokenizer class (from the java.util) package:

            StringTokenizer st = new StringTokenizer ( stringToTokenize, "," );

            String[] fields = new String [ st.countTokens()];
            for ( int i=0; i<st.countTokens(); i++ )
                fields[i] = st.nextToken();

    >"For each line use JDBC to insert into dataase"

    This may help:
    http://www.devdaily.com/java/edu/pj/jdbc/jdbc0002/


    Good luck with it ;-)
    [r.D]
    0
     
    LVL 13

    Expert Comment

    by:petmagdy
    DrWarezz, thanks your good value add and elaboration, deserves all the points thanks
    0
     
    LVL 9

    Expert Comment

    by:DrWarezz
    :-) No, I insist.  I honestly would feel like a 'point-theif' if I were to get the points, as you've answered the Q, I just elaborated a bit, because I felt that the author clearly wasn't TOO confident on how to do this. :-)

    So, author; no matter what petmagdy says, give him the points  :-)

    [r.D]
    0
     
    LVL 13

    Expert Comment

    by:petmagdy
    no "point-theif" u gived the answer more intension than me and provided value add, author best solution to split

    but wait a minute where is the author? seems not interested any more ;-)
    0
     
    LVL 13

    Expert Comment

    by:petmagdy
    more attension i mean, miss tepo, hhhhhhhhhh LOOOLLLLLLLLLLL
    0
     
    LVL 9

    Expert Comment

    by:DrWarezz
    lol -- it makes sense either way ;-)
    0
     
    LVL 9

    Expert Comment

    by:DrWarezz
    Okay.. thanks, VbMonk  :-)

    [r.D]
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Suggested Solutions

    Title # Comments Views Activity
    netstat -a in AIX unix 8 16
    Set up Wildfly 10 8 253
    if statement not resolving in my code 5 37
    spring JDBC Template example error 26 80
    New Windows 7 Installations take days for Windows-Updates to show up and install. This can easily be fixed. I have finally decided to write an article because this seems to get asked several times a day lately. This Article and the Links apply to…
    Data center, now-a-days, is referred as the home of all the advanced technologies. In-fact, most of the businesses are now establishing their entire organizational structure around the IT capabilities.
    Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    933 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

    13 Experts available now in Live!

    Get 1:1 Help Now