I'm building a Java system with MySQL database connection, but I stopped at some point because I can not find the error. My update method happens an error in JUnit, but I can not figure out what it is.
Follows parts of the code:
package com.sistemacliente.DAO;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import com.sistemacliente.domain.Balada;
import com.sistemacliente.domain.Cliente;
import com.sistemacliente.factory.ConexaoFactory;
public class ClienteDAO {
public void salvar(Cliente c) throws SQLException {
StringBuilder sql = new StringBuilder();
sql.append("INSERT INTO cliente");
sql.append("(nome,sobrenome,telefone,email,data,valor,baladas_codigo)");
sql.append("VALUES (?,?,?,?,?,?,?)");
Connection conexao = ConexaoFactory.conectar();
PreparedStatement comando = conexao.prepareStatement(sql.toString());
comando.setString(1, c.getNome());
comando.setString(2, c.getSobrenome());
comando.setInt(3, c.getTelefone());
comando.setString(4, c.getEmail());
comando.setDate(5, c.getData());
comando.setDouble(6, c.getValor());
comando.setInt(7, c.getBalada().getCodigo());
comando.executeUpdate();
}
public ArrayList<Cliente> listar() throws SQLException {
StringBuilder sql = new StringBuilder();
sql.append(
"SELECT c.codigo, c.nome, c.sobrenome, c.telefone, c.email, c.data, c.valor, b.codigo, b.descricao ");
sql.append("FROM cliente c ");
sql.append("INNER JOIN balada b ON b.codigo = c.baladas_codigo");
Connection conexao = ConexaoFactory.conectar();
PreparedStatement comando = conexao.prepareStatement(sql.toString());
ResultSet resultado = comando.executeQuery();
ArrayList<Cliente> lista = new ArrayList<Cliente>();
while (resultado.next()) {
Balada b = new Balada();
b.setCodigo(resultado.getInt("b.codigo"));
b.setDescricao(resultado.getString("b.descricao"));
Cliente c = new Cliente();
c.setCodigo(resultado.getInt("c.codigo"));
c.setNome(resultado.getString("c.nome"));
c.setSobrenome(resultado.getString("c.sobrenome"));
c.setTelefone(resultado.getInt("c.telefone"));
c.setEmail(resultado.getString("c.email"));
c.setData(resultado.getDate("c.data"));
c.setValor(resultado.getDouble("c.valor"));
c.setBalada(b);
lista.add(c);
}
return lista;
}
public void excluir(Cliente c) throws SQLException {
StringBuilder sql = new StringBuilder();
sql.append("DELETE FROM cliente ");
sql.append("WHERE codigo = ?");
Connection conexao = ConexaoFactory.conectar();
PreparedStatement comando = conexao.prepareStatement(sql.toString());
comando.setInt(1, c.getCodigo());
comando.executeUpdate();
}
public void atualizar(Cliente c) throws SQLException {
StringBuilder sql = new StringBuilder();
sql.append("UPDATE cliente");
sql.append("SET codigo = ? , nome = ? , sobrenome = ? , telefone = ? ,"
+ " email = ? , data = ? , valor = ? , baladas_codigo = ? ");
sql.append("WHERE codigo = ? ");
Connection conexao = ConexaoFactory.conectar();
PreparedStatement comando = conexao.prepareStatement(sql.toString());
comando.setString(1, c.getNome());
comando.setString(2, c.getSobrenome());
comando.setInt(3, c.getTelefone());
comando.setString(4, c.getEmail());
comando.setDate(5, c.getData());
comando.setDouble(6, c.getValor());
comando.setInt(7, c.getCodigo());
comando.executeUpdate();
}
}
And here is the part of JUnit:
@Test
public void atualizar() throws SQLException {
Cliente c = new Cliente();
c.setCodigo(7);
c.setNome("Koppa");
c.setSobrenome("Troppa");
c.setTelefone(11111);
c.setEmail("[email protected]");
c.setData(new Date(2016/07/10));
c.setValor(5.00);
Balada b = new Balada();
b.setCodigo(14);
c.setBalada(b);
ClienteDAO dao = new ClienteDAO();
dao.atualizar(c);
}
}
Here is the error generated:
java.sql.SQLException: No value specified for parameter 8
Error Screenshot: