Estou tendo problemas no método Editar usuário outros métodos como criar usuário deletar e listar estão funcionando normalmente segue abaixo meu código do método Editar obs:(estou usando sqlserver).
@Override
public void editar(Usuario usuario) {
String SQL = "UPDATE cadastrouser SET NOME ='?', USUARIO = '?', EMAIL = '?', SENHA = '?', CONFIRMESENHA = '?' WHERE ID = ?";
try {
PreparedStatement ps = connection.prepareStatement(SQL);
ps.setString(1, usuario.getNome());
ps.setString(2, usuario.getUsuario());
ps.setString(3, usuario.getEmail());
ps.setString(4, usuario.getSenha());
ps.setString(5, usuario.getConfirmeSenha());
ps.setInt(6, usuario.getId());
ResultSet rs = ps.executeQuery();
ps.executeUpdate();
} catch (SQLException ex) {
Logger.getLogger(SqlUsuarioDAO.class.getName()).log(Level.SEVERE, null, ex);
throw new RuntimeException("Erro ao Editar usuario", ex);
}
}
Classe onde estou testando o método
public class teste {
public static void main(String[] args) {
UsuarioDAO user = FactoryDAO.createUsuarioDAO();
Usuario usuario = new Usuario();
usuario.setNome("s");
usuario.setUsuario("s");
usuario.setEmail("s");
usuario.setSenha("123");
usuario.setConfirmeSenha("123");
usuario.setId(1002);
user.editar(usuario);
}
}
erro que estou recebendo.
run:
out 23, 2017 12:59:22 PM br.com.dxgames.DAO.SqlUsuarioDAO editar
GRAVE: null
com.microsoft.sqlserver.jdbc.SQLServerException: O índice 2 está fora do intervalo.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:191)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setterGetParam(SQLServerPreparedStatement.java:933)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setValue(SQLServerPreparedStatement.java:948)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setString(SQLServerPreparedStatement.java:1578)
at br.com.dxgames.DAO.SqlUsuarioDAO.editar(SqlUsuarioDAO.java:74)
at br.com.dxgames.DAO.teste.main(teste.java:30)
Exception in thread "main" java.lang.RuntimeException: Erro ao Editar usuario
at br.com.dxgames.DAO.SqlUsuarioDAO.editar(SqlUsuarioDAO.java:86)
at br.com.dxgames.DAO.teste.main(teste.java:30)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: O índice 2 está fora do intervalo.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:191)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setterGetParam(SQLServerPreparedStatement.java:933)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setValue(SQLServerPreparedStatement.java:948)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setString(SQLServerPreparedStatement.java:1578)
at br.com.dxgames.DAO.SqlUsuarioDAO.editar(SqlUsuarioDAO.java:74)
... 1 more
C:\Users\carlo\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
FALHA NA CONSTRUÇÃO (tempo total: 1 segundo)
já tinha testado isso e apresenta erro.
out 23, 2017 1:14:41 PM br.com.dxgames.DAO.SqlUsuarioDAO editar
GRAVE: null
com.microsoft.sqlserver.jdbc.SQLServerException: A instrução não retornou um conjunto de resultados.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:191)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:444)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:385)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7505)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2445)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:191)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:166)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeQuery(SQLServerPreparedStatement.java:297)
at br.com.dxgames.DAO.SqlUsuarioDAO.editar(SqlUsuarioDAO.java:80)
at br.com.dxgames.DAO.teste.main(teste.java:30)
Exception in thread "main" java.lang.RuntimeException: Erro ao Editar usuario
at br.com.dxgames.DAO.SqlUsuarioDAO.editar(SqlUsuarioDAO.java:86)
at br.com.dxgames.DAO.teste.main(teste.java:30)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: A instrução não retornou um conjunto de resultados.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:191)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:444)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:385)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7505)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2445)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:191)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:166)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeQuery(SQLServerPreparedStatement.java:297)
at br.com.dxgames.DAO.SqlUsuarioDAO.editar(SqlUsuarioDAO.java:80)
... 1 more
C:\Users\carlo\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
FALHA NA CONSTRUÇÃO (tempo total: 2 segundos)