What is reason for following sql code couldn't

2019-06-02 19:29发布

My database have two column. One column is auto increment id(Primary Key) and other is 'Sentence' column. Manually type something and I can insert that value. But when I'm trying insert variable value that gives error message. I tried different ways. ('?') /( ? )/(?) Not work anything for me.

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

1st value I didn't insert because of that is auto increment value. I just comment and that shows in my above code.

Error message:

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)

When I'm tring following code:

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

That gives following error messages:

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)

2条回答
做个烂人
2楼-- · 2019-06-02 19:47

pstmtJ.executeUpdate(sql); → You're sending the original SQL string, since that is calling Statement.executeUpdate(String).

Use pstmtJ.executeUpdate(); instead (PreparedStatement.executeUpdate()) to execute the prepared statement.

查看更多
相关推荐>>
3楼-- · 2019-06-02 19:58

Interface Statement

int executeUpdate(String sql)

Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement or an SQL statement that returns nothing, such as an SQL DDL statement. refer java doc

public interface PreparedStatement extends Statement

int executeUpdate()

Executes the SQL statement in this PreparedStatement object, which must be an SQL Data Manipulation Language (DML) statement, such as INSERT, UPDATE or DELETE; or an SQL statement that returns nothing, such as a DDL statement.refer java doc

you are calling executeUpdate(String sql) which is from Statement interface, you have to call executeUpdate() from PreparedStatement interface to resolve issue.

Modified Code :

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();
查看更多
登录 后发表回答