文章目录
JDBC 完成 CRUD 操作总结
准备工作
开发环境和工具
Eclipse、mysql、通过 JDBC 连接 mysql 数据库时需要的架包 mysql-connector-java。
JDBC 工具类
将获取连接和释放资源的方法封装到一个工具类中,之后在使用的时候直接调用此类中的方法即可,减少了代码量,代码实现见下方。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.junit.Test;
/**
* 工具类
* @author Tong
*
*/
public class JDBCUtils_V1 {
/**
* 获取连接方法
* @return
*/
@Test
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/web08", "root", "1");
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
/**
* 释放资源方法
* @param conn
* @param pstmt
* @param rs
*/
@Test
public static void release(Connection conn, PreparedStatement pstmt, ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
初始数据库表记录
CRUD 操作
根据 id 查询用户信息(可传参)
查询方法
/**
* 根据 id 查询用户信息
*/
@Test
public void testFindUserById() {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
// 1. 获取连接
conn = JDBCUtils_V1.getConnection();
// 2. 书写 sql 语句
String sql = "select * from tbl_user where uid=?";
// 3. 获取执行 sql 语句对象
pstmt = conn.prepareStatement(sql);
// 4. 设置参数
pstmt.setString(1, "2"); // 查询 uid = 2 的用户信息
// 5. 执行 sql 语句
rs = pstmt.executeQuery();
// 6. 处理结果集
while (rs.next()) {
System.out.println(rs.getString(2) + ", " + rs.getString("upassword"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 7. 释放资源
JDBCUtils_V1.release(conn, pstmt, rs);
}
}
查询结果
添加用户信息
添加方法,添加一条记录,uname = ‘zhaoliu’,upassword = ‘666’。
/**
* 添加用户信息
*/
@Test
public void testAdd() {
Connection conn = null;
PreparedStatement pstmt = null;
try {
// 1. 获取连接
conn = JDBCUtils_V2.getConnection();
// 2. 编写 sql 语句
String sql = "insert into tbl_user values(null,?,?)";
// 3. 获取执行 sql 语句的对象
pstmt = conn.prepareStatement(sql);
// 4. 设置参数
pstmt.setString(1, "zhaoliu");
pstmt.setString(2, "999");
// 5. 执行插入操作(返回影响的行数)
int row = pstmt.executeUpdate();
if (row > 0) {
System.out.println("添加成功!");
} else {
System.out.println("添加失败!");
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
JDBCUtils_V2.release(conn, pstmt, null);
}
}
添加结果
根据 id 修改用户信息(可传参)
修改方法,将 uid 为 2 的记录中的 upassword 改为 999。
/**
* 修改用户信息
*/
@Test
public void testUpdateById() {
Connection conn = null;
PreparedStatement pstmt = null;
try {
// 1. 获取连接
conn = JDBCUtils_V3.getConnection();
// 2. 编写 sql 语句
String sql = "update tbl_user set upassword=? where uid=?";
// 3. 获取执行 sql 语句的对象
pstmt = conn.prepareStatement(sql);
// 4. 设置参数
pstmt.setString(1, "999");
pstmt.setInt(2, 1);
// 5. 执行 sql 语句
int row = pstmt.executeUpdate();
if (row > 0) {
System.out.println("更新成功!");
} else {
System.out.println("更新失败!");
}
} catch(Exception e) {
throw new RuntimeException(e);
} finally {
JDBCUtils_V3.release(conn, pstmt, null);
}
}
修改结果
根据 id 删除用户信息(可传参)
删除方法,删除 uid 为 4 的记录。
/**
* 删除用户信息
*/
@Test
public void testDeleteById() {
Connection conn = null;
PreparedStatement pstmt = null;
try {
// 1. 获取连接
conn = JDBCUtils_V3.getConnection();
// 2. 编写 sql 语句
String sql = "delete from tbl_user where uid=?";
// 3. 获取执行 sql 语句的对象
pstmt = conn.prepareStatement(sql);
// 4. 设置参数
pstmt.setInt(1, 4);
// 5. 执行删除操作
int row = pstmt.executeUpdate();
if (row > 0) {
System.out.println("删除成功!");
} else {
System.out.println("删除失败!");
}
} catch(Exception e) {
throw new RuntimeException(e);
} finally {
JDBCUtils_V3.release(conn, pstmt, null);
}
}
删除结果
总结
JDBC开发一般步骤
使用 Statement
- 注册驱动
- 获取连接
- 书写 sql 语句
- 获取执行 sql 语句对象(Statement)
- 执行 sql 语句,获取结果集
- 处理结果集
- 释放资源
使用 PreparedStatement(防 sql 注入)
- 注册驱动
- 获取连接
- 书写 sql 语句
- 获取执行 sql 语句对象(PreparedStatement)
- 设置参数
- 执行 sql 语句,获取结果集
- 处理结果集
- 释放资源
涉及知识点
数据库 sql 语句(增删改查)、Java 基础语法(异常处理等)、Test单元测试、JDBC连接数据库开发步骤等。