parte 05
[code]
private Boolean verificaSePessoaExiste(String valor) throws Exception {
PreparedStatement ps = null;
Connection conect = null;
ResultSet rs = null;
String sql = “select count(*) from pessoa where email=?”;
try{
this.conn = Conexao.getConnection();
conect = this.conn;
ps = conn.prepareStatement(sql);
ps.setString(1,valor.toLowerCase());
rs = ps.executeQuery();
rs.next();
return rs.getInt(1)>0;
}catch(SQLException sqle){
throw new Exception("Error Verificar se Cliente Existe "+sqle);
}finally{
Conexao.CloseConnection(conn,ps,rs);
}
}
public void inserir() throws Exception {
try{
if(verificaSePessoaExiste(getEmail())==false){
inserirPessoa();
pegarIdPessoa();
inserirPessoaFisica();
inserirCliente();
pegarId();
setRetorno(1);
}else{
pegarId();
setRetorno(2);
}
}catch(SQLException sqle){
throw new Exception("error Inserir Cliente "+sqle);
}
}
private void inserirPessoa() throws Exception {
PreparedStatement ps = null;
Connection conect = null;
try{
String sql = "insert into pessoa(nome,telefone,email)values(?,?,?)";
this.conn = Conexao.getConnection();
conect = this.conn;
ps = conn.prepareStatement(sql);
ps.setString(1,getNome().toUpperCase());
ps.setString(2,getTelefone());
ps.setString(3,getEmail().toLowerCase());
ps.executeUpdate();
}catch(SQLException sqle){
throw new Exception("error Inserir Pessoa "+sqle);
}finally{
Conexao.CloseConnection(conn,ps);
}
}
private void inserirPessoaFisica() throws Exception {
PreparedStatement ps = null;
Connection conect = null;
try{
String sql = "insert into pessoa_fisica(pessoa_idPessoa, cpf, rg)values(?,?,?)";
this.conn = Conexao.getConnection();
conect = this.conn;
ps = this.conn.prepareStatement(sql);
if(getIdPessoa()>0){
ps.setInt(1,getIdPessoa());
ps.setString(2,getCPF());
ps.setString(3,getRG());
ps.executeUpdate();
}
}catch(SQLException sqle){
throw new Exception("error Inserir Pessoa Fisica "+sqle);
}finally{
Conexao.CloseConnection(conn,ps);
}
}
private void inserirCliente() throws Exception {
PreparedStatement ps = null;
Connection conect = null;
Boolean excluir = false;
try{
String sql = "insert into cliente(pessoa_Fisica_pessoa_idPessoa,excluido)values(?,?)";
this.conn = Conexao.getConnection();
conect = this.conn;
ps = conn.prepareStatement(sql);
ps.setInt(1, getIdPessoa());
ps.setBoolean(2, excluir);
ps.executeUpdate();
}catch(SQLException sqle){
throw new Exception("error Inserir Cliente "+sqle);
}finally{
Conexao.CloseConnection(conn,ps);
}
}
public ArrayList listar(String tipo, String valor) throws Exception{
PreparedStatement ps = null;
Connection conect = null;
ResultSet rs = null;
String sql = “select id_Cliente, nome, email, idPessoa from listar_clientes”;
String filtro="";
valor = valor.toUpperCase();
switch(Integer.parseInt(tipo)){
case 1: filtro = " where id_Cliente=?";
break;
case 2: filtro = "where CPF=? order by nome";
break;
case 3: filtro = "where nome like ? order by nome";
break;
}
sql = sql+" "+filtro;
try{
this.conn = Conexao.getConnection();
conect = this.conn;
ps = conn.prepareStatement(sql);
switch(Integer.parseInt(tipo)){
case 1: ps.setString(1,valor);
break;
case 2: ps.setString(1,valor);
break;
case 3: ps.setString(1, "%" + valor +"%");
break;
}
// if(tipo.equals(“2”)){
// ps.setString(1, “%” + valor +"%");} else {
// ps.setString(1,valor);
// }
rs = ps.executeQuery();
ArrayList<lCliente> listApoio = new ArrayList<lCliente>();
while(rs.next()){
lCliente apoio = new lCliente();
apoio.setId_Cliente(rs.getInt(1));
apoio.setNome (rs.getString(2));
apoio.setEmail (rs.getString(3));
apoio.setIdPessoa (rs.getInt(4));
listApoio.add(apoio);
}
return listApoio;
}catch(SQLException sqle){
throw new Exception("Erro no Metodo Listar Funcionarios "+sqle);
}finally{
Conexao.CloseConnection(conn,ps,rs);
}
}
}