Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Insert into PrepareStatement Using Field Name

 
vinod balaji
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
Is it possible to Insert the value in DB, using the field name. I couldnt able to find such Method in PreparedStatement..
Something like

String Query = "Insert into table(GROUP_NAME,SOURCE) values (?,?)";

PreparedStatement ps =conn.prepareStatement(Query,Statement.RETURN_GENERATED_KEYS);
// like the below i need
ps.setValue("GROUP",1);
ps.setValue("SOURCE",2)


ps.executeUpdate();
 
Swastik Dey
Rancher
Posts: 1639
6
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you need that? You have already mentioned the field names in the insert statement.
 
vinod balaji
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes ofcourse,
but if i able to specify the field name, i can give the statements in any order..
 
Swastik Dey
Rancher
Posts: 1639
6
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont find anything wrong with this

String Query = "Insert into table(SOURCE,GROUP_NAME,) values (?,?)";

PreparedStatement ps =conn.prepareStatement(Query,Statement.RETURN_GENERATED_KEYS);
// like the below i need
ps.setValue(1,sorceValue);
ps.setValue(2,grpValue)


ps.executeUpdate();
 
vinod balaji
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We can insert using the Index.. No problem with that..
Can we insert using the Field name using any PreparedStatement API ?
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you can't. Why would you?
 
Kaialsh Vashisth
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No , you need to suppy the index numbers ie integer values for correnponding columns.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic