Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to Fill ? For Prepared Statement in below case

 
balamurugan velliambalam
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic