黑马程序员技术交流社区

标题: [石家庄校区]数据库2 [打印本页]

作者: 社会人    时间: 2018-12-10 11:56
标题: [石家庄校区]数据库2
第三天多表查询事务
SELECT * FROM account;UPDATE account SET balance = 1000;-- 张三给李四转账 500 元
-- 0. 开启事务START TRANSACTION;-- 1. 张三账户 -500
UPDATE account SET balance = balance - 500 WHERE NAME = 'zhangsan';-- 2. 李四账户 +500-- 出错了...UPDATE account SET balance = balance + 500 WHERE NAME = 'lisi';
-- 发现执行没有问题,提交事务COMMIT;
-- 发现出问题了,回滚事务ROLLBACK;
DCL
SQL分类:        1. DDL:操作数据库和表        2. DML:增删改表中数据        3. DQL:查询表中数据        4. DCL:管理用户,授权
第四天JDBC抽取JDBC工具类:JDBCUtils
/**
/**
/**
}
JDBC控制事务:第五天数据库线程池
  public static void close(ResultSet rs , Statement stmt, Connection conn){
  if(rs != null){      try {          rs.close();      } catch (SQLException e) {          e.printStackTrace();      }  }
  if(stmt != null){      try {          stmt.close();      } catch (SQLException e) {          e.printStackTrace();      }  }
  if(conn != null){      try {          conn.close();//归还连接      } catch (SQLException e) {          e.printStackTrace();      }  }
  }
  /**
  public static DataSource getDataSource(){      return  ds;  }
  }
Spring JDBC
  //1. 获取JDBCTemplate对象  private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());  /**
*/   @Test   public void test1(){
  //2. 定义sql  String sql = "update emp set salary = 10000 where id = 1001";  //3. 执行sql  int count = template.update(sql);  System.out.println(count);}
  /**
  for (Emp emp : list) {      System.out.println(emp);  }
  }
  /**
*/
  @Test  public void test6_2(){      String sql = "select * from emp";      List<Emp> list = template.query(sql, new BeanPropertyRowMapper<Emp>(Emp.class));      for (Emp emp : list) {          System.out.println(emp);      }  }
  /**







欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2