为什么一直跳主键报错 299999是最大值吗?主键是没数据的
package com.imooc.jdbc.sample; import com.imooc.jdbc.hrapp.common.DbUtils; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.Date; public class BatchSample { private static void tc1(){ Connection conn = null; PreparedStatement pstmt = null; int num=0; try { long startTime = new Date().getTime(); conn = DbUtils.getConnection(); conn.setAutoCommit(false); String sql = "insert into employee(eno,ename,salary,dname) values(?,?,?,?)"; for (int i = 100000;i < 200000;i ++){ pstmt = conn.prepareStatement(sql); pstmt.setInt(1,i); pstmt.setString(2,"员工"+i); pstmt.setFloat(3,4000f); pstmt.setString(4,"市场部"); num = pstmt.executeUpdate()+num; } if (num != 0){ System.out.println("成功"+num); }else { System.out.println("失败"+num); } conn.commit(); long endTime = new Date().getTime(); System.out.println(endTime-startTime); } catch (Exception e) { e.printStackTrace(); try { if (conn != null && !conn.isClosed()){ conn.rollback(); } } catch (SQLException ex) { e.printStackTrace(); } }finally { DbUtils.closeConnection(null,pstmt,conn); } } private static void tc2(){ Connection conn = null; PreparedStatement pstmt = null; int num=0; try { long startTime = new Date().getTime(); conn = DbUtils.getConnection(); conn.setAutoCommit(false); String sql = "insert into employee(eno,ename,salary,dname) values(?,?,?,?)"; for (int i = 200000;i < 300000;i ++){ pstmt = conn.prepareStatement(sql); pstmt.setInt(1,i); pstmt.setString(2,"员工"+i); pstmt.setFloat(3,4000f); pstmt.setString(4,"市场部"); num = pstmt.executeUpdate()+num; pstmt.addBatch(); } if (num != 0){ System.out.println("成功"+num); }else { System.out.println("失败"+num); } pstmt.executeBatch(); conn.commit(); long endTime = new Date().getTime(); System.out.println(endTime-startTime); } catch (Exception e) { e.printStackTrace(); try { if (conn != null && !conn.isClosed()){ conn.rollback(); } } catch (SQLException ex) { e.printStackTrace(); } }finally { DbUtils.closeConnection(null,pstmt,conn); } } public static void main(String[] args) { tc1(); tc2(); } }
4
收起
正在回答 回答被采纳积分+1
Java工程师 2024版
- 参与学习 2020 人
- 提交作业 1249 份
- 解答问题 1202 个
2024重磅革新,超百小时内容豪华升级,加速提升高级技能与高薪就业竞争力 课程紧贴企业最新人才需求,历经7年持续迭代,帮助万名学子入行转行 从零起点到高阶实战,学习路径稳健顺滑,成就从小白到工程师高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星