小编典典

INSERT INTO语句中的Java SQL语法错误

sql

我正在尝试将下面的数据插入具有ID [autoNumber],type [text],to [text],from [text],message
[text]和attachmentLoc [text]列的表中

因此,生成了自动编号,我将其插入并提供了列名。运行控制台时,将打印出:

java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Syntax error in INSERT INTO statement.
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6957)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7114)
at sun.jdbc.odbc.JdbcOdbc.SQLExecute(JdbcOdbc.java:3149)
at sun.jdbc.odbc.JdbcOdbcPreparedStatement.execute(JdbcOdbcPreparedStatement.java:216)
at sun.jdbc.odbc.JdbcOdbcPreparedStatement.executeUpdate(JdbcOdbcPreparedStatement.java:138)
at MultiEchoServer.saveMessage(MultiEchoServer.java:147)
at ClientHandler.run(MultiEchoServer.java:409)

带来错误的代码是:

try
    {
        String sql = "INSERT INTO Messages (type, to, from, message, attachmentLoc) VALUES(?,?,?,?,?)";     
        PreparedStatement stmt = database.prepareStatement(sql);
        stmt.setString(1, message.getType());
        stmt.setString(2, message.getTo());
        stmt.setString(3, message.getFrom());
        stmt.setString(4, message.getMessage());
        stmt.setString(5, attachmentLoc);
        stmt.executeUpdate();
    }
    catch (SQLException e)
    {
        System.out.println("Could not perform statement");
        e.printStackTrace();
    }

谢谢你的帮助


阅读 152

收藏
2021-04-22

共1个答案

小编典典

From是保留字,在将其更改为messageFrom之后,它允许插入数据库。

2021-04-22