我试图在我的MySQL数据库中插入一条记录,该记录应插入表medication_profile
中,但我收到的错误是:
线程“main”中的异常 com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 您的SQL语法有错误;检查手册 对应于您的MySQL服务器版本,以便在附近使用正确的语法 'drug_type,diabetic_id,times_a_day,dose)VALUES('injections',2,1,'18')' 在第1行
我在java中用来插入此记录的代码如下:
PreparedStatement addMedicationStm = con.prepareStatement("INSERT INTO medication_profile"
+ ", medication_type, diabetic_id, times_a_day, dose) "
+ "VALUES (?,?,?,?))", Statement.RETURN_GENERATED_KEYS);
addMedicationStm.setString(1, medication.getMedicationType());
addMedicationStm.setInt(2, medication.getDiabeticID());
addMedicationStm.setInt(3, medication.getTimesPerDay());
addMedicationStm.setString(4, medication.getDose());
addMedicationStm.executeUpdate();
ResultSet rs = addMedicationStm.getGeneratedKeys();
if (rs != null && rs.next()) {
medication.setDiabeticID(rs.getInt(1));
addMedicationStm.close();
con.close();
}
我找不到错误的位置,因为我看到插入语法正确
有什么建议吗?
提前致谢
答案 0 :(得分:4)
您的字符串查询将连接到:
INSERT INTO medication_profile, medication_type, diabetic_id, times_a_day, dose) VALUES (?,?,?,?)
应该是
INSERT INTO medication_profile (medication_type, diabetic_id, times_a_day, dose) VALUES (?,?,?,?)
答案 1 :(得分:3)
删除表名后的逗号并添加(
PreparedStatement addMedicationStm = con.prepareStatement("INSERT INTO medication_profile"
+ "(medication_type, diabetic_id, times_a_day, dose) "
答案 2 :(得分:2)
你的sql语法错在这里:
PreparedStatement addMedicationStm =
con.prepareStatement
("INSERT INTO medication_profile"
+ "(medication_type, diabetic_id, times_a_day, dose) "
+ "VALUES (?,?,?,?)";
语法应该是这样的:
PreparedStatement addMedicationStm = con.prepareStatement("INSERT INTO medication_profile"
+ ", medication_type, diabetic_id, times_a_day, dose) "
+ "VALUES (?,?,?,?))", Statement.RETURN_GENERATED_KEYS);
答案 3 :(得分:1)
您放错了括号和逗号:
float: left;