0

下面是简单的登录系统。目前它允许我在表中输入一个空白的用户名和密码,即使每个索引都被指定为 NOT NULL?它不允许我输入我想要的重复项,但是如何从输入中捕获空白参数?我错过了什么?

注册小服务程序

....
LoginService loginService = new LoginService();
            connection = loginService.getConnection(connection);
            loginService.addNewUser(preparedStatement, connection, newUserId, newUserPassword, newUserFirstName, newUserLastName);
...

LoginService 方法 addNewUser

public void addNewUser(PreparedStatement ps, Connection connection, String newUserId, String newUserPassword,String newUserFirstName,String newUserLastname) throws SQLException
    {
        ps = connection.prepareStatement("INSERT INTO users (userId ,password , userFirstName, userLastName)VALUES(?,?,?,?)");
        ps.setString(1, newUserId);
        ps.setString(2, newUserPassword);
        ps.setString(3, newUserFirstName);
        ps.setString(4, newUserLastname);

        ps.executeUpdate();

    }
4

1 回答 1

2

要解决您当前的问题,您可以将其添加到 addNewUser 方法的开头(在以下行之前:ps = connection.prepareStatement("...");)

if (newUserId != null && "".equals(newUserId.trim()))
    newUserId = null;
if (newUserPassword != null && "".equals(newUserPassword.trim()))
    newUserPassword = null;

您应该在 JDBC 参数中传递真正的 NULL 值(空字符串不够好)

于 2013-05-28T15:43:17.700 回答