diff --git a/DbUtil.java b/DbUtil.java new file mode 100644 index 0000000..9023f4c --- /dev/null +++ b/DbUtil.java @@ -0,0 +1,139 @@ +package dao; + +import java.sql.*; + +/** + * 数据库工具类 + * 学号:2023210480 姓名:倪思羽 + */ +public class DbUtil { + private Connection conn = null; + private PreparedStatement ps = null; + private ResultSet rs = null; + + // 数据库连接配置(已改为postgres数据库,对应你新建的表) + private static final String URL = "jdbc:postgresql://localhost:5432/postgres"; + private static final String USER = "postgres"; + private static final String PASSWORD = "123456"; // 你的数据库密码 + + /** + * 获取数据库连接(非静态方法) + */ + public Connection getConnection() { + try { + if (conn == null || conn.isClosed()) { + Class.forName("org.postgresql.Driver"); + conn = DriverManager.getConnection(URL, USER, PASSWORD); + System.out.println("连接对象创建成功:" + conn); // 新增:打印连接对象,辅助排查 + } + } catch (ClassNotFoundException e) { + System.out.println("❌ 驱动加载失败:找不到PostgreSQL JDBC驱动"); + e.printStackTrace(); + } catch (SQLException e) { + System.out.println("❌ 数据库连接失败:URL/用户名/密码错误,或数据库未启动"); + e.printStackTrace(); + } catch (Exception e) { + System.out.println("❌ 未知异常导致连接失败"); + e.printStackTrace(); + } + return conn; + } + + /** + * 执行查询 + */ + public ResultSet executeQuery(String sql, Object... params) { + try { + ps = getConnection().prepareStatement(sql); + for (int i = 0; i < params.length; i++) { + ps.setObject(i + 1, params[i]); + } + rs = ps.executeQuery(); + } catch (SQLException e) { + System.out.println("❌ 执行查询失败"); + e.printStackTrace(); + } + return rs; + } + + /** + * 执行更新(插入/修改/删除) + */ + public int executeUpdate(String sql, Object... params) { + try { + ps = getConnection().prepareStatement(sql); + for (int i = 0; i < params.length; i++) { + ps.setObject(i + 1, params[i]); + } + int rows = ps.executeUpdate(); + System.out.println("✅ 执行更新成功,影响行数:" + rows); + return rows; + } catch (SQLException e) { + System.out.println("❌ 执行更新失败"); + e.printStackTrace(); + return -1; + } + } + + /** + * 关闭资源 + */ + public void closeResources() { + try { + if (rs != null) rs.close(); + if (ps != null) ps.close(); + if (conn != null) conn.close(); + System.out.println("✅ 资源关闭成功"); + } catch (SQLException e) { + System.out.println("❌ 资源关闭失败"); + e.printStackTrace(); + } + } + + /** + * 测试主方法:全量验证连接、查询、插入功能 + */ + public static void main(String[] args) { + // 1. 创建DbUtil实例(解决静态调用非静态方法的核心) + DbUtil dbUtil = new DbUtil(); + + // 2. 测试数据库连接 + System.out.println("===== 测试1:数据库连接 ====="); + Connection conn = dbUtil.getConnection(); + if (conn != null) { + System.out.println("✅ 数据库连接成功!"); + } else { + System.out.println("❌ 数据库连接失败!"); + return; // 连接失败,后续测试无需执行 + } + + // 3. 测试插入数据(往你的customer_cstatm2023210480表插测试数据) + System.out.println("\n===== 测试2:插入数据 ====="); + String insertSql = "INSERT INTO customer_cstatm2023210480 (cid, cname, cpin, ldatetime, lftimes, lstimes) VALUES (?, ?, ?, ?, ?, ?)"; + // 插入你的学号数据,避免重复插入,可改cid为2023210481 + dbUtil.executeUpdate(insertSql, "2023210481", "倪思羽测试", "123456", new Date(System.currentTimeMillis()), 0, 0); + + // 4. 测试查询数据(查询刚插入的表数据) + System.out.println("\n===== 测试3:查询数据 ====="); + String querySql = "SELECT * FROM customer_cstatm2023210480 WHERE cid = ?"; + ResultSet rs = dbUtil.executeQuery(querySql, "2023210481"); + try { + if (rs.next()) { + System.out.println("✅ 查询到数据:"); + System.out.println("账号(cid):" + rs.getString("cid")); + System.out.println("姓名(cname):" + rs.getString("cname")); + System.out.println("密码(cpin):" + rs.getString("cpin")); + System.out.println("失败次数(lftimes):" + rs.getInt("lftimes")); + } else { + System.out.println("❌ 未查询到指定数据"); + } + } catch (SQLException e) { + System.out.println("❌ 查询数据解析失败"); + e.printStackTrace(); + } + + // 5. 关闭资源 + System.out.println("\n===== 测试4:关闭资源 ====="); + dbUtil.closeResources(); + } +} \ No newline at end of file