check out if the user exists or not in the database

0

I'm trying to do this validation on my server but it does not work out when I pass a string value to the method that would be a user name, it must go through the methods and see if it exists in the database that does not let you out and if it does not exist that you can continue.

I have this in the SERVICE layer:

 public ReplyBean getexistsusername() throws Exception {
    /*
    //recuperar el username y ver si existe
    String username = ParameterCook.prepareUsername(oRequest);

    //mirar en la bd si existe username
     Connection oConnection = null;
        ConnectionInterface oDataConnectionSource = null;
    return new ReplyBean(200, JsonMessage.getJsonMsg(200, "true"));

     */
    if (this.checkpermission("getexistsusername")) {
        //recuperar el username y ver si existe
        String username = ParameterCook.prepareUsername(oRequest);

        ReplyBean oReplyBean = new ReplyBean();
        Connection oConnection = null;
        ConnectionInterface oDataConnectionSource = null;
        try {
            oDataConnectionSource = getSourceConnection();
            oConnection = oDataConnectionSource.newConnection();
            oConnection.setAutoCommit(false);
            Usuarios_propietariosDao oUserDao = new Usuarios_propietariosDao(oConnection, (UsuarioBean) oRequest.getSession().getAttribute("userBean"), null);
            Integer iResult = oUserDao.getexistsusername(username);
            if (iResult >= 1) {
                oReplyBean.setCode(200);
                oReplyBean.setJson(JsonMessage.getJsonExpression(200, "este usuario ya existe"));
            } else {
                oReplyBean.setCode(500);
                oReplyBean.setJson(JsonMessage.getJsonMsg(500, "el usuario no existe"));
            }
            oConnection.commit();
        } catch (Exception ex) {
            if (oConnection != null) {
                oConnection.rollback();
            }
            Log4j.errorLog(this.getClass().getName() + ":" + (ex.getStackTrace()[0]).getMethodName(), ex);
            throw new Exception();
        } finally {
            if (oConnection != null) {
                oConnection.close();
            }
            if (oDataConnectionSource != null) {
                oDataConnectionSource.disposeConnection();
            }
        }
        return oReplyBean;
    } else {
        return new ReplyBean(401, JsonMessage.getJsonMsg(401, "Unauthorized"));
    }
}

and in the DAO layer:

 public Integer getexistsusername(String strValor) throws Exception {
    String strResult = null;
    Statement oStatement = null;
    ResultSet oResultSet = null;
    Integer iResult = null;
    try {

        String strSQlL = "SELECT id FROM usuario WHERE username ='" + strValor + "'";
        oResultSet = oStatement.executeQuery(strSQlL);
        if (oResultSet.next()) {
            strResult = oResultSet.getString("id");
            iResult = Integer.parseInt(strResult, 1);
        } else {
            Log4j.errorLog(this.getClass().getName() + ":" + "getId error");
            throw new SQLException();
        }
    } catch (SQLException ex) {
        Log4j.errorLog(this.getClass().getName() + ":" + (ex.getStackTrace()[0]).getMethodName(), ex);
        throw new Exception();
    } finally {
        if (oResultSet != null) {
            oResultSet.close();
        }
        if (oStatement != null) {
            oStatement.close();
        }
    }
    return iResult;
}
    
asked by T P 07.06.2017 в 09:28
source

0 answers