jdbc PreparedStatement 事务与批处理

PreparedStatement.setXxx(1,xxx);//准备好sql语句*大量
PreparedStatement.addBatch();//将SQL语句添加到批处理
PreparedStatement.executeBatch();//处理大量sql语句
PreparedStatement.clearBatch();//将批量处理过的sql语句清空,等再次添加一组

使用批处理的前提是需要在连接数据库的url中配置rewriteBatchedStatements=true
批处理的底层是创建了一个ArrayList数组,初次创建长度为10,满后按照1.5倍扩容

user=root
password=cxl
url=jdbc:mysql://localhost:3306/testdb?rewriteBatchedStatements=true
#如果使用批量处理,则必须在url中添加 ?rewriteBatchedStatements=true
driver=com.mysql.jdbc.Driver

CRUD:

PreparedStatement preparedStatement = connection.preparedStatement(sql);
preparedStatement.setXxx(int n,xxx);//n代表第几个?,Xxx代表xxx的数值类型
int n = preparedStatement.executeUpdate();//增删改,n为执行结果影响的行数,当n>0,说明执行成功
ResultSet resultSet = preparedStatement.executeQuery();//查,resultSet为查询结果
while(resultSet.next()){
Xxx xx = resultSet.getXxx("字段名");
//String s = resultSet.getString("name");或者 getString(3),3代表name为表的第几列
System.out.print(xxx);
}

//最后关闭资源

事务:

Connection.setAutoCommit(false);//开启事务
Connection.rollback();//回滚,可以无参,也可以传入保存点
Connection.setSavepoint();设置保存点,可以无参,也可以传入String类型的保存点名
Connection.commit();//提交事务
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值