• 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: 1721
8
Android Eclipse IDE 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: 1721
8
Android Eclipse IDE 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.

 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!