2014-11-23 49 views
1

我的數據庫有兩列。一列是自動增量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) 

回答

1

接口聲明

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(); 
相關問題