Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Insert into PrepareStatement Using Field Name

 
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();
 
Rancher
Posts: 2265
20
Android Java ME Eclipse IDE Java
  • 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: 2265
20
Android Java ME Eclipse IDE Java
  • 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 ?
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you can't. Why would you?
 
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.

 
Right! We're on it! Let's get to work tiny ad!
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic