• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

how to insert 2 values togther in db?

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,
I have 2 functions which insert values from Arraylists into database in each column. When I execute this way, values are inserted 1 after another in transactions.
I want to do similar to:
eg:
insert into PAT_STUDENT_SURVEY(ANSWER1, ANSWER2) values('5','0');

how do i do with my code?

// Inserting StudentBvalues in db table
for (int i=0; i > StudentBvalues.size(); i++) {
String x= (String)StudentBvalues.get(i);
int Bvalue = Integer.parseInt(x);
sql = "INSERT INTO PAT_STUDENT_SURVEY"+
"(ANSWER1) VALUES "+
"('"+Bvalue+"')" ;
System.out.println("\nSQL in DataAccess==> " + sql);
_stmt1.execute(sql);
}
//_stmt.execute();
_stmt1.close();


// Inserting StudentFvalues in db table
for (int i=0; i < StudentFvalues.size(); i++) {
String xf= (String)StudentFvalues.get(i);
int Fvalue = Integer.parseInt(xf);
sql = "INSERT INTO PAT_STUDENT_SURVEY"+
"(ANSWER2) VALUES "+
"('"+Fvalue+"')" ;
System.out.println("\nSQL in DataAccess==> " + sql);
_stmt2.execute(sql);
}
//_stmt.execute();
_stmt2.close();
 
author & internet detective
Posts: 40913
840
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shivan,
First thing is to make sure the two lists are the same length. If so, you can get both values in the same loop iteration. It's somewhat easier with a prepared statement:

and then set the values:
 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try to use addBatch() and executeBatch()
stmt.setString(1,"3");
stmt.setString(2,"3");
stmt.addBatch();
stmt.setString(1,"5");
stmt.setString(2,"5");
stmt.addBatch();
stmt.executeBatch();
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic