[Resolvido] JAVA WEB erro ao tentar Editar usuário do bd

7 respostas Resolvido
javawebsqlserver
R

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)

7 Respostas

E

Amigo

Tire as aspas simples e veja se funciona

String SQL = “UPDATE cadastrouser SET NOME =?, USUARIO = ?, EMAIL = ?, SENHA = ?, CONFIRMESENHA = ? WHERE ID = ?”;

R
 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)
E

Amigo[

Remova

ResultSet rs = ps.executeQuery();

Apenas

ps.executeUpdate();

R

continua apresentando o mesmo erro :confused:

E
Solucao aceita

Tente algo assim:

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());

            ps.execute();
            //sempre feche sua connection 
            connection.close();

        } catch (SQLException ex) {
            Logger.getLogger(SqlUsuarioDAO.class.getName()).log(Level.SEVERE, null, ex);
            throw new RuntimeException("Erro ao Editar usuario", ex);

        }
    }

Ou com

ps.executeUpdate();

R

obg cara agora deu certo :slight_smile:

E

kkkk
Show

Adicione [RESOLVIDO] ao título

Valeu

Criado 23 de outubro de 2017
Ultima resposta 23 de out. de 2017
Respostas 7
Participantes 2