amigos… olhem só, me desculpem pela confusao, agora nem eu sei mais o que estou fazendo, to confundindo geral…
meu campo id no banco de dados, esta como integer. e meu DAO esta desta forma:
package br.com.customermanager.model.dao;
import br.com.customermanager.model.entity.Customer;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JOptionPane;
/**
*
* @author marcelo
*/
public class CustomerDao {
private final static String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS clientes (id int(10) "
+ "NOT NULL AUTO_INCREMENT PRIMARY KEY, nome VARCHAR(20)"
+ " NOT NULL, cpf varchar(20) NOT NULL, telefone varchar(20) NOT NULL)";
private final static String DELETE_CLIENTE = "DELETE FROM clientes WHERE cpf = '";
private final static String GET_CLIENTE_CPF = "SELECT * FROM clientes WHERE cpf = ?";
ResultSet rs = null;
Statement stmt = null;
PreparedStatement pstmt = null;
private Connection conexao;
public CustomerDao() throws SQLException {
this.conexao = Conecta.getConexao();
}
public void createTable() throws SQLException, Exception {
Connection conn = null;
try {
conn = Conecta.getConexao();
stmt = conn.createStatement();
pstmt.executeUpdate(CREATE_TABLE);
} catch (SQLException e) {
e.printStackTrace();
throw new Exception(
"Erro ao criar a tabela de clientes : " + CREATE_TABLE, e);
} finally {
fechaConexao(pstmt);
}
}
public void create(Customer customer) throws SQLException {
// if (!customer.equals(getClienteByCPF(cliente.getCpf()))) {
String sql = "insert into clientes (nome, cpf, endereco, telefone, estado, cidade)"
+ " values (?,?,?,?,?,?)";
PreparedStatement stmt = conexao.prepareStatement(sql);
String msg = null;
stmt.setString(1, customer.getNome());
stmt.setString(2, customer.getCpf());
stmt.setString(3, customer.getEndereco());
stmt.setString(4, customer.getTelefone());
stmt.setString(5, customer.getEstado());
stmt.setString(6, customer.getCidade());
fechaConexao(stmt);
JOptionPane.showMessageDialog(null, "Adicionado ao banco de dados");
}
public void alterar(Customer customer) throws SQLException {
String sql = "update clientes set nome = ?, cpf= ?, endereco = ?, telefone= ? , estado = ?, cidade = ? where id = ? ";
PreparedStatement stmt = conexao.prepareStatement(sql);
stmt.setString(1, customer.getNome());
stmt.setString(2, customer.getCpf());
stmt.setString(3, customer.getEndereco());
stmt.setString(4, customer.getTelefone());
stmt.setString(5, customer.getEstado());
stmt.setString(6, customer.getCidade());
stmt.setString(7, customer.getId());
fechaConexao(stmt);
JOptionPane.showMessageDialog(null, " Cadastro atualizado !!! ");
}
public Customer buscaPorID(int id) throws SQLException {
Customer customer = null;
String sql = "select * from clientes where id = ?";
PreparedStatement pstmt = null;
pstmt = conexao.prepareStatement(sql);
this.pstmt.setInt(1, id);
rs = pstmt.executeQuery();
while (rs.next()) {
customer.setId(rs.getInt(1));
customer.setNome(rs.getString(2));
customer.setCpf(rs.getString(3));
customer.setEndereco(rs.getString(4));
customer.setTelefone(rs.getString(5));
customer.setEstado(rs.getString(6));
customer.setCidade(rs.getString(7));
break;
}
rs.close();
pstmt.close();
fechaConexao(this.pstmt);
return customer;
}
public void remover(Customer customer) throws SQLException {
String sql = "delete from clientes WHERE ID=?";
PreparedStatement stmt = conexao.prepareStatement(sql);
stmt.setString(1, customer.getId());
stmt.executeUpdate();
stmt.close();
JOptionPane.showMessageDialog(null, "Cadastro removido");
}
// pelo cpf
public Customer getClienteCPF(String cpf) throws Exception {
Connection conn = null;
PreparedStatement stmt = null;
Customer cli = null;
conn = Conecta.getConexao();
try {
stmt = conn.prepareStatement(GET_CLIENTE_CPF);
stmt.setString(1, cpf);
rs = stmt.executeQuery();
while (rs.next()) {
// cli = new Customer(rs.getString("nome"));
// , rs
//.getString("telefone"), rs.getString("cpf"), rs
// .getInt("id"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
fechaConexao(stmt);
}
return cli;
}
public List<Customer> getLista() throws SQLException {
List<Customer> listaContato = new LinkedList<Customer>();
String sql = "select * from clientes";
PreparedStatement psm = conexao.prepareStatement(sql);
ResultSet rset = psm.executeQuery();
try {
while (rset.next()) {
Customer customer = new Customer();
customer.setNome(rset.getString(1));
customer.setCpf(rset.getString(2));
customer.setEndereco(rset.getString(3));
customer.setTelefone(rset.getString(4));
customer.setEstado(rset.getString(5));
customer.setCidade(rset.getString(6));
listaContato.add(customer);
}
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "erro na execucao do select");
} finally {
rset.close();
psm.close();
}
return listaContato;
}
public void fechaConexao(PreparedStatement stmt) throws SQLException {
stmt.execute();
stmt.close();
}
}
a minha classe actionListener esta desta forma:
package br.com.customermanager.model.dao;
import br.com.customermanager.model.entity.Customer;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JOptionPane;
/**
*
* @author marcelo
*/
public class CustomerDao {
private final static String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS clientes (id int(10) "
+ "NOT NULL AUTO_INCREMENT PRIMARY KEY, nome VARCHAR(20)"
+ " NOT NULL, cpf varchar(20) NOT NULL, telefone varchar(20) NOT NULL)";
private final static String DELETE_CLIENTE = "DELETE FROM clientes WHERE cpf = '";
private final static String GET_CLIENTE_CPF = "SELECT * FROM clientes WHERE cpf = ?";
ResultSet rs = null;
Statement stmt = null;
PreparedStatement pstmt = null;
private Connection conexao;
public CustomerDao() throws SQLException {
this.conexao = Conecta.getConexao();
}
public void createTable() throws SQLException, Exception {
Connection conn = null;
try {
conn = Conecta.getConexao();
stmt = conn.createStatement();
pstmt.executeUpdate(CREATE_TABLE);
} catch (SQLException e) {
e.printStackTrace();
throw new Exception(
"Erro ao criar a tabela de clientes : " + CREATE_TABLE, e);
} finally {
fechaConexao(pstmt);
}
}
public void create(Customer customer) throws SQLException {
// if (!customer.equals(getClienteByCPF(cliente.getCpf()))) {
String sql = "insert into clientes (nome, cpf, endereco, telefone, estado, cidade)"
+ " values (?,?,?,?,?,?)";
PreparedStatement stmt = conexao.prepareStatement(sql);
String msg = null;
stmt.setString(1, customer.getNome());
stmt.setString(2, customer.getCpf());
stmt.setString(3, customer.getEndereco());
stmt.setString(4, customer.getTelefone());
stmt.setString(5, customer.getEstado());
stmt.setString(6, customer.getCidade());
fechaConexao(stmt);
JOptionPane.showMessageDialog(null, "Adicionado ao banco de dados");
}
public void alterar(Customer customer) throws SQLException {
String sql = "update clientes set nome = ?, cpf= ?, endereco = ?, telefone= ? , estado = ?, cidade = ? where id = ? ";
PreparedStatement stmt = conexao.prepareStatement(sql);
stmt.setString(1, customer.getNome());
stmt.setString(2, customer.getCpf());
stmt.setString(3, customer.getEndereco());
stmt.setString(4, customer.getTelefone());
stmt.setString(5, customer.getEstado());
stmt.setString(6, customer.getCidade());
stmt.setString(7, customer.getId());
fechaConexao(stmt);
JOptionPane.showMessageDialog(null, " Cadastro atualizado !!! ");
}
public Customer buscaPorID(int id) throws SQLException {
Customer customer = null;
String sql = "select * from clientes where id = ?";
PreparedStatement pstmt = null;
pstmt = conexao.prepareStatement(sql);
this.pstmt.setInt(1, id);
rs = pstmt.executeQuery();
while (rs.next()) {
customer.setId(rs.getInt(1));
customer.setNome(rs.getString(2));
customer.setCpf(rs.getString(3));
customer.setEndereco(rs.getString(4));
customer.setTelefone(rs.getString(5));
customer.setEstado(rs.getString(6));
customer.setCidade(rs.getString(7));
break;
}
rs.close();
pstmt.close();
fechaConexao(this.pstmt);
return customer;
}
public void remover(Customer customer) throws SQLException {
String sql = "delete from clientes WHERE ID=?";
PreparedStatement stmt = conexao.prepareStatement(sql);
stmt.setString(1, customer.getId());
stmt.executeUpdate();
stmt.close();
JOptionPane.showMessageDialog(null, "Cadastro removido");
}
// pelo cpf
public Customer getClienteCPF(String cpf) throws Exception {
Connection conn = null;
PreparedStatement stmt = null;
Customer cli = null;
conn = Conecta.getConexao();
try {
stmt = conn.prepareStatement(GET_CLIENTE_CPF);
stmt.setString(1, cpf);
rs = stmt.executeQuery();
while (rs.next()) {
// cli = new Customer(rs.getString("nome"));
// , rs
//.getString("telefone"), rs.getString("cpf"), rs
// .getInt("id"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
fechaConexao(stmt);
}
return cli;
}
public List<Customer> getLista() throws SQLException {
List<Customer> listaContato = new LinkedList<Customer>();
String sql = "select * from clientes";
PreparedStatement psm = conexao.prepareStatement(sql);
ResultSet rset = psm.executeQuery();
try {
while (rset.next()) {
Customer customer = new Customer();
customer.setNome(rset.getString(1));
customer.setCpf(rset.getString(2));
customer.setEndereco(rset.getString(3));
customer.setTelefone(rset.getString(4));
customer.setEstado(rset.getString(5));
customer.setCidade(rset.getString(6));
listaContato.add(customer);
}
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "erro na execucao do select");
} finally {
rset.close();
psm.close();
}
return listaContato;
}
public void fechaConexao(PreparedStatement stmt) throws SQLException {
stmt.execute();
stmt.close();
}
}
e minha classe customerService, agora, esta desta forma:
package br.com.customermanager.model.service;
import br.com.customermanager.model.dao.CustomerDao;
import br.com.customermanager.model.entity.Customer;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author aluno
*/
public class CustomerService {
private CustomerDao dao;
public CustomerService() {
try {
dao = new CustomerDao();
} catch (SQLException ex) {
Logger.getLogger(CustomerService.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void create(Customer customer) {
try {
dao.create(customer);
} catch (SQLException ex) {
Logger.getLogger(CustomerService.class.getName()).log(Level.SEVERE, null, ex);
}
}
public int buscaPorID(int id) {
try {
Customer customer = new Customer();
customer = dao.buscaPorID(id);
return customer.getId();
} catch (Exception ex) {
Logger.getLogger(CustomerService.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void excluir(Customer customer) {
try {
dao.remover(customer);
} catch (Exception ex) {
Logger.getLogger(CustomerService.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void exibir(Customer customer) {
// System.out.println("Exibir " + customer);
try {
dao.getLista();
} catch (SQLException ex) {
Logger.getLogger(CustomerService.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void alterar(Customer customer) {
System.out.println("Alterar " + customer);
try {
dao.alterar(customer);
} catch (SQLException ex) {
Logger.getLogger(CustomerService.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
deste jeito que esta, na customer service, esta apresentando o mesmo erro:
missing return statement
ja mechi tando no codigo, que não sei mais o que ta errado, e o que esta certo… …rsrs…desculpem…