1.预编译
1.1是预编译:提前编译SQL语句
1.2预编译的操作
加载驱动
创建连接对象
创建预编译对象,同时编写sql语句
补齐sql
执行sql语句
操作结果集
释放资源
1.3CRUD(曾,删,改,查询全部,查询单条)
//CRUD
@Test
public void jdbcAdd() throws ClassNotFoundException, SQLException {
//需求:给user添加数据
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//创建连接对象
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test4?characterEncoding=utf-8",
"root", "root");
//创建操作数据库对象
Statement stat = conn.createStatement();
//编写sql语句
String sql =" insert into user(name,age,address,birthday) values('曹操',40,'北大青
鸟','2019-04-26') " ;
//执行sql
int i = stat.executeUpdate(sql);
//操作结果集
if(i>0){
System.out.println("添加成功!");
}else{
System.out.println("添加失败!");
}
//释放资源
stat.close();
conn.close();
}
/**
* 修改
*/
@Test
public void jdbcUpdate() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test4?
characterEncoding=utf-8");
Statement stat = conn.createStatement();
String sql =" update user set name='小乔',age=18 ,address='武汉' ,birthday='2018-08-
08' where id=7 ";
int i = stat.executeUpdate(sql);
if(i>0){
System.out.println("修改成功!");
}else{
System.out.println("修改失败!");
}
stat.close();
conn.close();
}
//删除
@Test
public void jdbcDelete() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test4?
characterEncoding=utf-8");
Statement stat = conn.createStatement();
String sql =" delete from user where id = 6 ";
int i = stat.executeUpdate(sql);
if(i>0){
System.out.println("删除成功!");
}else {
System.out.println("删除失败!");
}
stat.close();
conn.close();
}
@Test
public void queryAll(String[] args) throws ClassNotFoundException, SQLException {
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
// 2.创建连接数据库对象
Connection conn=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test4?characterEncoding=utf-8","root","root");
DBUtil
抽取过程
1.写出完整版的JDBC(CRUD)
2.抽取四要素、加载驱动、创建数据库连接、关闭连接、改动操作(曾、删、改)、设置参数、查询操作
3.把上面所以得方法放在DBUtile类中
4.简化JDBC(CRUD)
5.创建Properties配置文件,在DBUtile读取配置文件信息,赋值给四要素
DBUtil.java
// 3.创建操作数据库对象
Statement stat=conn.createStatement();
// 4.编写sql语句
String sql =" select name from user ";
// 5.执行sql语句,返回结果集
ResultSet rs=stat.executeQuery(sql);
// 6.操作结果集
while(rs.next()){
int id = rs.getInt(1);
String name = rs.getString(2);
int age = rs.getInt(3);
String address = rs.getString(4);
Date birthday = rs.getDate(5);
System.out.println(id+" "+name+" "+age+" "+address+" "+birthday);
}
// 7.释放资源
rs.close();
stat.close();
conn.close();
}
public class DBUtil {
//连库四要素
private static String driverName;
private static String url;
private static String username;
private static String password;
static{
try {
//创建Properties对象
Properties prop=new Properties();
//创建输入流对象,指向配置文件
InputStream is= new FileInputStream("config/db.properties");
//加载
prop.load(is);
//获取文件中的数据
driverName=prop.getProperty("driverName");
url =prop.getProperty("url");
username=prop.getProperty("username");
password=prop.getProperty("password");
} catch (IOException e) {
e.printStackTrace();
}
}
//加载驱动
private static void driverLoader(){
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//创建连接对象
public static Connection getConnection(){
try {
driverLoader();
//创建连接对象
return DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
//关闭连接对象
public static void closeAll(Connection conn){
if(conn==null) return;
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
//执行改动操作
public static void executeUpdate(Connection conn,String sql,Object... args){
if(conn==null)return;
// 创建预编译对象,同时编写sql语句
PreparedStatement pst = null;
try {
pst = conn.prepareStatement(sql);
//设置参数
setPropretie(pst,args);
// 执行sql语句
pst.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
//设置参数
private static void setPropretie(PreparedStatement pst,Object... args){
if(pst==null) return;
if(args==null) return;
try {
for(int i=0;i<args.length;i++){
pst.setObject(i+1,args[i]);
}
db.properties
JDBC.java
} catch (SQLException e) {
e.printStackTrace();
}
}
//执行查询操作
public static ResultSet executeQuery(Connection conn, String sql, Object... args){
if(conn==null) return null;
ResultSet rs =null;
try {
//创建预编译对象
PreparedStatement pst = conn.prepareStatement(sql);
//设置参数
setPropretie(pst,args);
//执行sql
rs = pst.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
driverName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test4?characterEncoding=utf-8
username=root
password=root
//预编译操作数据库 添加数据
@Test
public void jdbcAdd() throws SQLException {
// 创建连接对象
Connection conn = DBUtil.getConnection();
String sql =" insert into user(name,age,address,birthday) values(?,?,?,?)";
DBUtil.executeUpdate(conn,sql,"周瑜",25,"吴国",new Date(new
java.util.Date().getTime()));
//释放资源
DBUtil.closeAll(conn);
}
//修改
@Test
public void jdbcUpdate() throws SQLException {
//创建连接对象
Connection conn =DBUtil.getConnection();
String sql ="update user set name=?,age=?,address=?,birthday= ? where id = ? ";
DBUtil.executeUpdate(conn,sql,"孙尚香",22,"吴国",new Date(new
java.util.Date().getTime()),18);
// 释放资源
DBUtil.closeAll(conn);
}
//删除
@Test
public void jdbcDelete() throws SQLException {
Connection conn = DBUtil.getConnection();
String sql =" delete from user where id = ? ";
DBUtil.executeUpdate(conn,sql,3);
DBUtil.closeAll(conn);
}
//查询所有
@Test
public void jdbcQueryAll() throws SQLException {
Connection conn = DBUtil.getConnection();
String sql =" select id,name,age,address,birthday from user ";
//执行查询操作
ResultSet rs = DBUtil.executeQuery(conn, sql);
while(rs.next()){
int id = rs.getInt(1);
String name = rs.getString(2);
int age = rs.getInt(3);
String address = rs.getString(4);
Date birthday = rs.getDate(5);
System.out.println(id+" "+name+" "+age+" "+address+" "+birthday);
}
DBUtil.closeAll(conn);
}
//查询一条
@Test
public void jdbcQueryById() throws SQLException {
Connection conn = DBUtil.getConnection();
String sql =" select * from user where id = ? ";
ResultSet rs =DBUtil.executeQuery(conn,sql,7);
while(rs.next()){
int id = rs.getInt(1);
String name = rs.getString(2);
int age = rs.getInt(3);
String address = rs.getString(4);
Date birthday = rs.getDate(5);
System.out.println(id+" "+name+" "+age+" "+address+" "+birthday);
}
DBUtil.closeAll(conn);
}
事务
什么是事务?
一组操作,要么全执行,要么全不执行。
同生共死
事务的特性
原子性,
事务的最小的操作单位。
一致性,
要么全执行,要么全不执行,从一个状态到另一个状态总量不会发生改变。
隔离性,
事务与事务之间是独立存在的,“互不干扰”
持久性
事务一旦提交,无法回滚,不能反向操作
隔离问题
脏读
事务A读到了事务B未提交的数据
不可重复读
事务A在多次读取数据的时候,事务B修改了数据,导致事务A多次读取的数据是不一致的。
幻读
事务A在批量更改数据时,事务B插入了一条数据,事务A在查询操作结果会发现,有一条数据没有被改动
隔离级别
读未提交 什么都解决不了
不可重复读 解决脏读
可重复读 不可重复读 mysql
串行话 幻读
事务的操作
@Test
public void add() {
Connection conn =null;
try{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test4?
characterEncoding=utf-8", "root", "root");
//关闭事务的自动提交
conn.setAutoCommit(false);
PreparedStatement pst = conn.prepareStatement("update people set aslary=aslary-
1000 where id = ?");
pst.setInt(1,2);
pst.executeUpdate();
int i=10/0;
pst=conn.prepareStatement(" update people set aslary=aslary+1000 where id = ? ");
pst.setInt(1,3);
pst.executeUpdate();
//提交事务
conn.commit();
conn.close();
}catch (Exception e){
e.printStackTrace();
if(conn!=null){
try {
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}