赞
踩
- @Test
- public void test() {
- Connection con = null;
- PreparedStatement ps=null;
- String sql="INSERT INTO BatchTest(`name`) VALUES (?)";
- //没配置批处理共花费时间:61210
- //打开批处理共花费时间:177
- try {
- con=JDBCUtil.getConnection();
- ps=con.prepareStatement(sql);
- //创建一个for循环,来设置占位符
- for (int i = 0; i < 1000; i++) {
- ps.setString(1, "test"+i);
- //添加到批处理方法中,调用无参的,有参的是Statement来调用的!
- ps.addBatch();
-
- }
- //获取一个时间戳
- long start=System.currentTimeMillis();
- //执行批处理
- ps.executeBatch();
- //获取一个时间戳
- long end=System.currentTimeMillis();
- System.out.println("共花费时间:"+(end-start));
- } catch (SQLException e) {
- e.printStackTrace();
- }finally {
- JDBCUtil.close(con, ps);
- }
- }
- public class AcountDao {
- public void update(Connection conn,String name,double money) {
- PreparedStatement ps=null;
- String sql = "update trade set money = money + ? where name = ?";
- try {
- ps = conn.prepareStatement(sql);
- ps.setInt(1, money);
- ps.setString(2, name);
- ps.execute();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }finally {
- JDBCUtil.close(null, ps);
- }
-
- }
- }
- public class TestTransaction {
- private AcountDao dao = new AcountDao();
-
- @Test
- public void test() {
- //从sunwukong账户向shaheshang账户转账100元!
- //1.从sunwukong账户扣除100元
- dao.update("sunwukong", -100);
- //2.向shaheshang账户添加100元
- dao.update("shaheshang", 100);
- }
- }
- //从sunwukong账户向shaheshang账户转账100元!
- //1.从sunwukong账户扣除100元
- accountDao.update("sunwukong", -100);
- int i =10/0;//添加一个异常
- //2.向shaheshang账户添加100元
- accountDao.update("shaheshang", 100);
- //创建一个Connection
- Connection conn = null;
-
- try{
-
- //获取Connection
- conn = JDBCUtils.getConnection();
-
- //开启事务
- conn.setAutoCommit(false);
-
- //对数据库进行操作
-
- //操作成功,提交事务
- conn.commit();
-
- }catch(Exception e){
- e.printStackTrace();
-
- //回滚事务
- try {
- conn.rollback();
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
-
- }finally{
- JDBCUtils.close(conn, null, null);
- }
- public class AcountDao {
- public void update(Connection conn,String name,double money){
- //准备两个变量
- PreparedStatement ps = null;
- //准备SQL模板
- String sql = "UPDATE trade SET money = money + ? WHERE name = ?";
-
- try {
- //获取PreparedStatement
- ps = conn.prepareStatement(sql);
- //填充占位符
- ps.setDouble(1, money);
- ps.setString(2, name);
-
- //执行SQL语句
- ps.executeUpdate();
-
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }finally{
- //此时也不能在这里关闭数据库连接了,而是在外边统一关闭
- JDBCUtil.close(null, ps, null);
- }
- }
- }
- @Test
- public void testTranscateandButch() {
- Connection con=null;
- PreparedStatement ps=null;
- String sql="INSERT INTO BatchTest(`name`) VALUES (?)";
- //事务结合批处理共花费时间:19
- try {
- con=JDBCUtil.getConnection();
-
- con.setAutoCommit(false);
-
- ps=con.prepareStatement(sql);
- for (int i = 0; i < 1000; i++) {
- ps.setString(1, "test"+i);
- ps.addBatch();
-
- }
- long start=System.currentTimeMillis();
- ps.executeBatch();
- long end=System.currentTimeMillis();
- System.out.println("共花费时间:"+(end-start));
- con.commit();
-
-
- } catch (Exception e) {
- // TODO: handle exception
- System.out.println(e.getMessage());
- try {
- con.rollback();
- } catch (SQLException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- }finally {
- JDBCUtil.close(con, null);
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。