验证Java中的文本字段不起作用?

时间:2018-02-28 14:42:43

标签: java sql database validation netbeans

我正在创建一个存储客户信息的SQL,并希望所有客户都拥有唯一的联系号码,基本上作为自己的标识符。但是,即使SQL中已存在该数字,我对SQL的验证似乎也在运行,并且不确定出现了什么问题。

public static boolean uniqueValidation(String contactNum){
    Connection c = null;                                                                          
    Statement stmt = null;
    boolean result = true;

    try {
        Class.forName("org.sqlite.JDBC");
        c = DriverManager.getConnection("jdbc:sqlite:custInfo.db"); //this is telling the code where to retrieve the data from that it is looking for.

        stmt = c.createStatement();
        String sql = "SELECT rowid AS isFound FROM CUSTINFO WHERE CONTACTNUMBER LIKE "+contactNum+""; //collecting a rowid from custinfo, if the number is found anywhere
        ResultSet rs = stmt.executeQuery(sql);
        String compareString = rs.getString("isFound"); //setting the value from the resultset of rowid to comparestring

        if(!compareString.isEmpty()){ //if there is a value return false
            result = false;
        }else{
            result = true; //else return true as there is no value
        }

        stmt.close();
        c.close();
    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
    }
    return result;
}

0 个答案:

没有答案