package javabean; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class Base { // 数据库连接信息 private static String driver = "com.mysql.cj.jdbc.Driver"; private static String url = "jdbc:mysql://localhost:3306/library?&useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8"; private static String username = "root"; private static String password = "root"; /** * 获取数据库连接 * * @return 返回数据库连接对象 * @throws ClassNotFoundException 如果未找到JDBC驱动类 */ public static Connection getConnection() throws ClassNotFoundException { Connection connection = null; try { // 加载数据库驱动 Class.forName(driver); // 获取数据库连接 connection = DriverManager.getConnection(url, username, password); } catch (SQLException e) { e.printStackTrace(); } return connection; } /** * 公共查询方法,用于执行SELECT语句 * * @param connection 数据库连接对象 * @param preparedStatement 执行查询的PreparedStatement对象 * @param resultSet 查询结果集 * @param sql 执行的SQL语句 * @param params SQL语句的参数值(可以为null) * @return 返回查询结果集 * @throws SQLException 如果SQL执行过程中发生错误 */ public static ResultSet executequery(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet, String sql, Object[] params) throws SQLException { if (preparedStatement == null) { // 如果PreparedStatement为null,则创建新的PreparedStatement对象 preparedStatement = connection.prepareStatement(sql); } // 设置SQL语句中的参数 for (int i = 0; params != null && i < params.length; i++) { preparedStatement.setObject(i + 1, params[i]); } // 执行查询并返回结果集 resultSet = preparedStatement.executeQuery(); return resultSet; } /** * 公共修改方法,用于执行INSERT、UPDATE或DELETE语句 * * @param connection 数据库连接对象 * @param preparedStatement 执行修改的PreparedStatement对象 * @param sql 执行的SQL语句 * @param params SQL语句的参数值(可以为null) * @return 返回受影响的行数 * @throws SQLException 如果SQL执行过程中发生错误 */ public static int executeUpdate(Connection connection, PreparedStatement preparedStatement, String sql, Object[] params) throws SQLException { if (preparedStatement == null) { // 如果PreparedStatement为null,则创建新的PreparedStatement对象 preparedStatement = connection.prepareStatement(sql); } // 设置SQL语句中的参数 for (int i = 0; params != null && i < params.length; i++) { preparedStatement.setObject(i + 1, params[i]); } // 执行更新语句,返回更新的行数 int updateRows = preparedStatement.executeUpdate(); return updateRows; } /** * 释放数据库连接相关的资源 * * @param connection 数据库连接对象 * @param preparedStatement 执行的PreparedStatement对象 * @param resultSet 查询结果集 * @return 如果资源成功关闭返回true,否则返回false * @throws SQLException 如果关闭资源时发生错误 */ public static boolean closeResource(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet) throws SQLException { boolean flag = true; // 关闭ResultSet资源 if (resultSet != null) { try { resultSet.close(); resultSet = null; } catch (SQLException e) { e.printStackTrace(); flag = false; } } // 关闭PreparedStatement资源 if (preparedStatement != null) { try { preparedStatement.close(); preparedStatement = null; } catch (SQLException e) { e.printStackTrace(); flag = false; } } // 关闭数据库连接资源 if (connection != null) { try { connection.close(); connection = null; } catch (SQLException e) { e.printStackTrace(); flag = false; } } // 返回资源关闭的状态 return flag; } }