MySQL官网下载mysql的jar包步骤
下载地址,看下版本下载好即可
把这个玩意搞出来复制到java项目中即可
看看导入成功没有两个地方
其实没导入代码也会报错的哈~
jdbc本质
java官方提 供的一套规范接口,用于帮助程序员开发者操作不同的关系型数据库(mysql、oracle、SqlServer)
在java官方知识提供jdbc规范的接口,如果需要连接到具体数据库比如mysql就需要导入mysql依赖jar包具体实现是由不同的数据库厂商决定的
以mysql为例:
- 导入mysql的驱动jar包
- 注册驱动( Javase反射机制 Class.forName())
- 获取数据库连接
- 获取执行者对象
- 执行sql语句并获取返回结果
- 对结果进行处理
- 释放jdbc资源
java
package com.lsl.jdbc.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcDemo {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
// Create a connection to the database
// com.mysql.jdbc.Driver 已经被弃用,推荐使用 com.mysql.cj.jdbc.Driver。
//从MySQL Connector/J 8.0版本开始,驱动程序通过Java的Service Provider Interface (SPI)机制自动注册,因此通常不需要手动加载驱动类。
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test"; // 你是数据库地址 默认本地
String user = "root"; // 数据库账户
String password = "xxxx"; // 数据库密码
Connection conn = DriverManager.getConnection(url, user, password);
System.out.println("Connected to the database");
// 获取到执行者对象
Statement stmt = conn.createStatement();
// 执行SQL语句
ResultSet rs = stmt.executeQuery("SELECT * FROM user");
// 处理结果集
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
System.out.println("id: " + id + ", name: " + name + ", age: " + age);
}
// 关闭资源
rs.close();
stmt.close();
conn.close();
System.out.println("Connection closed");
}
}
可以看到输出
一、JDBC 基础概念
-
作用 Java 访问关系型数据库的标准 API,提供与数据库交互的统一接口,独立于具体数据库实现。
-
核心组件
DriverManager
:管理数据库驱动。Connection
:数据库连接对象。Statement
/PreparedStatement
/CallableStatement
:执行 SQL 语句。ResultSet
:封装查询结果集。SQLException
:处理数据库异常。
分层架构
- com.lss.entity -- 实体类 创建实体类与数据库表结构字段一一对应
- com.lss.dao -- 数据库访问层 db处理
- com.lss.service -- 业务逻辑层 数据处理
- com.lss.controller -- 控制层 spring 才有
com.lss.entity -- 实体类 创建实体类与数据库表结构字段一一对应
java
package com.lss.entity;
/**
* 学生实体类 表结构的映射字段一一对应
* 如果db数据类型是varchar,则对应java类型String
* 如果db数据类型是int,则对应java类型Integer
* 如果db数据类型是long,则对应java类型Long
* 如果db数据类型是datetime,则对应java类型Date
* 如果db数据类型是decimal,则对应java类型BigDecimal
* 如果db数据类型是bit,则对应java类型Boolean
* 如果db数据类型是text,则对应java类型String
* 如果db数据类型是blob,则对应java类型byte[]
* 如果db数据类型是time,则对应java类型Date
* 如果db数据类型是enum,则对应java类型String
* 如果db数据类型是set,则对应java类型String[]
* 如果db数据类型是json,则对应java类型String
* ....
* @author lss
*/
public class StudentEntity {
/**
* 学生id
* 主键
*/
private Long id; // 默认值 null
/**
* 学生姓名
*/
private String name;
/**
* 学生年龄
*/
private Integer age;
/**
* 学生地址
*/
private String address;
/**
* 学生工作
*/
private String job;
// 不要id的构造方法
public StudentEntity(String name, Integer age, String address, String job) {
this.id = null; // 或者使用其他默认值
this.name = name;
this.age = age;
this.address = address;
this.job = job;
}
public StudentEntity(Long id, String name, Integer age, String address, String job) {
this.id = id;
this.name = name;
this.age = age;
this.address = address;
this.job = job;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
@Override
public String toString() {
return "StudentEntity{" +
"id=" + id +
", name='" + name + ''' +
", age=" + age +
", address='" + address + ''' +
", job='" + job + ''' +
'}';
}
public void setId(long l) {
this.id = l;
}
}
com.lss.dao -- 数据库访问层 db处理 crud操作
java
package com.lss.dao;
import com.lss.entity.StudentEntity;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
/**
* 学生数据库访问
* dao层负责处理数据库的读写操作 跟业务无关
*/
public class StudentDao {
/**
* 查询学生列表
*
* @return 学生列表
*/
public ArrayList<StudentEntity> getStudentList() {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
ArrayList<StudentEntity> studentList = new ArrayList<>();
try {
// 加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 连接数据库
String url = "jdbc:mysql://localhost:3306/test";
String user = "root";
String password = "xxx";
conn = DriverManager.getConnection(url, user, password);
// 创建Statement对象
stmt = conn.createStatement();
// 执行查询
String sql = "SELECT * FROM user";
rs = stmt.executeQuery(sql);
while (rs.next()) {
Long id = rs.getLong("id");
String name = rs.getString("name");
Integer age = rs.getInt("age");
String address = rs.getString("address");
String job = rs.getString("job");
// 封装数据
StudentEntity student = new StudentEntity(id, name, age, address, job);
// 加入集合
studentList.add(student);
}
return studentList;
} catch (Exception e) {
// 处理异常
e.printStackTrace();
return null;
} finally {
// 关闭资源
try {
if (rs != null)
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
if (stmt != null)
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
if (conn != null)
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 添加学生
*
* @param student 学生对象
* @return 影响的行数
*/
public int addStudent(StudentEntity student) throws SQLException {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
// 加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 连接数据库
String url = "jdbc:mysql://localhost:3306/test";
String user = "root";
String password = "xxx";
conn = DriverManager.getConnection(url, user, password);
// 创建Statement对象
stmt = conn.createStatement();
// 执行插入
String sql = "INSERT INTO user (name, age, address, job) VALUES ('" + student.getName() + "', " + student.getAge()
+ ", '" + student.getAddress() + "', '" + student.getJob() + "')";
int result = stmt.executeUpdate(sql);
System.out.println("Student added successfully"+result);
return result;
} catch (Exception e) {
// 处理异常
e.printStackTrace();
} finally {
// 关闭资源
try {
if (stmt != null)
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
if (conn != null)
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return 0;
}
/**
* 更新学生信息
*
* @param student 学生对象
*/
public void updateStudent(StudentEntity student) throws SQLException {
Connection conn = null;
Statement stmt = null;
try {
// 加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 连接数据库
String url = "jdbc:mysql://localhost:3306/test";
String user = "root";
String password = "xxx";
conn = DriverManager.getConnection(url, user, password);
// 创建Statement对象
stmt = conn.createStatement();
// 执行更新
String sql = "UPDATE user SET name = '" + student.getName() + "', age = " + student.getAge() + ", address = '"
+ student.getAddress() + "', job = '" + student.getJob() + "' WHERE id = " + student.getId();
stmt.executeUpdate(sql);
} catch (Exception e) {
// 处理异常
e.printStackTrace();
} finally {
// 关闭资源
try {
if (stmt != null)
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
if (conn != null)
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 删除学生
*
* @param id 学生id
*/
public void deleteStudent(Long id) throws SQLException {
Connection conn = null;
Statement stmt = null;
try {
// 加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 连接数据库
String url = "jdbc:mysql://localhost:3306/test";
String user = "root";
String password = "xxx";
conn = DriverManager.getConnection(url, user, password);
// 创建Statement对象
stmt = conn.createStatement();
// 执行删除
String sql = "DELETE FROM user WHERE id = " + id;
int count = stmt.executeUpdate(sql);
} catch (Exception e) {
// 处理异常
e.printStackTrace();
} finally {
// 关闭资源
try {
if (stmt != null)
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
if (conn != null)
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 根据id查询学生信息
* @param id 学生id
* @return 学生对象
*/
public StudentEntity getStudentById(Long id) throws SQLException {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
// 加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 连接数据库
String url = "jdbc:mysql://localhost:3306/test";
String user = "root";
String password = "xxx";
conn = DriverManager.getConnection(url, user, password);
// 创建Statement对象
stmt = conn.createStatement();
// 执行查询
String sql = "SELECT * FROM user WHERE id = " + id;
rs = stmt.executeQuery(sql);
if (rs.next()) {
Long studentId = rs.getLong("id");
String name = rs.getString("name");
Integer age = rs.getInt("age");
String address = rs.getString("address");
String job = rs.getString("job");
// 封装数据
return new StudentEntity(studentId, name, age, address, job);
}
return null;
} catch (Exception e) {
// 处理异常
e.printStackTrace();
return null;
} finally {
// 关闭资源
try {
if (rs != null)
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
if (stmt != null)
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
if (conn != null)
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
com.lss.service -- 业务逻辑层 数据处理
java
package com.lss.service;
import com.lss.dao.StudentDao;
import com.lss.entity.StudentEntity;
import java.sql.SQLException;
import java.util.ArrayList;
public class StudentService {
// new 出学生对象
private final StudentDao studentDao = new StudentDao();
// 定义方法,查询所有学生
public ArrayList<StudentEntity> queryAllStudent() {
return studentDao.getStudentList();
}
// 定义方法,添加学生
public int addStudent(StudentEntity student) throws SQLException {
return studentDao.addStudent(student);
}
// 定义方法,更新学生信息
public void updateStudent(StudentEntity student) throws SQLException {
studentDao.updateStudent(student);
}
// 定义方法,删除学生
public void deleteStudent(Long id) throws SQLException {
studentDao.deleteStudent(id);
}
// 定义方法,根据id查询学生
public StudentEntity queryStudentById(Long id) throws SQLException {
return studentDao.getStudentById(id);
}
}
测试
java
package com.lss.test;
import com.lss.entity.StudentEntity;
import com.lss.service.StudentService;
import java.sql.SQLException;
public class StudentTest {
public static void main(String[] args) throws SQLException {
// 示例:查询所有学生数据
StudentService studentService = new StudentService();
System.out.println(studentService.queryAllStudent());
// 示例:添加学生数据
StudentEntity studentEntity = new StudentEntity(null, "王五", 20, "北京", "学生");
int result = studentService.addStudent(studentEntity);
System.out.println("result ==== " + result);
System.out.println(studentService.queryAllStudent());
// 示例:删除学生数据
studentService.deleteStudent(8L);
System.out.println(studentService.queryAllStudent());
// 示例:修改学生数据
studentEntity.setId(1L);
studentEntity.setName("李四5");
studentEntity.setAge(22);
studentEntity.setAddress("上海1");
studentEntity.setJob("teacher");
studentService.updateStudent(studentEntity);
// 示例:查询学生数据
System.out.println(studentService.queryStudentById(1L));
}
}
使用工具类优化代码
java
package com.lss.utils;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;
/**
* 工具类
* 构造方法私有化
* 工具类不允许实例化,不需要new出来 直接类名.方法名即可调用
* @author lss
* @create 2022年03月19日 15:52
*/
public class JdbcUtils<inputStream> {
// 私有化构造方法,防止被new出来
private JdbcUtils() {
}
// 定义工具类 需要声明变量
private static String DriverClass;
private static String URL;
private static String UserName;
private static String PassWord;
// 给声明好的 jdbc的变量赋值
static {
try {
// 读取配置文件 config.properties
InputStream resourceAsStream = JdbcUtils.class.getClassLoader().getResourceAsStream("config.properties");
// 获取属性值
Properties properties = new Properties();
properties.load(resourceAsStream);
DriverClass = properties.getProperty("DriverClass");
URL = properties.getProperty("URL");
UserName = properties.getProperty("UserName");
PassWord = properties.getProperty("PassWord");
} catch (Exception e){
e.printStackTrace();
}
}
// 连接sql数据库方法
public static Connection getConnection(){
try {
// 获取连接
return DriverManager.getConnection(URL, UserName, PassWord);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
// 关闭数据库连接方法
// 查询,释放jdbc资源
public static void closeConnection(ResultSet resultSet, Statement statement, Connection connection){
try {
if (resultSet!= null) {
resultSet.close();
}
if (statement!= null) {
statement.close();
}
if (connection!= null) {
connection.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
// 增删改 ---释放jdbc资源重载上面方法
public static void closeConnection(Statement statement, Connection connection){
closeConnection(null, statement, connection);
}
public static void main(String[] args) throws ClassNotFoundException {
// Initialize the driver class
Class.forName(DriverClass);
// 测试工具类是否可以正常获取配置文件的属性值
System.out.println(JdbcUtils.DriverClass);
System.out.println(JdbcUtils.URL);
System.out.println(JdbcUtils.UserName);
System.out.println(JdbcUtils.PassWord);
}
}
更改dao层代码
java
package com.lss.dao;
import com.lss.entity.StudentEntity;
import com.lss.utils.JdbcUtils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
/**
* 学生数据库访问
* dao层负责处理数据库的读写操作 跟业务无关
*/
public class StudentDao {
/**
* 查询学生列表
*
* @return 学生列表
*/
public ArrayList<StudentEntity> getStudentList() {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
ArrayList<StudentEntity> studentList = new ArrayList<>();
try {
// 工具类替换掉
conn = JdbcUtils.getConnection();
// 创建Statement对象
assert conn != null;
stmt = conn.createStatement();
// 执行查询
String sql = "SELECT * FROM user";
rs = stmt.executeQuery(sql);
while (rs.next()) {
Long id = rs.getLong("id");
String name = rs.getString("name");
Integer age = rs.getInt("age");
String address = rs.getString("address");
String job = rs.getString("job");
// 封装数据
StudentEntity student = new StudentEntity(id, name, age, address, job);
// 加入集合
studentList.add(student);
}
return studentList;
} catch (Exception e) {
// 处理异常
e.printStackTrace();
return null;
} finally {
// 关闭资源
JdbcUtils.closeConnection(rs, stmt, conn);
}
}
/**
* 添加学生
*
* @param student 学生对象
* @return 影响的行数
*/
public int addStudent(StudentEntity student) throws SQLException {
Connection conn = null;
Statement stmt = null;
try {
// 连接数据库
conn = JdbcUtils.getConnection();
// 创建Statement对象
assert conn != null;
stmt = conn.createStatement();
// 执行插入
String sql = "INSERT INTO user (name, age, address, job) VALUES ('" + student.getName() + "', " + student.getAge()
+ ", '" + student.getAddress() + "', '" + student.getJob() + "')";
return stmt.executeUpdate(sql);
} catch (Exception e) {
// 处理异常
e.printStackTrace();
} finally {
// 关闭资源
JdbcUtils.closeConnection(stmt, conn);
}
return 0;
}
/**
* 更新学生信息
*
* @param student 学生对象
*/
public void updateStudent(StudentEntity student) throws SQLException {
Connection conn = null;
Statement stmt = null;
try {
// 连接数据库
conn = JdbcUtils.getConnection();
// 创建Statement对象
assert conn != null;
stmt = conn.createStatement();
// 执行更新
String sql = "UPDATE user SET name = '" + student.getName() + "', age = " + student.getAge() + ", address = '"
+ student.getAddress() + "', job = '" + student.getJob() + "' WHERE id = " + student.getId();
stmt.executeUpdate(sql);
} catch (Exception e) {
// 处理异常
e.printStackTrace();
} finally {
// 关闭资源
JdbcUtils.closeConnection(stmt, conn);
}
}
/**
* 删除学生
*
* @param id 学生id
*/
public void deleteStudent(Long id) throws SQLException {
Connection conn = null;
Statement stmt = null;
try {
// 连接数据库
conn = JdbcUtils.getConnection();
// 创建Statement对象
assert conn != null;
stmt = conn.createStatement();
// 执行删除
String sql = "DELETE FROM user WHERE id = " + id;
int count = stmt.executeUpdate(sql);
} catch (Exception e) {
// 处理异常
e.printStackTrace();
} finally {
// 关闭资源
JdbcUtils.closeConnection(stmt, conn);
}
}
/**
* 根据id查询学生信息
* @param id 学生id
* @return 学生对象
*/
public StudentEntity getStudentById(Long id) throws SQLException {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
// 连接数据库
conn = JdbcUtils.getConnection();
// 创建Statement对象
assert conn != null;
stmt = conn.createStatement();
// 执行查询
String sql = "SELECT * FROM user WHERE id = " + id;
rs = stmt.executeQuery(sql);
if (rs.next()) {
Long studentId = rs.getLong("id");
String name = rs.getString("name");
Integer age = rs.getInt("age");
String address = rs.getString("address");
String job = rs.getString("job");
// 封装数据
return new StudentEntity(studentId, name, age, address, job);
}
return null;
} catch (Exception e) {
// 处理异常
e.printStackTrace();
return null;
} finally {
// 关闭资源
JdbcUtils.closeConnection(rs, stmt, conn);
}
}
}
数据完整的正确,数据库也正确!!!
二、JDBC 架构与驱动类型
-
架构模型 应用程序 → JDBC API → 驱动管理器 → 数据库驱动 → 数据库。
-
驱动类型
- Type 1: JDBC-ODBC 桥接(已淘汰)。
- Type 2: 部分 Java + 本地 API(如 Oracle OCI)。
- Type 3: 网络协议驱动(中间件转发)。
- Type 4 : 纯 Java 驱动(直接连接,如 MySQL
com.mysql.cj.jdbc.Driver
)。
三、JDBC 基本使用步骤
-
加载驱动
javaClass.forName("com.mysql.cj.jdbc.Driver"); // JDBC 4.0+ 可自动加载
-
建立连接
javaConnection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/db", "user", "password");
-
创建 Statement
Statement
: 普通 SQL 语句(易 SQL 注入)。PreparedStatement
: 预编译 SQL(防注入,高效复用)。CallableStatement
: 调用存储过程。
-
执行 SQL
java// 查询 ResultSet rs = statement.executeQuery("SELECT * FROM table"); // 更新 int rows = statement.executeUpdate("UPDATE table SET col=val");
-
处理结果集
javawhile (rs.next()) { String data = rs.getString("column_name"); }
-
关闭资源 按顺序关闭
ResultSet
→Statement
→Connection
(推荐try-with-resources
)。
四、连接管理与连接池
-
数据源配置 使用
DataSource
(如HikariCP
、DBCP
)替代DriverManager
提升性能。javaHikariConfig config = new HikariConfig(); config.setJdbcUrl("jdbc:mysql:///db"); HikariDataSource ds = new HikariDataSource(config); Connection conn = ds.getConnection();
-
连接池优势
- 复用连接,减少开销。
- 控制最大连接数,避免资源耗尽。
五、事务处理
-
手动提交事务
javaconn.setAutoCommit(false); // 关闭自动提交 try { // 执行多个操作 conn.commit(); } catch (SQLException e) { conn.rollback(); }
-
隔离级别
javaconn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
六、PreparedStatement 与防 SQL 注入
-
预编译 SQL
javaString sql = "SELECT * FROM users WHERE username = ? AND password = ?"; PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, user); ps.setString(2, pwd); ResultSet rs = ps.executeQuery();
-
优势
- 防止 SQL 注入。
- 提升性能(SQL 预编译)。
七、批处理与性能优化
- 批量更新
java
PreparedStatement ps = conn.prepareStatement("INSERT INTO table VALUES (?)");
for (Data data : list) {
ps.setString(1, data.getValue());
ps.addBatch();
}
int[] counts = ps.executeBatch();
-
优化手段
- 使用批处理减少网络开销。
- 设置
fetchSize
优化查询性能。
八、ResultSet 高级特性
-
结果集类型
TYPE_FORWARD_ONLY
:默认,仅向前遍历。TYPE_SCROLL_INSENSITIVE
:可滚动,不感知外部变更。TYPE_SCROLL_SENSITIVE
:可滚动,感知外部变更。
-
并发模式
CONCUR_READ_ONLY
:只读。CONCUR_UPDATABLE
:可更新结果集。
九、元数据(Metadata)
-
DatabaseMetaData 获取数据库信息(如版本、表结构)。
javaDatabaseMetaData meta = conn.getMetaData(); ResultSet tables = meta.getTables(null, null, "%", new String[]{"TABLE"});
-
ResultSetMetaData 获取结果集列信息。
javaResultSetMetaData rsmd = rs.getMetaData(); int columnCount = rsmd.getColumnCount();
十、异常处理
- SQLException 捕获数据库操作异常,支持链式异常(
getNextException()
)。 - SQL 状态码 通过
SQLException.getSQLState()
获取标准错误码。
十一、JDBC 高级特性
- RowSet 离线结果集(如
CachedRowSet
),支持序列化与断开连接操作。 - JNDI 集成 在 Java EE 环境中通过 JNDI 获取
DataSource
。 - 分布式事务(XA) 使用
XADataSource
和XAResource
支持两阶段提交。
十二、常见问题与注意事项
-
SQL 注入 强制使用
PreparedStatement
,避免拼接 SQL。 -
资源泄漏 确保所有资源(Connection、Statement、ResultSet)在 finally 块或 try-with-resources 中关闭。
-
性能优化
- 合理配置连接池参数。
- 减少数据库交互次数(如批量操作)。
-
数据库兼容性 不同数据库的 SQL 语法差异需处理(如分页 SQL)。
十三、JDBC 与 ORM 框架
- 对比 ORM(如 Hibernate、MyBatis) JDBC 更底层,灵活但代码量大;ORM 简化 CRUD,但需学习框架语法。
连接时候常见错误
账号/密码错误
数据库地址错误
SQL语句表名错误
通过以上知识点梳理,可以系统掌握 JDBC 的核心技术与最佳实践。实际开发中建议结合连接池和预编译语句提升性能与安全性。