每个人。我收到此错误:
您的 SQL 语法有误;检查手册
对应你的MySQL服务器版本为对
在第 1 行的 '14:37:41)' 附近使用的语法
对于这段代码
public String addName() {
// TODO Auto-generated method stub
try {
java.util.Date dt = new java.util.Date();
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
String currentTime = sdf.format(dt);
String name = "RandomName";
Connection connect = DriverManager.getConnection(
"jdbc:mysql://localhost", "ericman", "ericman");
Statement stat = (Statement) connect.createStatement();
String insert = "INSERT INTO `bookcatalog`.`puch` (`name`, `time`) VALUES ('"
+ name + "', " + currentTime + ")";
stat.executeUpdate(insert);
} catch (Exception e) {
System.out.println(e);
}
return "Name Updated";
}
关于为什么会发生这种情况的任何建议,我很讨厌结构化语言,所以你知道:)
最佳答案
使用PreparedStatement
。
String insert = "INSERT INTO `bookcatalog`.`puch` (`name`, `time`) VALUES (?,?)";
PreparedStatement ps=connect.prepareStatement(insert);
ps.setString(1,name);
ps.setTimeStamp(2,TimeStamp.valueOf(currentTime));
ps.executeUpdate();
关于java - 将 Java 日期转换为 MySql DateTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8258128/