0
值java.sql.SQLException:参数指标超出范围(2>参数的数量,这是1)为什么此代码显示参数索引超出范围异常?
我已经检查了所有的答案本主题中却找不到解决方案为我的问题。无论我做了什么改变,都会显示异常情况。 我的代码段:
public void updatetData() {
System.out.println("Enter the id of the record you want to update :");
int updateID = sc.nextInt();
if(checkDuplicateId(updateID)){
sql = "UPDATE student SET name = '?', address = '?', email_address = '?', phone_no = '?' WHERE id = ?" ;
try(Connection con = dbConn.getConnection();
PreparedStatement stmtUpdate = con.prepareStatement(sql);) {
System.out.println("\nEnter the name to update : ");
String name = sc.next();
System.out.println("\nEnter the address to update : ");
String address = sc.next();
System.out.println("\nEnter the email address to update : ");
String email_address = sc.next();
System.out.println("\nEnter the phone no to update : ");
String phone_no = sc.next();
stmtUpdate.setString(1, name);
stmtUpdate.setString(2, address);
stmtUpdate.setString(3, email_address);
stmtUpdate.setString(4, phone_no);
stmtUpdate.setInt(5, updateID);
stmtUpdate.execute();
System.out.println("Updation completed");
} catch (SQLException ex) {
System.out.println("Exception in updateData");
ex.printStackTrace();
}
} else{
System.out.println("ID doesn't exist!!");
}
}
**为checkDuplicateId方法的代码如下:**
private boolean checkDuplicateId(int id){
boolean checkDup = false;
String sql1 = "SELECT * FROM student WHERE id = ?";
try(Connection con = dbConn.getConnection();
PreparedStatement stmt = con.prepareStatement(sql1);) {
stmt.setInt(1, id);
try(ResultSet rs = stmt.executeQuery();){
if(rs.next())
return !checkDup;
else
return checkDup;
}
} catch (SQLException ex) {
System.out.println("Exception occured in checkduplicate method");
ex.printStackTrace();
return false;
} finally{
}
删除围绕'?'占位符的单引号。 – Berger