批量插入getGeneratedKeys()方法使用

mysql 中批量插入時使用getGeneratedKeys()方法可以。代碼如下:
String iMcSql = "insert into MXMCMG(MXMC) values(?)";
try {
Class.forName("com.mysql.jdbc.Driver");
Connection  conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/t1","root","123");
conn.setAutoCommit(false);
PreparedStatement pstmt = conn.prepareStatement(iMcSql,Statement.RETURN_GENERATED_KEYS);
for(int j = 0; j < 10; j++){
for (int i = 0; i < 1; i++) {
pstmt.setObject(i + 1, mxmcmg[i]);
}
pstmt.addBatch();
}
pstmt.executeBatch();
ResultSet rs = pstmt.getGeneratedKeys();
while (rs.next()) {
System.out.println(rs.getInt(1));
}
conn.commit();
pstmt.close();
conn.close();
} catch(Exception ex){
log.error(ex.toString());
}
return false;
       
oracle 中批量插入時使用getGeneratedKeys()方法,不可以。代碼如下:
 
String iMcSql = "insert into MXMCMG(MXID,MXMC) values(HIBERNATE_SEQUENCE.NEXTVAL,?)";
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@ip:1521:dbName","user","pwd");
conn.setAutoCommit(false);
PreparedStatement pstmt = conn.prepareStatement(iMcSql,Statement.RETURN_GENERATED_KEYS);
for(int j = 0; j < 10; j++){
for (int i = 0; i < 1; i++) {
pstmt.setObject(i + 1, mxmcmg[i]);
}
pstmt.addBatch();
}
pstmt.executeBatch(); 
ResultSet rs = pstmt.getGeneratedKeys();
while (rs.next()) {
System.out.println(rs.getInt(1));
}
conn.commit();
pstmt.close();
conn.close();
} catch(Exception ex){
log.error(ex.toString());
}
return false;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章