Boa noite
Alguem consegue me ajudar a encontrar o erro pois não consigo encontrar faço uma consultar no banco de dados e esta me retornando os dados do estado null .
[b]
run:
1
São Paulo
Estados{nome=null, sigla=null}
2
Belem
Estados{nome=null, sigla=null}
3
Boa Vista
Estados{nome=null, sigla=null}
4
Manaus
Estados{nome=null, sigla=null}
CONSTRUÍDO COM SUCESSO (tempo total: 0 segundos)
[/b]
Model Bean Estado
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package br.emp.estoque.model.bean;
/**
*
* @author ferratone
*/
public class Estados {
private Integer id;
private String nome;
private String sigla;
public Estados() {
}
public Estados(Integer id) {
this.id = id;
}
public Estados(Integer id, String nome, String sigla) {
this.id = id;
this.nome = nome;
this.sigla = sigla;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getSigla() {
return sigla;
}
public void setSigla(String sigla) {
this.sigla = sigla;
}
@Override
public String toString() {
return "Estados{" + "nome=" + nome + ", sigla=" + sigla + '}';
}
}
Model Bean Cidade
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package br.emp.estoque.model.bean;
/**
*
* @author ferratone
*/
public class Cidade {
private Integer id;
private String nome;
private Estados estado;
public Cidade() {
}
public Cidade(Integer id) {
this.id = id;
}
public Cidade(Integer id, String nome, Estados estado) {
this.id = id;
this.nome = nome;
this.estado = estado;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public Estados getEstado() {
return estado;
}
public void setEstado(Estados estado) {
this.estado = estado;
}
}
DAO CidadeDAO
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package br.emp.estoque.model.DAO;
import br.emp.estoque.model.bean.Cidade;
import br.emp.estoque.model.bean.Estados;
import br.emp.estoque.model.connection.ConnectionFactory;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author ferratone
*/
public class CidadeDAO implements ICidadeDAO {
private Connection connection;
private PreparedStatement preparedStatement;
private ResultSet resultSet;
private String sql;
@Override
public String cadastrar(Cidade cidade) {
try {
sql = "insert into cidade (tx_nome, id_estado) values (?, ?)";
connection = ConnectionFactory.getConnection();
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, cidade.getNome());
preparedStatement.setInt(2, cidade.getEstado().getId());
preparedStatement.execute();
return "Cadastrado com sucesso.";
} catch (SQLException e) {
return "Erro: " + e.getMessage();
} finally {
ConnectionFactory.closeConnection(connection, preparedStatement);
}
}
@Override
public String alterar(Cidade cidade) {
try {
sql = "update cidade set tx_nome=?, id_estado=? where id_cidade= ?";
connection = ConnectionFactory.getConnection();
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, cidade.getNome());
preparedStatement.setInt(2, cidade.getEstado().getId());
preparedStatement.setInt(3, cidade.getId());
preparedStatement.execute();
return "Alterado com sucesso.";
} catch (SQLException e) {
return "Erro: " + e.getMessage();
} finally {
ConnectionFactory.closeConnection(connection, preparedStatement);
}
}
@Override
public String excluir(Integer id) {
try {
sql = " delete from cidade where id_cidade= ?";
connection = ConnectionFactory.getConnection();
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, id);
preparedStatement.execute();
return "Excluído com sucesso.";
} catch (SQLException e) {
return "Erro: " + e.getMessage();
} finally {
ConnectionFactory.closeConnection(connection, preparedStatement);
}
}
@Override
public List<Cidade> listarTodos() {
List<Cidade> cidadesList = new ArrayList<>();
try {
sql = "select * from cidade inner join estado on cidade.id_estado = estado.id_estado";
//sql = "select * from cidade";
connection = ConnectionFactory.getConnection();
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
Cidade cidade = new Cidade();
cidade.setId(resultSet.getInt(1));
cidade.setEstado(new Estados(resultSet.getInt(2)));
cidade.setNome(resultSet.getString(3));
cidadesList.add(cidade);
}
} catch (SQLException e) {
e.printStackTrace();
}
return cidadesList;
}
@Override
public List<Cidade> listarPorNome(String nome) {
List<Cidade> cidadesList = new ArrayList<>();
try {
sql = "select * from cidade where tx_nome ilike concat('%', ? ,'%')";
connection = ConnectionFactory.getConnection();
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, nome);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
Cidade cidade = new Cidade();
cidade.setId(resultSet.getInt(1));
cidade.setEstado(new Estados(resultSet.getInt(2)));
cidade.setNome(resultSet.getString(3));
cidadesList.add(cidade);
}
} catch (SQLException e) {
e.printStackTrace();
}
return cidadesList;
}
@Override
public Cidade consultarPorId(Integer id) {
Cidade cidade = null;
try {
sql = "select c.id_cidade, c.tx_nome, e.id_estado, e.tx_nome, e.tx_sigla from cidade c "
+ "inner join estado e on c.id_estado = e.id_estado "
+ "where c.id_cidade = ?";
connection = ConnectionFactory.getConnection();
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, id);
resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
cidade = new Cidade();
cidade.setId(resultSet.getInt(1));
cidade.setNome(resultSet.getString(2));
cidade.setEstado(new Estados(resultSet.getInt(3), resultSet.getString(4), resultSet.getString(5)));
}
} catch (SQLException e) {
e.printStackTrace();
}
return cidade;
}
public static void main(String[] args) {
// TODO code application logic here
CidadeDAO cidadeDAO = new CidadeDAO();
List<Cidade> cidadesList = new ArrayList<>();
cidadesList = cidadeDAO.listarTodos();
for (Cidade c : cidadesList) {
System.out.println(c.getId());
System.out.println(c.getNome());
System.out.println(c.getEstado());
}
}
}