I have an error when I try to update the record in MSSQL. My code:
PreparedStatement ps = con.prepareStatement("UPDATE tblUsers SET firstName='?', lastName = '?', phone = ?,"
+ "[group] = ?, picture = ?, address = ? Where id=?");
ps.setString(1, firstName);
ps.setString(2, lastName);
//update phone has two conditions
if (!phone.isEmpty()) {
ps.setString(3, "'" + phone + "'");
} else {
ps.setString(3, null);
}
ps.setInt(4, group);
//update img has two conditions
if (pathImg != null) {
ps.setBytes(5, MyUtils.getImgbinary(pathImg));
} else {
ps.setBytes(5, MyUtils.getImgbinary(defaultPathImg));
}
//update address has two conditions
if (!address.isEmpty()) {
ps.setNString(6, "'" + address +"'");
} else {
ps.setNString(6, null);
}
ps.setInt(7, userID);
When I tried to executeUpdate(), 'The index 6 is out of range' error throws. I don't understand why? In here, address can be null.