Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SQL插入:错误检查_Java_Sql_Message - Fatal编程技术网

Java SQL插入:错误检查

Java SQL插入:错误检查,java,sql,message,Java,Sql,Message,我有下面的类,插入函数工作得很好,到目前为止,您输入了正确的信息。UserID是一个主键,当UserID已经存在,或者超过10个符号时,我想给出一些个人错误消息。。有人能告诉我如何检查这两种情况吗 public void createCustomer(String UserID, String userName, String userMail ) throws SQLException { PreparedStatement pstmt = null;

我有下面的类,插入函数工作得很好,到目前为止,您输入了正确的信息。UserID是一个主键,当UserID已经存在,或者超过10个符号时,我想给出一些个人错误消息。。有人能告诉我如何检查这两种情况吗

public void createCustomer(String UserID, String userName, String userMail ) throws SQLException { 

            PreparedStatement pstmt = null;
            String iSQL = "INSERT INTO TEST_USER" 
                                + "(UserID, UserName, UserMail VALUES"      
                                + "(?, ? ,?)";


            pstmt = conn.prepareStatement(iSQL);

            pstmt.setString(1, userID);
            pstmt.setString(2, userName);
            pstmt.setString(3, userMail);

            pstmt.executeUpdate();


}

非常感谢。

要检查问题,可以使用try catch,然后在catch中,您知道发生了错误

try {
    // do your sql calls here
} catch (SQLException e) { 
    // handle the sql error here (for instance printing the message)
}
您可以使用以下各项检查哪个错误:


您可以使用方法e.getErrorCode()并对这些代码进行相等性检查。

当然,您可以在检查之前检查用户ID的长度

if (UserID.length >= 10) {
  // do your error handling
}

但是我不知道是哪种错误,我如何才能过滤出哪种错误来设置正确的错误消息?请注意右括号:…+“(UserID、UserName、UserMail)值”。。。