【发布时间】:2021-06-29 17:53:16
【问题描述】:
PreparedStatement ps 按预期工作,user_type 正在为第一行更新,但是我的第二条语句 (ps1) 应该更新第二行不是。因为唯一的变化是偏移量'LIMIT 1, 1;'我只能假设这就是系统抛出 MYSQLSyntaxErrorException 的原因。我是 MySQL 新手,所以不确定它为什么会抛出异常以及如何通过尝试其他方法来实现相同的目标。
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/child_timer", "root", "");
PreparedStatement ps = conn.prepareStatement("UPDATE user_profile SET user_type=? ORDER BY user_id ASC LIMIT 1;");
ps.setString(1, sp.getUserType().name());
int x = ps.executeUpdate();
if (x > 0) {
alertDialogBuilder(AlertType.INFORMATION, "Success", null, "Changes have been successfully saved.");
}
PreparedStatement ps1 = conn.prepareStatement("UPDATE user_profile SET user_type=? ORDER BY user_id ASC LIMIT 1, 1;");
ps1.setString(1, sp.getUserType1().name());
int x1 = ps1.executeUpdate();
if (x1 > 0) {
alertDialogBuilder(AlertType.INFORMATION, "Success", null, "Changes have been successfully saved.");
} catch (Exception e1) {
System.out.println(e1);
}
【问题讨论】: