When I run the code through the browser, the error mentioned in the title appears
"No value specified for parameter 4".
Below I will leave my DAO and my MySQL configuration if someone can help me, if I need more information let me know.
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)
);