Java 以下sql代码无法';不插入数据?

Java 以下sql代码无法';不插入数据?,java,mysql,sql,sql-insert,Java,Mysql,Sql,Sql Insert,我的数据库有两列。一列是自动递增的id(主键),另一列是“句子”列。手动键入一些内容,我可以插入该值。但当我尝试插入变量值时,它会给出错误消息。我试过不同的方法(“?”)/(?)/(?)对我来说没有任何用处 int s1=9; String s2="Kasuni"; String sql = "INSERT INTO sentences (Sentence) VALUES ( ? )"; PreparedStatem

我的数据库有两列。一列是自动递增的
id(主键)
,另一列是
“句子”
列。手动键入一些内容,我可以插入该值。但当我尝试插入变量值时,它会给出错误消息。我试过不同的方法<代码>(“?”)/(?)/(?)对我来说没有任何用处

          int s1=9;
          String s2="Kasuni";
          String sql = "INSERT INTO sentences (Sentence) VALUES ( ? )";
          PreparedStatement pstmtJ = conn.prepareStatement(sql);
          //pstmtJ.setInt(1, s1);
          pstmtJ.setString(1,s2);
          pstmtJ.executeUpdate(sql);
第一个值我没有插入,因为它是自动增量值。我只是在上面的代码中添加了注释

错误消息:

Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.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 '? )' at line 1
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
    at com.mysql.jdbc.Util.getInstance(Util.java:381)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1030)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3558)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3490)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2109)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2642)
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1647)
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1566)
    at TestJsoup.main(TestJsoup.java:66)
当我尝试以下代码时:

          int s1=9;
          String s2="Kasuni";
          String sql = "INSERT INTO sentences (Sentence) VALUES ('?')";
          PreparedStatement pstmtJ = conn.prepareStatement(sql);
          //pstmtJ.setInt(1, s1);
          pstmtJ.setString(1,s2);
          pstmtJ.executeUpdate(sql);
这将给出以下错误消息:

Exception in thread "main" java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
    at com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3646)
    at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3630)
    at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:4481)
    at TestJsoup.main(TestJsoup.java:65)
pstmtJ.executeUpdate(sql)→ 您正在发送原始SQL字符串,因为它正在调用


使用
pstmtJ.executeUpdate()改为()以执行准备好的语句。

接口语句

int executeUpdate(String sql)
int executeUpdate()
执行给定的SQL语句,该语句可以是INSERT、UPDATE或DELETE语句,也可以是不返回任何内容的SQL语句,例如SQL DDL语句

公共接口PreparedStatement扩展语句

int executeUpdate(String sql)
int executeUpdate()
执行此
PreparedStatement
对象中的SQL语句,该语句必须是SQL数据操作语言(DML)语句,例如INSERT、UPDATE或DELETE;或不返回任何内容的SQL语句,例如DDL语句

您正在从
Statement
接口调用
executeUpdate(String sql)
,您必须从
PreparedStatement
接口调用
executeUpdate()

修改代码:

int s1=9;
String s2="Kasuni";
String sql = "INSERT INTO sentences (Sentence) VALUES ( ? )";
PreparedStatement pstmtJ = conn.prepareStatement(sql);
//pstmtJ.setInt(1, s1);
pstmtJ.setString(1,s2);
pstmtJ.executeUpdate();

:是的,这对我来说很有用。谢谢。我试了很多。又是唐克斯。祝你今天愉快:)。