• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to insert 2 values togther in db?  RSS feed

 
Liz Brown
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • 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();
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37395
531
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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:
 
Dorj Galaa
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • 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();
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!