jQuery in Action, 2nd edition*
The moose likes JDBC and the fly likes How to Fill ? For Prepared Statement in below case Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to Fill ? For Prepared Statement in below case" Watch "How to Fill ? For Prepared Statement in below case" New topic
Author

How to Fill ? For Prepared Statement in below case

balamurugan velliambalam
Greenhorn

Joined: Jul 13, 2010
Posts: 20
Java Insert Statement:

insert into temp2(NO,NAME,DATE) values(?,?,?)

I have recieve csv in any one of format below even it may change in formats.

How we insert this statement using PreparedStatement in java.

It may like this

NO,NAME,DATE
1,BALA,24-02-1987
2,BALA,24-02-1987

(OR)

NO,DATE,NAME
1,24-02-1987,BALA
2,24-02-1987,BALA

(OR)

DATE,NAME,NO
24-02-1987,BALA,1
24-02-1983,BALA,2
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3606
    
  60

It's up to you to read the contents of the CSV file and figure out the order of its columns. You should end up with a list of objects/records which will contain the NO, NAME and DATE values from individual rows. You'll then use setString or setDate or whatever appropriate method with index corresponding to the bind variable (the question mark) of your SQL statement and the corresponding value you obtain from the record which keeps the data from the CSV file. If your case, the NO field comes first (with index 1), NAME second (index 2) and DATE third (index 3).

It would be also possible to create the SQL statement with fields ordered to match the order in CSV file. You'd need o create a new statement to process another file, of course, since it could have different order of fields. If you had lots of small files with lots of differently ordered columns, the first approach would be better.
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

Martin Vajsar wrote:It's up to you to read the contents of the CSV file and figure out the order of its columns. You should end up with a list of objects/records which will contain the NO, NAME and DATE values from individual rows. You'll then use setString or setDate or whatever appropriate method with index corresponding to the bind variable (the question mark) of your SQL statement and the corresponding value you obtain from the record which keeps the data from the CSV file. If your case, the NO field comes first (with index 1), NAME second (index 2) and DATE third (index 3).

It would be also possible to create the SQL statement with fields ordered to match the order in CSV file. You'd need o create a new statement to process another file, of course, since it could have different order of fields. If you had lots of small files with lots of differently ordered columns, the first approach would be better.


can i just highlight the index does start at 1.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to Fill ? For Prepared Statement in below case
 
Similar Threads
compatibility of date format
Mysql Java and Date Formats
How can we store serialized object
MSSQL - Date
Cannot perform SQL UPDATE null