您的位置:首页 > 编程语言 > Java开发

java JDBC-批处理Batch-事务

2019-10-09 22:23 651 查看
public class Demo5 {
public static void main(String[] args) {
Statement stmt=null;
Connection conn=null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","dyl123");

//设为手动提交
conn.setAutoCommit(false);

stmt=conn.createStatement();
for(int i=0;i<20000;i++)
{
stmt.addBatch("insert into t_user(username,pwd,regTime) values('米"+i+"',555,now())");
}
stmt.executeBatch();
conn.commit(); //提交事务

} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}

}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: