java - 插入给问题

标签 java mysql sql sql-insert

我做了一个简单的 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/

相关文章:

java - Fortify 扫描问题 (XSS) 跨站点脚本反射 - 方法将未经验证的数据发送到网络浏览器

php - 严格标准 : only variables should be passed by reference - why does this still work and how can I correct it?

java - SQL:从executeUpdate获取自动递增列的值

mysql - 在 MySQL 中逐行使用 SUM()

MySQL 用一个查询从 2 个表中删除

java - Android:在某些情况下禁用 AlertDialogBu​​ilder 的“确定”按钮

java - 程序重载,如何解决?

java - 矩阵格式的控制台输出

php - MySQLI 在生产中发生 fatal error ,但在开发中则不然

MySQL 5.7 - 分组依据没有工作