我做了一个简单的 insert into 语句,但效果不佳。它给我的错误是
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'From, To, Message, Date, Read) VALUES (3,1,'iepa','2012-10-16 16:26:42',0)' at line 1
这是我的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();
如你所见,我并没有做任何奇怪或困难的事情,但我无法运行它。
最佳答案
你需要转义reserved words in MySQL像带有反引号的 from
String sql = "INSERT INTO MESSAGE("
+ "`From`,"
+ "`To`,"
+ "Message,"
+ "Date,"
+ "`Read`) "
+ "VALUES(?,?,?,?,?)";
关于java - 插入给问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12919500/