我想在数据库中插入11个字段。显示的错误是我的12参数未定义,即ps.executeUpdate()。我该怎么办?
我的插入方法如下。
public void update(User user){
try{
Connection conn = DbUtils.getConnection();
PreparedStatement ps = conn.prepareStatement("UPDATE db.db set id = ?,username = ?,password = ?,firstname = ?, lastname = ?, fathersname =?, gender = ?, email = ?,address =? , phnno = ?, courses = ?" + "WHERE id =?");
ps.setInt(1, user.getId());
ps.setString(2, user.getName());
ps.setString(3, user.getPwd());
ps.setString(4, user.getFname());
ps.setString(5, user.getLname());
ps.setString(6, user.getFathername());
ps.setString(7, user.getAddress());
ps.setString(8, user.getEmail());
ps.setString(9, user.getGender());
ps.setString(10, user.getPhno());
ps.setString(11, user.getCradio());
ps.execute();
}catch(Exception e){
e.printStackTrace();
}
`
答案 0 :(得分:0)
函数应该有12个占位符的另一个参数。 像这样:
public void update(User user){
try{
Connection conn = DbUtils.getConnection();
PreparedStatement ps = conn.prepareStatement("UPDATE db.db set id = ?,username = ?,password = ?,firstname = ?, lastname = ?, fathersname =?, gender = ?, email = ?,address =? , phnno = ?, courses = ?" + " WHERE id =?");
ps.setInt(1, user.getId());
ps.setString(2, user.getName());
ps.setString(3, user.getPwd());
ps.setString(4, user.getFname());
ps.setString(5, user.getLname());
ps.setString(6, user.getFathername());
ps.setString(7, user.getAddress());
ps.setString(8, user.getEmail());
ps.setString(9, user.getGender());
ps.setString(10, user.getPhno());
ps.setString(11, user.getCradio());
ps.setInt(12, user.getId()); //parameter 12 added
ps.execute();
}catch(Exception e){
e.printStackTrace();
}
你真的想更新ID字段吗?