将值插入 mysql 表所需的帮助

发布于 2024-10-31 11:26:57 字数 2265 浏览 5 评论 0原文

我已经使用 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 是自动增量,因此我没有将其包含在插入语句中。

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

I have created a table using 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)
);

in cmd.

From Eclipse, i coded for inserting the values using prepared Statement for the table. Since ID is a auto_increment, i didn't include it in the insert statement.

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

But the output shows me :

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)

Can anyone please tell me how to pass the parameter list? Please help me resolve this error!!

Update:

Here is my code:
The method call is:

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

The method definition:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

只想待在家 2024-11-07 11:26:57

需要读取您的堆栈跟踪。在您的代码(Connect.java 的第 40 行)中,您试图将一个值设置为第 5 个?但你准备好的声明中只有 4 个 ? 。

Need to read your stack trace. In your code (on line 40 of Connect.java) you're attempting to set a value into the 5th ? but there are only 4 ?s in your prepared statement.

灼疼热情 2024-11-07 11:26:57

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

你必须从 1 开始。

PS:抱歉我的英语不好。

When you set the parameters, you are starting with 2, and it must be 1.
If you see, the last parameter is the index 5, and you don't have a 5° parameter,
Because of this java say the exception "Parameter index out of range".

You must start in 1.

PS: Sorry for my english.

勿挽旧人 2024-11-07 11:26:57

准备语句参数从1个数字开始,根据您的代码参数应该是1到4
但你最终得到了 5。
导致参数索引超出范围

Prepare statement parameter begin from 1 number, based on your code the parameter should be 1 to 4
but you ended with 5.
it cause parameter index out of range

白首有我共你 2024-11-07 11:26:57

你的尝试应该是这样的,

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

}

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

Your try should look like this,

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

}

and that should solve the problem....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文