什么是JDBC
Java连接数据库
JDBC固定步骤:
- 加载驱动
- 连接数据库
- 向数据库发送SQL对象Statement:CRUD
- 编写SQL(根据业务,写不同的SQL)
- 执行SQL
- 关闭连接
public class TestJdbc {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//配置信息
String url = "jdbc:mysql://localhost:3306/jdbc";
String username = "root";
String password = "123456";
//1.加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//2.连接数据库 connection代表数据库
Connection connection = DriverManager.getConnection(url, username, password);
//3.向数据库发送SQL的对象Statement
Statement statement = connection.createStatement();
//4.编写SQL
String sql = "select * from users";
//5.执行查询SQL,返回一个结果集 ResultSet
ResultSet rs = statement.executeQuery(sql);
while (rs.next()){
System.out.println("id="+rs.getObject("id"));
System.out.println("name="+rs.getObject("name"));
System.out.println("password="+rs.getObject("password"));
System.out.println("email="+rs.getObject("email"));
System.out.println("birthday="+rs.getObject("birthday"));
}
//6.关闭链接,释放资源 先开的后关
rs.close();
statement.close();
connection.close();
}
}
预编译SQL
public class TestJdbc2 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//配置信息
String url = "jdbc:mysql://localhost:3306/jdbc";
String username = "root";
String password = "123456";
//1.加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//2.连接数据库 connection代表数据库
Connection connection = DriverManager.getConnection(url, username, password);
//3.编写SQL
String sql = "insert into users(id, name, password, email, birthday) values (?,?,?,?,?);";
//4.预编译
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1,5);//给第一个占位符“?”的值赋为1
preparedStatement.setString(2,"小红");//给第二个占位符“?”的值赋为“小红”
preparedStatement.setString(3,"123456");//给第三个占位符“?”的值赋为123456
preparedStatement.setString(4,"123456@qq.com");//给第四个占位符“?”的值赋为123456@qq.com
preparedStatement.setDate(5,new Date(new java.util.Date().getTime()));//给第五个占位符“?”的值赋为2000.11.3
//5.执行查询SQL,返回一个结果集 ResultSet
int i = preparedStatement.executeUpdate();
if(i>0){
System.out.println("插入成功");
}
//6.关闭链接,释放资源 先开的后关
preparedStatement.close();
connection.close();
}
}
JDBC事务
要么都成功,要么都失败
ACID原则:保证数据的安全
- 开启事务
- 事务提交 commit()
- 事务回滚 rollback()
- 关闭事务
设置自动提交(开启事务): setAutoCommit() 设置自动提交,方法中需要传入一个boolean类型的参数, true为自动提交,false为手动提交
原子性、一致性、隔离性、持久性
public class TestJdbc3 {
@Test
public void test(){
//配置信息
String url = "jdbc:mysql://localhost:3306/jdbc";
String username = "root";
String password = "123456";
Connection connection = null;
try {
//1.加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//2.连接数据库 connection代表数据库
connection = DriverManager.getConnection(url, username, password);
//3.通知数据库开启事务
connection.setAutoCommit(false);
String sql ="update account set money = money-100 where name = 'A'";
//执行sql
connection.prepareStatement(sql).executeUpdate();
//制造错误
// int i=1/0;
String sql2 ="update account set money = money+100 where name = 'B'";
//执行sql
connection.prepareStatement(sql2).executeUpdate();
connection.commit();//以上两条SQL都执行成功了,就提交事务
System.out.println("success");
} catch (Exception e) {
try {
//如果有异常,就通知数据库回滚事务
connection.rollback();
} catch (SQLException ex) {
ex.printStackTrace();
}
e.printStackTrace();
}finally {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}