We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

upload the csv file those records should be inserted corresponding fields of the table in the dabatase

chaitu chaitu
on
Medium Priority
283 Views
Last Modified: 2010-03-31
i have a csv file in that i have  id and name columns and the corresponding values below that.my requirement is i will upload that csv file those records should be inserted corresponding fields of the table in the dabatase.how will i do this?
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
This should make it easier:

http://ostermiller.org/utils/CSV.html
CERTIFIED EXPERT
Top Expert 2016

Commented:
Some dbs can load CSV automatically btw

Author

Commented:
through java i have to do this
CERTIFIED EXPERT
Top Expert 2016

Commented:
I'd use the above with a PreparedStatement in batch mode

Author

Commented:
just tell me a psecudo code how to read csv file using CSVParser
CERTIFIED EXPERT
Top Expert 2016

Commented:
You don't need pseudo. There's actual code on the main page:

http://ostermiller.org/utils/CSV.html

Author

Commented:
ur talking abt this;but here how will u pass cvs file??

String[][] values = CSVParser.parse(new StringReader(""));

// Display the parsed data
        for (int i=0; i<values.length; i++){
            for (int j=0; j<values[i].length; j++){
                System.out.println(values[i][j]);
            }
            System.out.println("-----");
        }
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>how will u pass cvs file??

You don't pass the CVS file. You insert individual values in that array into the DB

Author

Commented:
String[][] values = CSVParser.parse(new FileReader(new File("c:/chaitu/emp.csv")));

// Display the parsed data
        for (int i=0; i<values.length; i++){
            for (int j=0; j<values[i].length; j++){
                System.out.println(values[i][j]);
            }
            System.out.println("-----");
        }

in the emp.csv file i have id and name values will be stored.......
CERTIFIED EXPERT
Top Expert 2016
Commented:
>>
          for (int j=0; j<values[i].length; j++){
                System.out.println(values[i][j]);
            }
>>

Replace the above by

ps.setString("id", values[i][0]);
ps.setString("name", values[i][1]);
ps.addBatch();

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
CERTIFIED EXPERT
Top Expert 2016

Commented:
(If 'id' is not a String, obviously make the appropriate adjustments)

Author

Commented:
so what i am doing is correct

String[][] values = CSVParser.parse(new FileReader(new File("c:/chaitu/emp.csv")));
CERTIFIED EXPERT
Top Expert 2016

Commented:
Yes
CERTIFIED EXPERT
Top Expert 2016

Commented:
:-)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.