20070119

20070110

利用 PreparedStatement 批次查詢

平常StatementPreparedStatement的executeUpdate()的方法只能執行一個SQL敘述, 如果有很多SQL要執行的話,就得用addBatch()executeBatch()

ex1.

String strqry="insert into test (col1,col2) values (?,?)";
PreparedStatement pstmt = con.prepareStatement(strqry);
pstmt.setString(1,"test1");
pstmt.setString(2, "123");
pstmt.addBatch();
pstmt.setString(1,"test2");
pstmt.setString(2, "456");
pstmt.addBatch();
pstmt.executeBatch();

ex2:

Statement stmt=con.createStatement();
stmt.addBatch("insert into test (col1,col2) values ('test1','123')");
stmt.addBatch("update test set col2='789' where col1='test4'");
stmt.executeBatch();