I have the following code:
public void updateAccount(String username, String name, String address, String aboutMe,
String id) {
String sql = "update Account set username = ?, \n"
+ " [Full_Name] = ?,\n"
+ " [Address] = ?,\n"
+ " [about_me] = ?\n"
+ " where id = ?";
try {
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1, username);
ps.setString(2, name);
ps.setString(3, address);
ps.setString(4, aboutMe);
ps.setString(5, id);
ps.executeUpdate();
} catch (Exception ex) {
Logger.getLogger(AccountDao.class.getName()).log(Level.SEVERE, null, ex);
}
}
But when I change something like username, fullname in my web, they are still not updated and show the following error:
Severe: com.microsoft.sqlserver.jdbc.SQLServerException: Cannot insert the value NULL into column 'username', table 'FERA_ONL_LEARNING.dbo.Account'; column does not allow nulls. UPDATE fails. at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:217) at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1655) at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:440) at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:385) at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7505) at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2445) at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:191) at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:166) at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(SQLServerPreparedStatement.java:328) at dao.AccountDao.updateAccount(AccountDao.java:142) at controller.UserProfileController.doPost(UserProfileController.java:91)
I'm sure I changed their values and it can't be NULL.
How to fix this error?
1条答案
按热度按时间ojsjcaue1#
您是否尝试过打印动态查询的结果?您可以在ps.executeUpdate()之前添加一个system.out.println(sql),以验证是否正确地为变量赋值。
我根据sql的连接方式估计它没有正确地获取位置,默认情况下setString返回NULL。