什么是JDBC?
Java连接数据库
需要的jar包支持
- java.sql
- javax.sql
- mysql-connecter-java...连接驱动(必须要导入)
实验环境搭建
User表
CREATE TABLE users (
id int PRIMARY KEY,
name VARCHAR(40),
password VARCHAR(40),
email VARCHAR(60),
birthday DATE);
INSERT INTO users(id,name,password,email,birthday)
VALUES(1,'张三','123456','563923245@qq.com','1999-04-15');
INSERT INTO users(id,name,password,email,birthday)
VALUES(2,'李四','123456','ls@qq.com','1996-05-17');
INSERT INTO users(id,name,password,email,birthday)
VALUES(3,'王五','123456','ww@qq.com','1997-06-13');SELECT * FROM users;
导入数据库依赖
<!--mysql的驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency>
IDEA中连接数据库
JDBC固定步骤
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.连接数据库,代表数据库
Connection connection = DriverManager.getConnection(url, username, password);
//3.向数据库发送SQL的对象Statement:做增删改查
Statement statement = connection.createStatement();
//4.编写SQL(根据业务,不同的SQL)
String sql = "select * from users";
//5.执行查询SQL,返回一个ResultSet:结果集
ResultSet resultSet = statement.executeQuery(sql);
//6.关闭连接
resultSet.close();
statement.close();
connection.close();
TestJDBC.java
package com.godairo.test;
import java.sql.*;
public class TestJDBC {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//配置信息
//useUnicode=true&characterEncoding=UTF-8 解决中文乱码问题
String url = "jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=UTF-8";
String username = "root";
String password = "root";
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.连接数据库,代表数据库
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 resultSet = statement.executeQuery(sql);
while (resultSet.next()){
System.out.println("id="+resultSet.getObject("id"));
System.out.println("name="+resultSet.getObject("name"));
System.out.println("password="+resultSet.getObject("password"));
System.out.println("email="+resultSet.getObject("email"));
System.out.println("birthday="+resultSet.getObject("birthday"));
}
//6.关闭连接,释放资源(一定要做),先开后关
resultSet.close();
statement.close();
connection.close();
}
}
这是查询,对于增删改我们都需要使用statement.executeUpdate
预编译SQL
package com.godairo.test;
import java.sql.*;
public class TestJDBC2 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//配置信息
//useUnicode=true&characterEncoding=UTF-8 解决中文乱码问题
String url = "jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=UTF-8";
String username = "root";
String password = "root";
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.连接数据库,代表数据库
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,4);//给第一个占位符?的值赋值为1;
preparedStatement.setString(2,"小可爱");//给第二个占位符?的值赋值为小可爱;
preparedStatement.setString(3,"123456");//给第三个占位符?的值赋值为123456;
preparedStatement.setString(4,"384483548@qq.com");//给第四个占位符?的值赋值为384483548@qq.com;
preparedStatement.setDate(5,new Date(new java.util.Date().getTime()));//给第五个占位符?的值赋值为Date(new java.util.Date().getTime());
//5.执行SQL
int i = preparedStatement.executeUpdate();
if (i>0){
System.out.println("插入成功!");
}
//6.关闭连接,释放资源(一定要做),先开后关
preparedStatement.close();
connection.close();
}
}
事务
事务经典的话:要么都成功,要么都失败!
ACID原则:保证数据的安全。
开启事务
事务提交 commit( )
事务回滚 rollback( )
关闭事务
转账:
A:1000
B:1000
A给B转100块钱,此时A:900,B:1100
转账的过程中突然服务器崩了,会发生可能A扣了,B没加到。假设A和B在一组事情里面发生,要么转账成功,要么都不成功,变成1000