Java JDBC(2)

JDBC批处理
对于大量的批处理,应使用Statement,因为PreparedStatement的预编译空间有限,当数据量特别大的时候,会发生异常
Class.forName("com.mysql.jdbc.Driver");
Connection connection=GetConnection.getConnection();
connection.setAutoCommit(false);
Statement statement=connection.createStatement();
for(int i=0;i<1000;i++){
    statement.addBatch("INSERT INTO test(name,password) VALUES ('zhang"+i+"',"+"'wang"+i+"')");
}
statement.executeBatch();
connection.commit();


事务:
一组要么同时执行成功,要么同时执行失败的SQL语句,是数据库操作中的一个执行单元


事务开始于:
连接到数据库上,并执行一条DML语句
前一个事务结束后,又输入了另外一条DML语句


事务结束后:
执行COMMIT或ROLLBACK语句
执行一条DDL语句,例如CREATE TABLE语句;在这种情况下,回自动执行COMMIT语句
执行一条DCL语句,例如GRANT语句;在这种情况下,会自动执行COMMIT语句
断开与数据库的连接
执行一条DML语句,该语句失败了;在这种情况下,会为这个无效的DML语句执行ROLLBACK语句


事务的四大特性:
原子性,一致性,隔离性,持久性


事务隔离级别从低到高:
读取未提交
读取已提交
可重复度
序列化


Class.forName("com.mysql.jdbc.Driver");
Connection connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/JDBC","root","");
connection.setAutoCommit(false);
String sql="INSERT INTO jdbc(name,password) VALUES(?,?)";
PreparedStatement preparedStatement=connection.prepareStatement(sql);
preparedStatement.setObject(1,"wang");
preparedStatement.setObject(2,"he");
preparedStatement.executeUpdate();


try {
    Thread.sleep(1000);
} catch (InterruptedException e) {
    e.printStackTrace();
}
PreparedStatement preparedStatement1=connection.prepareStatement("INSERT INTO jdbc(name,password) VALUES(?,?,?)")
preparedStatement1.setObject(1,"wang");
preparedStatement1.setObject(2,"he");
preparedStatement1.executeUpdate();


connection.commit();


其中第二个SQL语句没有成功执行,则这两个SQL语句都不会执行



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值