这是我的代码
java.sql.Timestamp date =
new java.sql.Timestamp(new java.util.Date().getTime());
java.text.SimpleDateFormat sdf =
new java.text.SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
String currentTime = sdf.format(date)
int abs=0;
Statement tgl = connect.createStatement();
ResultSet rs = tgl.executeQuery("SELECT id_absensi FROM absensi ORDER BY id_absensi DESC LIMIT 1");
while (rs.next()){
abs=rs.getInt("id_absensi");
}
String query = "UPDATE absensi SET tgl_keluar ="
+ currentTime
+ " WHERE 'id_absensi'=" + abs ;
Statement aa = connect.createStatement();
int bb = aa.executeUpdate(query);
问题是,当我尝试运行此程序时,我收到此通知
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '01:59:35 WHERE 'id_absensi'=1' at line 1
我知道这应该是一个简单的语法错误,但我不知道是哪一个?我什至尝试复制实际的 mysql 语句进行更新 附言。 Select 不是问题,只是更新。
请帮忙
最佳答案
您的 INSERT 查询中的 id_absensi
不需要单引号。您还应该使用 PreparedStatement
并将 Timestamp date
设置为查询参数,如下所示:
String query = "UPDATE absensi SET tgl_keluar=? WHERE id_absensi=?";
PreparedStatement aa = connect.prepareStatement(query);
aa.setTimestamp(1, date);
aa.setInt(2, abs);
int bb = aa.executeUpdate(query);
关于java - 我无法从 Java 更新 SQL 中的 DATETIME,但插入工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37888890/