This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Prepared statement looping

 
Manoj John
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 572
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 572
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 572
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its better to make a seperate prepared statement for each insert statement and then close that after inserting records.
 
Manoj John
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
greate help for me thanks....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic