我在声明中做了一个简单的插入,但它运行不正常。它给我的错误是
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException:您有错误 在你的SQL语法中;查看与MySQL对应的手册 服务器版本,用于在“From,To,Message”附近使用正确的语法 日期,阅读)第1行的价值(3,1,'iepa','2012-10-16 16:26:42',0)'
这是我的sql的代码。
Connection conn = Connect.getConnection();
try{
Statement stmt = conn.createStatement();
String sql = "INSERT INTO MESSAGE("
+ "From,"
+ "To,"
+ "Message,"
+ "Date,"
+ "Read) "
+ "VALUES(?,?,?,?,?)";
PreparedStatement pstat = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
pstat.setInt(1, user.getId());
pstat.setInt(2, Integer.parseInt(who.getText().toString()));
pstat.setString(3, message.toString());
pstat.setTimestamp(4, new Timestamp(new Date().getTime()));
pstat.setInt(5, 0);
pstat.executeUpdate();
如你所见,我没有做任何奇怪或困难,但我无法运行它。
答案 0 :(得分:4)
您需要使用反引号{/ 1>来reserved words in MySQL转义from
String sql = "INSERT INTO MESSAGE("
+ "`From`,"
+ "`To`,"
+ "Message,"
+ "Date,"
+ "`Read`) "
+ "VALUES(?,?,?,?,?)";
答案 1 :(得分:3)
将From
,Read
和Date
更改为不是SQL关键字的任何其他列名。
查找Reserved Words in MySQL