package jsp.tutoriais.tutorial_003_001_jsp; import java.sql.*; public class Pesquisa extends java.lang.Object { private String nome = blank; private String fone = blank; private String celular = blank; private String email = blank; private int intmod1; private int intmod2; private int intmod3; private int intmod4; private int dispmod1; private int dispmod2; private int dispmod3; private int dispmod4; private int acao; static Connection con = null; static private String blank = ""; public Pesquisa() { if (con == null) { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con = DriverManager.getConnection("jdbc:odbc:OPINIAO", "tutdb", "tutdb"); } catch (Exception e) { System.err.println ("Erro no comando SQL de Conexao"); } } } public String getNome() { return nome; } public void setNome(String nome) { this.nome = nome; } public String getFone() { return fone; } public void setFone(String fone) { this.fone = fone; } public String getCelular() { return celular; } public void setCelular(String celular) { this.celular = celular; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public int getIntmod1() { return intmod1; } public void setIntmod1(int intmod1) { this.intmod1 = intmod1; } public int getIntmod2() { return intmod2; } public void setIntmod2(int intmod2) { this.intmod2 = intmod2; } public int getIntmod3() { return intmod3; } public void setIntmod3(int intmod3) { this.intmod3 = intmod3; } public int getIntmod4() { return intmod4; } public void setIntmod4(int intmod4) { this.intmod4 = intmod4; } public int getDispmod1() { return dispmod1; } public void setDispmod1(int dispmod1) { this.dispmod1 = dispmod1; } public int getDispmod2() { return dispmod2; } public void setDispmod2(int dispmod2) { this.dispmod2 = dispmod2; } public int getDispmod3() { return dispmod3; } public void setDispmod3(int dispmod3) { this.dispmod3 = dispmod3; } public int getDispmod4() { return dispmod4; } public void setDispmod4(int dispmod4) { this.dispmod4 = dispmod4; } public int getAcao() { return acao; } public void setAcao(int acao) { this.acao = acao; } public boolean fazAcao() { if (acao == 1) { // consultar try { Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery ("SELECT * FROM TREINAMENTO WHERE NOME LIKE '%" + nome + "%'"); if (rs.next()) { nome = rs.getString(1); fone = rs.getString(2); celular = rs.getString(3); email = rs.getString(4); intmod1 = rs.getInt(5); intmod2 = rs.getInt(6); intmod3 = rs.getInt(7); intmod4 = rs.getInt(8); dispmod1 = rs.getInt(9); dispmod2 = rs.getInt(10); dispmod3 = rs.getInt(11); dispmod4 = rs.getInt(12); } else { acao = 3; fazAcao(); return false; } rs.close(); stmt.close(); } catch (Exception e) { System.err.println ("Erro no comando SQL de Consulta"); return false; } } else if (acao == 2) { // Inserir try { Statement stmt = con.createStatement(); stmt.execute ("INSERT INTO TREINAMENTO (nome, fone, celular, email, " + "intmod1, intmod2, intmod3, intmod4, "+ "dispmod1, dispmod2, dispmod3, dispmod4) VALUES (' "+ nome + "', '" + fone + "', '" + celular + "', '" + email + "', "+ intmod1 + ", " + intmod2 + ", " + intmod3 + ", " + intmod4 + ", " + dispmod1 + ", " + dispmod2 + ", " + dispmod3 + ", " + dispmod4 + ") "); stmt.close(); } catch (Exception e) { System.err.println ("Erro no comando SQL de Insert"); return false; } } else if (acao == 3) { // Limpar Campos nome = fone = celular = email = blank; intmod1 = intmod2 = intmod3 = intmod4 = 0; dispmod1 = dispmod2 = dispmod3 = dispmod4 = 0; } acao = 0; return true; } public static void main (String args[]) { Pesquisa p1 = new Pesquisa(); p1.setNome("Galileu"); p1.setEmail("galileu@info.ufrn.br"); p1.setIntmod1(10); p1.setDispmod1(3); p1.setAcao(2); p1.fazAcao(); Pesquisa p2 = new Pesquisa(); p2.setNome("Maria Joaquina"); p2.setEmail("mjapg@jspbrasil.com.br"); p2.setIntmod3(8); p2.setDispmod3(1); p2.setAcao(2); p2.fazAcao(); Pesquisa p3 = new Pesquisa(); p3 = new Pesquisa(); p3.setNome("Joaquina"); p3.setAcao(1); p3.fazAcao(); System.out.println (p3.getNome()+": "+p3.getEmail()); } }