【问题标题】:What is reason for following sql code couldn't insert data?以下sql代码无法插入数据的原因是什么?
【发布时间】:2014-11-23 03:58:40
【问题描述】:

我的数据库有两列。一列是自动递增id(Primary Key),另一列是'Sentence'。手动输入一些东西,我可以插入那个值。但是当我尝试插入给出错误消息的变量值时。我尝试了不同的方法。 ('?') /( ? )/(?) 对我没用。

          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)

【问题讨论】:

    标签: java mysql sql sql-insert


    【解决方案1】:

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

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

    【讨论】:

    • :D 是的,这对我有用。谢谢很多。我试过很多。再次感谢。祝你有美好的一天:) .
    【解决方案2】:

    接口声明

    int executeUpdate(String sql)
    

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

    公共接口 PreparedStatement 扩展语句

    int executeUpdate()
    

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

    你正在从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();
    

    【讨论】:

      猜你喜欢
      • 2015-01-27
      • 1970-01-01
      • 2022-11-04
      • 1970-01-01
      • 1970-01-01
      • 2019-05-05
      • 2019-02-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多