使用JDBC实现工具类抽取需要遵循以下一般步骤:
- 加载JDBC驱动
- 创建数据库连接
- 创建Statement/PreparedStatment对象
- 执行SQL语句
- 处理结果集
- 释放资源
下面通过两个示例说明具体操作。
示例1:查询数据库
public class JdbcUtil {
private static String url = "jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC";
private static String user = "root";
private static String password = "123456";
static {
try {
// 加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, user, password);
}
public static void close(Connection conn, Statement stmt, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static List<User> getUsers(String nameLike) {
List<User> userList = new ArrayList<>();
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
// 获取连接
conn = JdbcUtil.getConnection();
// 预编译sql
String sql = "SELECT id, name, age, gender, phone FROM user WHERE name LIKE ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "%" + nameLike + "%");
// 执行查询操作
rs = pstmt.executeQuery();
while (rs.next()) {
User user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setAge(rs.getInt("age"));
user.setGender(rs.getString("gender"));
user.setPhone(rs.getString("phone"));
userList.add(user);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 释放资源
JdbcUtil.close(conn, pstmt, rs);
}
return userList;
}
}
public class User {
private Integer id;
private String name;
private Integer age;
private String gender;
private String phone;
// 省略getter/setter方法
}
public class TestJdbcUtil {
public static void main(String[] args) {
List<User> userList = JdbcUtil.getUsers("张");
for (User user : userList) {
System.out.println(user);
}
}
}
示例2:插入数据
public class JdbcUtil {
private static String url = "jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC";
private static String user = "root";
private static String password = "123456";
static {
try {
// 加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, user, password);
}
public static void close(Connection conn, Statement stmt, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static boolean addUser(User user) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = JdbcUtil.getConnection();
String sql = "INSERT INTO user(name, age, gender, phone) VALUES(?, ?, ?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, user.getName());
pstmt.setInt(2, user.getAge());
pstmt.setString(3, user.getGender());
pstmt.setString(4, user.getPhone());
int count = pstmt.executeUpdate();
if (count > 0) {
return true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JdbcUtil.close(conn, pstmt, null);
}
return false;
}
}
public class User {
private Integer id;
private String name;
private Integer age;
private String gender;
private String phone;
// 省略getter/setter方法
}
public class TestJdbcUtil {
public static void main(String[] args) {
User user = new User();
user.setName("张三");
user.setAge(20);
user.setGender("男");
user.setPhone("13800000000");
boolean isSuccess = JdbcUtil.addUser(user);
if (isSuccess) {
System.out.println("添加成功!");
} else {
System.out.println("添加失败!");
}
}
}
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:如何使用JDBC实现工具类抽取 - Python技术站