以下sql代码的原因是什么,无法插入数据?

问题描述:

我的数据库有两列。一列是自动增量id(Primary Key),另一列是'Sentence'列。手动输入一些东西,我可以插入该值。但是当我试图插入变量值,给出错误信息。我尝试了不同的方式。 ('?') /(?)/(?)没有为我工作。以下sql代码的原因是什么,无法插入数据?

  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字符串,因为那叫做Statement.executeUpdate(String)

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

+0

:d是的,这是为我工作。感谢很多。我尝试了很多。再次感谢。祝你今天愉快 :) 。 – Maduri 2014-11-23 04:22:06

接口声明

int executeUpdate(String sql) 

执行给定的SQL语句,这可能是一个INSERT,UPDATE或DELETE语句,或者不返回任何内容,如SQL DDL语句的SQL语句。 refer java doc

公共接口的PreparedStatement延伸声明

int executeUpdate() 

执行在此PreparedStatement对象的SQL语句,它必须是一个SQL数据操纵语言(DML)语句,如INSERT,UPDATE或DELETE;或者不返回任何内容的SQL语句,如DDL语句。 refer java doc

您呼叫executeUpdate(String sql)这是从Statement界面,你必须调用executeUpdate()PreparedStatement接口来解决问题。

修改代码:

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