[code]
package model;
import java.sql.Connection;
import model.ConexaoBanco;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import model.ClienteF;
import model.ClienteJ;
public class ClienteBanco {
public static void cadastrarCliente (ClienteF c) throws SQLException, ClassNotFoundException {
try {
Connection conexao = ConexaoBanco.conecta();
Statement stat = conexao.createStatement();
String sql = "";
sql = "insert into empresa.pessoafisica(nome, sobrenome, rg, cpf, idade, dtNascimento, sexo, estadoCivil, email, DDD, telefone, endereco, cidade, estado, pais)"
+ "values('" + c.getNome() + "', '" + c.getSobrenome() + "'," + c.getRg() + "," + c.getCpf() + "," + c.getIdade() + ", " + c.getDtNascimento() + ",'" + c.getSexo() + "', '" + c.getEstadocivil() + "', '" + c.getEmail() + "', " + c.getDDD() + ", " + c.getTelefone() + ", '" + c.getEndereco() + "', '" + c.getCidade() + "', '" + c.getEstado() + "', '" + c.getPais() + "')";
stat.execute(sql);
stat.close();
conexao.close();
} catch (SQLException se) {
throw new SQLException("\nErro ao inserir! Altere os dados e tente novamente. \n\n"+ se.getMessage());
}
catch(ClassNotFoundException e)
{
throw new ClassNotFoundException("Não foi possível localizar o driver do banco de dados! " + e);}
}
public static void cadastrarCliente2 (ClienteJ cj) throws SQLException, ClassNotFoundException {
try {
Connection conexao = ConexaoBanco.conecta();
Statement stat = conexao.createStatement();
String sql = "";
sql = "insert into empresa.pessoajuridica(nome, nomefantasia, cnpj, cnae, logradouro, numero, complemento, bairro, estado, pais, cep, ddd, telefone1, telefone2, email)"
+ "values('" + cj.getNome() + "', '" + cj.getNomeFantasia() + "'," + cj.getCnpj() + "," + cj.getCnae() + ", '" + cj.getLogradouro() + "'," + cj.getNumero() + ", '" + cj.getBairro()+ "', '" + cj.getComplemento() + "', '" + cj.getEstado() + "', '" + cj.getPais() + "', " + cj.getCep() + ", " + cj.getDdd() + ", " + cj.getTelefone1() + ", " + cj.getTelefone2() + ", '" + cj.getEmail() + "')";
stat.execute(sql);
stat.close();
conexao.close();
} catch (SQLException se) {
throw new SQLException("\nErro ao inserir! Altere os dados e tente novamente.\n\n" + se.getMessage());
}
catch(ClassNotFoundException e)
{
throw new ClassNotFoundException("Não foi possível localizar o driver do banco de dados! " + e);}
}
public static ArrayList <ClienteF> buscarTodosClientes() throws SQLException, ClassNotFoundException {
try {
Connection conexao = ConexaoBanco.conecta();
Statement stat = conexao.createStatement();
String sql = "";
sql = "select * from empresa.pessoafisica;";
ResultSet rs = stat.executeQuery(sql);
ArrayList <ClienteF> pAux = new ArrayList<ClienteF>();
while (rs.next()) {
ClienteF c = new ClienteF();
c.setCpf(rs.getInt("cpf"));
c.setNome(rs.getString("nome"));
c.setSobrenome(rs.getString("sobrenome"));
pAux.add(c);
}
return pAux;
} catch (SQLException se) {
throw new SQLException(" Erro ao buscar todos os clientes! " + se.getMessage());
}
catch(ClassNotFoundException e)
{throw new ClassNotFoundException("Não foi possível localizar o driver do banco de dados! " + e);}
}
public static ArrayList <ClienteJ> buscarTodosClientes2() throws SQLException, ClassNotFoundException {
try {
Connection conexao = ConexaoBanco.conecta();
Statement stat = conexao.createStatement();
String sql = "";
sql = "select * from empresa.pessoajuridica;";
ResultSet rs = stat.executeQuery(sql);
ArrayList <ClienteJ> pAux = new ArrayList<ClienteJ>();
while (rs.next()) {
ClienteJ c = new ClienteJ();
c.setCnpj(rs.getInt("cnpj"));
c.setNome(rs.getString("nome"));
pAux.add(c);
}
return pAux;
} catch (SQLException se) {
throw new SQLException("Erro ao buscar todos os clientes! " + se.getMessage());
}
catch(ClassNotFoundException e)
{throw new ClassNotFoundException("Não foi possível localizar o driver do banco de dados! " + e);}
}
//public static void buscarClienteFNome(ClienteF cf) throws SQLException, ClassNotFoundException {
public static ArrayList <ClienteF> buscarClienteFNome() throws SQLException, ClassNotFoundException {
try {
Connection conexao = ConexaoBanco.conecta();
Statement stat = conexao.createStatement();
ClienteF cf= new ClienteF();
String sql = "";
sql = "select * from empresa.pessoafisica where nome= " + cf.getNome() + "";
ResultSet rs = stat.executeQuery(sql);
ArrayList <ClienteF> pAux = new ArrayList<ClienteF>();
while (rs.next()) {
ClienteF c = new ClienteF();
c.setCpf(rs.getInt("cpf"));
c.setNome(rs.getString("nome"));
c.setSobrenome(rs.getString("sobrenome"));
c.setRg(rs.getInt("rg"));
c.setIdade(rs.getInt("idade"));
c.setDtNascimento(rs.getInt("dtNascimento"));
c.setSexo(rs.getString("sexo"));
c.setEstadocivil(rs.getString("estadocivil"));
c.setEmail(rs.getString("email"));
c.setDDD(rs.getInt("ddd"));
c.setTelefone(rs.getInt("telefone"));
c.setIdade(rs.getInt("idade"));
c.setEndereco(rs.getString("endereco"));
c.setCidade(rs.getString("cidade"));
c.setEstado(rs.getString("estado"));
c.setPais(rs.getString("pais"));
pAux.add(c);
}
return pAux;
} catch (SQLException se) {
throw new SQLException("Erro ao buscar todos os clientes! " + se.getMessage());
}
catch(ClassNotFoundException e)
{throw new ClassNotFoundException("Não foi possível localizar o driver do banco de dados! " + e);}
}
//public static ArrayList <ClienteJ cj> buscarClienteJNome() throws SQLException, ClassNotFoundException {
/*(public static void buscarClienteJNome(ClienteJ cj) throws SQLException, ClassNotFoundException {
try {
Connection conexao = ConexaoBanco.conecta();
Statement stat = conexao.createStatement();
String sql = "";
sql = "select * from empresa.pessoajuridica where nome= " + cj.getNome() + "";
ResultSet rs = stat.executeQuery(sql);
ArrayList <ClienteJ> pAux = new ArrayList<ClienteJ>();
while (rs.next()) {
ClienteJ c = new ClienteJ();
c.setCnpj(rs.getInt("cnpj"));
c.setNome(rs.getString("nome"));
c.setNomeFantasia(rs.getString("nome fantasia"));
c.setCnae(rs.getInt("cnae"));
c.setLogradouro(rs.getString("logradouro"));
c.setNumero(rs.getInt("numero"));
c.setComplemento(rs.getString("complemento"));
c.setBairro(rs.getString("bairro"));
c.setCidade(rs.getString("cidade"));
c.setEstado(rs.getString("estado"));
c.setPais(rs.getString("pais"));
c.setCep(rs.getInt("cep"));
c.setDdd(rs.getInt("ddd"));
c.setTelefone1(rs.getInt("telefone1"));
c.setTelefone2(rs.getInt("telefone2"));
c.setEmail(rs.getString("email"));
pAux.add(c);
}
return pAux;
} catch (SQLException se) {
throw new SQLException("Erro ao buscar todos os clientes! " + se.getMessage());
}
catch(ClassNotFoundException e)
{throw new ClassNotFoundException("Não foi possível localizar o driver do banco de dados! " + e);}
}*/
public static void mostrarQuantidadeClientes () throws SQLException, ClassNotFoundException {
try {
Connection conexao = ConexaoBanco.conecta();
Statement stat = conexao.createStatement();
String sql = "";
sql = "select count(*) from pessoafisica;";
stat.execute(sql);
stat.close();
conexao.close();
} catch (SQLException se) {
throw new SQLException("Erro ao mostrar a quantidade de clientes cadastrados! " + se.getMessage());
}
catch(ClassNotFoundException e)
{throw new ClassNotFoundException("Não foi possível localizar o driver do banco de dados! " + e);}
}
public static void mostrarQuantidadeClientes2 () throws SQLException, ClassNotFoundException {
try {
Connection conexao = ConexaoBanco.conecta();
Statement stat = conexao.createStatement();
String sql = "";
sql = "select count(*) from pessoajuridica;";
stat.execute(sql);
stat.close();
conexao.close();
} catch (SQLException se) {
throw new SQLException("Erro ao mostrar a quantidade de clientes cadastrados! " + se.getMessage());
}
catch(ClassNotFoundException e)
{throw new ClassNotFoundException("Não foi possível localizar o driver do banco de dados! " + e);}
}
public static void deletarClientes() throws SQLException, ClassNotFoundException {
try {
Connection conexao = ConexaoBanco.conecta();
Statement stat = conexao.createStatement();
String sql = "";
sql = "drop table pessoafisica;";
stat.execute(sql);
stat.close();
conexao.close();
} catch (SQLException se) {
throw new SQLException("Erro ao deletar! " + se.getMessage());
}
catch(ClassNotFoundException e)
{throw new ClassNotFoundException("Não foi possível localizar o driver do banco de dados! " + e);}
}
public static void deletarPessoaF(ClienteF cf) throws SQLException, ClassNotFoundException {
try {
Connection conexao = ConexaoBanco.conecta();
Statement stat = conexao.createStatement();
String sql = "";
sql = "delete from pessoafisica where cpf= " + cf.getCpf() + "";
stat.execute(sql);
stat.close();
conexao.close();
} catch (SQLException se) {
throw new SQLException("Errro ao deletar! " + se.getMessage());
}
}
public static void deletarPessoaJ(ClienteJ cj) throws SQLException, ClassNotFoundException {
try {
Connection conexao = ConexaoBanco.conecta();
Statement stat = conexao.createStatement();
String sql = "";
sql = "delete from pessoajuridica where cnpj= " + cj.getCnpj() + "";
stat.execute(sql);
stat.close();
conexao.close();
} catch (SQLException se) {
throw new SQLException("Errro ao deletar! " + se.getMessage());
}
}
public static void atualizarClienteF(ClienteF f) throws SQLException, ClassNotFoundException {
try {
Connection conexao = ConexaoBanco.conecta();
Statement stat = conexao.createStatement();
String sql = "";
sql = "update pessoafisica set nome='" + f.getNome() + "', sobrenome='" + f.getSobrenome() + "', idade='" + f.getIdade() + "', dtNascimento='" + f.getDtNascimento() + "', sexo='" + f.getSexo() + "', estadocivil='" + f.getEstadocivil() + "', email='" + f.getEmail() + "', ddd='" + f.getDDD() + "', telefone='" + f.getTelefone() + "', endereco='" + f.getEndereco() + "', cidade='" + f.getCidade() + "', estado='" + f.getEstado() + "', pais='" + f.getPais() + "' where nome= " + f.getNome() + "";
stat.executeUpdate(sql);
stat.close();
conexao.close();
} catch (SQLException se) {
throw new SQLException("Erro ao atualizar! " + se.getMessage());
}
}
}//fim da Classe ClienteBanco [/code]