Error No value specified for Parameter 4

Asked

Viewed 682 times

0

When I execute the code by the browser appears the error quoted in the title

"Error No value specified for Parameter 4".

Below I will leave my DAO and my Mysql configuration if someone can help me if it is necessary more information inform me.

DAO

package net.trabalhojava.javaee.usuario;

import java.sql.Connection;

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;





public class UsuarioDAO {
    private String jdbcURL;
    private String jdbcUsername;
    private String jdbcPassword;
    private Connection jdbcConnection;

    public UsuarioDAO(String jdbcURL, String jdbcUsername, String jdbcPassword) {
        this.jdbcURL = jdbcURL;
        this.jdbcUsername = jdbcUsername;
        this.jdbcPassword = jdbcPassword;
    }

    protected void connect() throws SQLException {
        if (jdbcConnection == null || jdbcConnection.isClosed()) {
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                throw new SQLException(e);
            }
            jdbcConnection = DriverManager.getConnection(
                                        jdbcURL, jdbcUsername, jdbcPassword);
        }
    }

    protected void disconnect() throws SQLException {
        if (jdbcConnection != null && !jdbcConnection.isClosed()) {
            jdbcConnection.close();
        }
    }

    public boolean insertUsuario(Usuario usuario) throws SQLException {
        String sql = "INSERT INTO usuario (nome, endereco, cidade, estado, telefone, celular, email) VALUES (?, ?, ?, ?, ?, ?, ?)";
        connect();

        PreparedStatement statement = jdbcConnection.prepareStatement(sql);
        statement.setString(1, usuario.getNome());
        statement.setString(2, usuario.getEndereco());
        statement.setString(3, usuario.getCidade());
        statement.setString(1, usuario.getEstado());
        statement.setString(2, usuario.getTelefone());
        statement.setString(3, usuario.getCelular());
        statement.setString(7, usuario.getEmail());

        boolean rowInserted = statement.executeUpdate() > 0;
        statement.close();
        disconnect();
        return rowInserted;
    }

    public List<Usuario> listTodosUsuarios() throws SQLException {
        List<Usuario> listUsuario = new ArrayList<>();

        String sql = "SELECT * FROM usuario";

        connect();

        PreparedStatement statement = (PreparedStatement) jdbcConnection.prepareStatement(sql);
        ResultSet resultSet = statement.executeQuery(sql);

        while (resultSet.next()) {
            int id = resultSet.getInt("usuario_id");
            String nome = resultSet.getString("nome");
            String endereco = resultSet.getString("endereco");
            String cidade = resultSet.getString("cidade");
            String estado = resultSet.getString("estado");
            String telefone = resultSet.getString("telefone");
            String celular = resultSet.getString("celular");
            String email = resultSet.getString("email");

            Usuario usuario = new Usuario(id, nome, endereco, cidade, estado, telefone, celular, email);
            listUsuario.add(usuario);
        }

        resultSet.close();
        statement.close();

        disconnect();

        return listUsuario;
        }



    public boolean deleteUsuario(Usuario usuario) throws SQLException {
        String sql = "DELETE FROM usuario where usuario_id = ?";

        connect();

        PreparedStatement statement = jdbcConnection.prepareStatement(sql);
        statement.setInt(1, usuario.getId());

        boolean rowDeleted = statement.executeUpdate() > 0;
        statement.close();
        disconnect();
        return rowDeleted;      
    }




    public boolean updateUsuario(Usuario usuario) throws SQLException {
        String sql = "UPDATE usuario SET nome = ?, endereco = ?, cidade = ?, estado = ?, telefone = ?, celular = ?, email = ?";
        sql += " WHERE usuario_id = ?";
        connect();

        PreparedStatement statement = jdbcConnection.prepareStatement(sql);
        statement.setString(1, usuario.getNome());
        statement.setString(2, usuario.getEndereco());
        statement.setString(3, usuario.getCidade());
        statement.setString(4, usuario.getEstado());
        statement.setString(5, usuario.getTelefone());
        statement.setString(6, usuario.getCelular());
        statement.setString(6, usuario.getEmail());
        statement.setInt(4, usuario.getId());

        boolean rowUpdated = statement.executeUpdate() > 0;
        statement.close();
        disconnect();
        return rowUpdated;      
    }

    public Usuario getUsuario(int id) throws SQLException {
        Usuario usuario = null;
        String sql = "SELECT * FROM usuario WHERE usuario_id = ?";

        connect();

        PreparedStatement statement = jdbcConnection.prepareStatement(sql);
        statement.setInt(1, id);

        ResultSet resultSet = statement.executeQuery();

        if (resultSet.next()) {         
            String nome = resultSet.getString("nome");
            String endereco = resultSet.getString("endereco");
            String cidade = resultSet.getString("cidade");
            String estado = resultSet.getString("estado");
            String telefone = resultSet.getString("telefone");
            String celular = resultSet.getString("celular");
            String email = resultSet.getString("email");

            usuario = new Usuario(id, nome, endereco, cidade, estado, telefone, celular, email);
        }

        resultSet.close();
        statement.close();

        return usuario;
    }


}

Mysql

use usuariocad;
create table usuario (


    usuario_id int(11) NOT NULL AUTO_INCREMENT,
    nome varchar(45) NOT NULL,
    endereco varchar(45) NOT NULL,
    cidade varchar(45) NOT NULL,
    estado varchar(45) NOT NULL,
    telefone varchar(45) NOT NULL,
    celular varchar(45) NOT NULL, 
    email varchar(45) NOT NULL, 
    PRIMARY KEY (usuario_id)


);

1 answer

2

In his method insertUsuario you are waiting 7 parameters in the query, but you are not sending them in your PreparedStatement: Your code

PreparedStatement statement = jdbcConnection.prepareStatement(sql);
statement.setString(1, usuario.getNome());
statement.setString(2, usuario.getEndereco());
statement.setString(3, usuario.getCidade());
statement.setString(1, usuario.getEstado());
statement.setString(2, usuario.getTelefone());
statement.setString(3, usuario.getCelular());
statement.setString(7, usuario.getEmail());

Then you are only specifying values for parameters 1, 2, 3 and 7. Should be:

PreparedStatement statement = jdbcConnection.prepareStatement(sql);
statement.setString(1, usuario.getNome());
statement.setString(2, usuario.getEndereco());
statement.setString(3, usuario.getCidade());
statement.setString(4, usuario.getEstado());
statement.setString(5, usuario.getTelefone());
statement.setString(6, usuario.getCelular());
statement.setString(7, usuario.getEmail());

Also review the method that updates user, it seems that the parameters are also wrong.

  • Thank you very much, it’s funny how I’ve been analyzing the code for so long and I haven’t noticed such obvious things.

  • Normal, try to read the error and understand it. 99% of the time they tell us everything we need to get to the problem. If you decided to put the answer as accepted!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.