登錄讀取數據正常,註冊寫入 `INSERT` 執行無異常但數據庫無數據。
String sql = "use jsgl;insert into jsgl.user values(...)";
// ...
Statement stmt = DB.createStmt(conn);
int n = DB.executeUpdate(stmt, sql);
// 1. 修正 SQL
String sql = "INSERT INTO user (name, account, pwd1, pwd2) VALUES (?, ?, ?, ?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, name);
ps.setString(2, account);
ps.setString(3, pswd1);
ps.setString(4, pswd2);
int n = ps.executeUpdate();
// 2. 確保提交
conn.commit();
// 3. 捕獲異常上拋
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("註冊失敗", e);
}
`insert into jsgl.user` → `insert into user` (配合 `use jsgl`),或去掉 `use jsgl` 用全限定名。