This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes JDBC and the fly likes Prepared statement looping 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 "Prepared statement looping" Watch "Prepared statement looping" New topic
Author

Prepared statement looping

Manoj John
Ranch Hand

Joined: May 13, 2004
Posts: 36
please help,
I,ve the situation that in my program i have the table with fields father(varchar) and children(varchar).I have to write a prepared statement for inserting values for father having more than one child.
if any examples it'll be a greate help for me.
Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572
what is the problem in doing so? Explain

You can just make preparedstatements or statements for each record to insert and then execute them later at once or seperately.

If you find any problem doing so then post your code where you found the problem.
Manoj John
Ranch Hand

Joined: May 13, 2004
Posts: 36
code----------------------------------------------------------------
query = "INSERT INTO PARENT VALUES(?,?)";

prepStmt = conn.prepareStatement(query);
prepStmt.setString(1,fname);

for(int i=0;i<childr.length;i++)
prepStmt.setString(2,childr[i]);

prepStmt.executeUpdate();
prepStmt.clearParameters();
--------------------------------------------------------------------------
is it right?

[ May 13, 2004: Message edited by: Manoj John ]
[ May 13, 2004: Message edited by: Manoj John ]
Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572
code----------------------------------------------------------------
query = "INSERT INTO PARENT VALUES(?,?)";

prepStmt = conn.prepareStatement(query);
prepStmt.setString(1,fname);

for(int i=0;i<magazines.length;i++)
prepStmt.setString(2,childr[i]);

prepStmt.executeUpdate();
prepStmt.clearParameters();
--------------------------------------------------------------------------
is it right?


As i have understood from the above code is that you want to insert different children Strings against 1 parent String.

So you can do as following

for(int i=0;i<childr.length;i++){
PreparedStatement pstmt = con.prepareStatement(query);
pstmt.setString(1,fname);
pstmt.setString(2,childr[i]);
pstmt.executeUpdate();
}

This loop will insert all the records.
}
Manoj John
Ranch Hand

Joined: May 13, 2004
Posts: 36
one more qestion
can i insert the values to more than one table using same
PreparedStatement object?
Please give a suggestion on that..
Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572
Its better to make a seperate prepared statement for each insert statement and then close that after inserting records.
Manoj John
Ranch Hand

Joined: May 13, 2004
Posts: 36
greate help for me thanks....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Prepared statement looping