【发布时间】: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