Meaningless Drivel is fun!*
The moose likes JDBC and the fly likes how to insert 2 values togther in db? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "how to insert 2 values togther in db?" Watch "how to insert 2 values togther in db?" New topic
Author

how to insert 2 values togther in db?

Liz Brown
Ranch Hand

Joined: Oct 22, 2002
Posts: 112
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
Marshal

Joined: May 26, 2003
Posts: 30789
    
157

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:


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Dorj Galaa
Ranch Hand

Joined: May 29, 2001
Posts: 113
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();


Senior software engineer
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to insert 2 values togther in db?