本文介绍了以下sql代码无法插入数据的原因是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库有两列.一列是自动递增id(Primary Key)列,另一列是'Sentence'列.手动输入内容,然后我就可以插入该值.但是,当我尝试插入会给出错误消息的变量值时.我尝试了不同的方法. ('?') /( ? )/(?)对我不起作用.

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.

错误消息:

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

这会显示以下错误消息:

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)

推荐答案

pstmtJ.executeUpdate(sql);→您正在发送原始SQL字符串,因为它正在调用 Statement.executeUpdate(String).

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

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

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

这篇关于以下sql代码无法插入数据的原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 11:30