【问题标题】:Help needed for inserting values to a mysql table将值插入 mysql 表所需的帮助
【发布时间】:2011-08-02 01:50:26
【问题描述】:

我已经使用mysql创建了一个表:

CREATE TABLE JobCard (
    ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    JobNo Long,
    RegNo VARCHAR(20),
    Service_Type VARCHAR(20),
    Customer_Complaints VARCHAR(100)
);

在 cmd 中。

在 Eclipse 中,我使用为表准备的语句为插入值进行了编码。由于 ID 是一个 auto_increment,我没有在插入语句中包含它。

String Query =
    "INSERT INTO JobCard (JobNo, RegNo, Service_Type, Customer_Complaints)
        VALUES (?,?,?,?)";

但输出显示了我:

java.sql.SQLException: Parameter index out of range
               (5 > number of parameters, which is 4).
    at
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
    at
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
    at
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
    at
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
    at
com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3717)
    at
com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3701)
    at
com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:4552)
    at
example.Connect.DoInsertIntoDB(Connect.java:40)

谁能告诉我如何传递参数列表?请帮我解决这个错误!!

更新:

这是我的代码: 方法调用为:

System.out.println(strLine);
                    
String[] dbColumnValues = strLine.split("%");
Connect.DoInsertIntoDB(Long.parseLong(dbColumnValues[0]),dbColumnValues[1],dbColumnValues[2], dbColumnValues[3]);

方法定义:

public static void DoInsertIntoDB(Long JobNo, String RegNo, String Service_Type, String Customer_Complaints){
    
    
    String Query = "INSERT INTO JobCard (JobNo, RegNo, Service_Type, Customer_Complaints) VALUES (?,?,?,?)";
    try {
        Connection conn = toConnect();
                        
        PreparedStatement pstmt = conn.prepareStatement(Query);
        
        pstmt.setLong(2, JobNo);
        pstmt.setString(3, RegNo);
        pstmt.setString(4, Service_Type);
        pstmt.setString(5, Customer_Complaints);
        
        pstmt.executeUpdate();
        pstmt.close();
        conn.close();
        
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}

【问题讨论】:

  • 您的执行需要 4 个参数,这是正确的。你能告诉我们你准备和/或执行查询的行吗?
  • 感谢您的回复。这是我的代码:

标签: java mysql insert auto-increment


【解决方案1】:

你的尝试应该是这样的,

try {
    Connection conn = toConnect();

    PreparedStatement pstmt = conn.prepareStatement(Query);

    pstmt.setLong(1, JobNo);
    pstmt.setString(2, RegNo);
    pstmt.setString(3, Service_Type);
    pstmt.setString(3, Customer_Complaints);

    pstmt.executeUpdate();
    pstmt.close();
    conn.close();

}

这应该可以解决问题....

【讨论】:

    【解决方案2】:

    Prepare 语句参数从 1 个数字开始,根据您的代码,参数应为 1 到 4 但你以 5 结尾。 它导致参数索引超出范围

    【讨论】:

      【解决方案3】:

      设置参数时是从2开始的,必须是1。 如果您看到,最后一个参数是索引 5,而您没有 5° 参数, 因为这个java说异常“参数索引超出范围”。

      你必须从 1 开始。

      PS:对不起我的英语。

      【讨论】:

        【解决方案4】:

        需要阅读您的堆栈跟踪。在您的代码(Connect.java 的第 40 行)中,您试图将值设置为 5th ?但是您准备好的语句中只有 4 个 ?s。

        【讨论】:

        • 是的.. 好的。谢谢你的帮助。我的准备语句错了。现在它工作正常!
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-05-03
        • 1970-01-01
        • 2012-02-27
        • 1970-01-01
        • 1970-01-01
        • 2011-02-18
        • 1970-01-01
        相关资源
        最近更新 更多