Estoy intentando hacer esta validación en mi servidor pero no me sale cuando le paso un valor string al método que seria un nombre de usuario, debe pasar por los métodos y ver si existe en la base de datos que no te deje salir y si no existe que puedas continuar.
Tengo en la capa SERVICE esto:
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"));
}
}
y en la capa DAO:
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;
}
checkpermission
qué hace? Te recomiendo que sigas la sintáxis de java a la hora de escribir código, lo hará más fácil de leer para ti y tus compañeros, además de ser una buena práctica – Erlantz Calvo el 7 jun. 17 a las 7:37