| 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....
|
 |
 |
|
|
subject: Prepared statement looping
|
|
|