本文介绍了INSERT INTO语句中的Java SQL语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将下面的数据插入到具有列ID [autoNumber],类型[text],[text],[text],message [text]和attachmentLoc [text]

因此生成自动编号我插入它们提供列名。当它运行时,控制台打印出来:

  java.sql.SQLException:[Microsoft] [ODBC Microsoft Access Driver]语法错误INSERT INTO声明。 
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)$ EB $ b在MultiEchoServer.saveMessage(MultiEchoServer.java:147)
在ClientHandler.run(MultiEchoServer.java:409)

带来错误的代码是:

 试试
{
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(无法执行语句);
e.printStackTrace();
}

感谢您的帮助

解决方案

From是一个保留字,在此之后被更改为messageFrom它允许插入数据库。


I am trying to insert the data below into a table that has the columns ID[autoNumber], type[text], to[text], from[text], message[text] and attachmentLoc[text]

So the autonumber is generated I insert them supplying the column names. When it runs the console prints out:

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)

The code that is bringing the error is :

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();
    }

Thanks for your help

解决方案

From is a reserved word, after this was changed to messageFrom it allowed the insertion to the database.

这篇关于INSERT INTO语句中的Java SQL语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 18:14