日期:2014-05-17  浏览次数:20677 次

如何检查用户名已经存在
我是这样做的,不知道有没有简洁的方法
Java code

public boolean isExists(String userName) {
        boolean ie = false;
        int result = 0;
        sql = "select count(*) as result from t_user where [user_name] = ?";
        try {
            super.openConn();
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, userName);
            rs = pstmt.executeQuery();
            if(rs.next()){
                result = rs.getInt("result");
            }
            if(result > 0){
                ie = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            super.closeAll();
        }
        return ie;
    }



------解决方案--------------------
直接select * from t_user where [user_name] = ?啊,就不必用两个if判断语句了
直接一个if判断就行了:
if(rs.next()){
ie = true;
}