【发布时间】:2016-12-27 19:25:54
【问题描述】:
我要将日期添加到数据库中。数据库字段的类型为 DATE,我传入一个带有日期的字符串。这是sql:
INSERT INTO timetableslot (TO_DATE('?', 'MM/DD/YYYY'), time, classID, cost)
VALUES ('12/23/2016','12:00',3,12)
我收到以下错误:
您的 SQL 语法有错误;检查手册 对应于您的 MariaDB 服务器版本,以便使用正确的语法 '('?', 'MM/DD/YYYY'), 时间, classID, cost)VALUES 附近 ('12/23/2016','12:00',3,12)' 在第 1 行
public boolean saveToDatabase() {
boolean saved = false;
Connection c = DBHelperClass.getConnection();
String template = "INSERT INTO timetableslot (TO_DATE('?', 'MM/DD/YYYY'), time, classID, cost)"
+ "VALUES (?,?,?,?)";
if (c != null) {
try {
PreparedStatement inserter = c.prepareStatement(template);
inserter.setString(1, this.getDate());
inserter.setString(2, this.getTime());
inserter.setInt(3, this.getClassID());
inserter.setInt(4, this.getCost());
System.out.println(inserter);
inserter.executeUpdate();
saved = true;
} catch (SQLException ex) {
Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
}
}
return saved;
}
【问题讨论】:
-
列名将代替
TO_DATE('?', 'MM/DD/YYYY')