Advertisement
hercioneto

[RSTI 2023] Projeto agenda - classe Contatos

Dec 8th, 2023
942
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.64 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.ResultSet;
  3. import java.sql.SQLException;
  4. import java.sql.Statement;
  5. import java.util.ArrayList;
  6.  
  7. /**
  8.  *
  9.  * @author Professor Hercio Neto
  10.  */
  11. public class Contatos {
  12.  
  13.     private int idContato;
  14.     private String nome, telefone, email;
  15.  
  16.     public int getIdContato() {
  17.         return idContato;
  18.     }
  19.  
  20.     public void setIdContato(int idContato) {
  21.         this.idContato = idContato;
  22.     }
  23.  
  24.     public String getNome() {
  25.         return nome;
  26.     }
  27.  
  28.     public void setNome(String nome) {
  29.         this.nome = nome;
  30.     }
  31.  
  32.     public String getTelefone() {
  33.         return telefone;
  34.     }
  35.  
  36.     public void setTelefone(String telefone) {
  37.         this.telefone = telefone;
  38.     }
  39.  
  40.     public String getEmail() {
  41.         return email;
  42.     }
  43.  
  44.     public void setEmail(String email) {
  45.         this.email = email;
  46.     }
  47.    
  48.      public void listarUnico(int idContato) {
  49.  
  50.         Conexao conexao = new Conexao();
  51.         Connection conn = conexao.conectar();
  52.  
  53.         try {
  54.             String sql = "Select * from contatos where idContato = "+ idContato;
  55.              
  56.             Statement stmt = conn.createStatement();
  57.  
  58.             ResultSet rs = stmt.executeQuery(sql);
  59.  
  60.             while (rs.next()) {
  61.  
  62.                 this.idContato = rs.getInt("idContato");
  63.                 this.nome = rs.getString("nome");
  64.                 this.telefone = rs.getString("telefone");
  65.                 this.email = rs.getString("email");
  66.                 //System.out.println("Código: " + codigoCategoria + "\ncategoria: " + categoria + "\nPosição: " + posicao + "\n ------\n");
  67.  
  68.             }
  69.  
  70.             stmt.close();
  71.             conexao.desconectar(conn);
  72.  
  73.         } catch (SQLException ex) {
  74.             System.out.println("Erro " + ex);
  75.         }
  76.  
  77.     }
  78.    
  79.     public ArrayList listar() {
  80.  
  81.         Conexao conexao = new Conexao();
  82.         Connection conn = conexao.conectar();
  83.  
  84.         try {
  85.             String sql = "Select * from contatos";
  86.              
  87.             Statement stmt = conn.createStatement();
  88.  
  89.             ResultSet rs = stmt.executeQuery(sql);
  90.             stmt.close();
  91.             conexao.desconectar(conn);
  92.            
  93.            
  94.             ArrayList<Contatos> contatos = new ArrayList<>();
  95.             while(rs.next())
  96.             {  
  97.                 Contatos c = new Contatos();
  98.                 c.setIdContato(rs.getInt("idContato"));
  99.                 c.setNome(rs.getString("nome"));
  100.                 c.setTelefone(rs.getString("telefone"));
  101.                 c.setEmail(rs.getString("email"));
  102.                contatos.add(c);
  103.             }
  104.           return contatos;  
  105.  
  106.         } catch (SQLException ex) {
  107.             System.out.println("Erro " + ex);
  108.             ArrayList<Contatos> contatos = new ArrayList<>();
  109.             return contatos;
  110.         }
  111.  
  112.     }
  113.  
  114.     public void inserir() {
  115.  
  116.         Conexao conexao = new Conexao();
  117.         Connection conn = conexao.conectar();
  118.  
  119.         try {
  120.             String sql = "INSERT INTO `contatos` (`idcontato`, `nome`, `telefone`,`email`) VALUES (NULL, '" + this.nome + "', '" + this.telefone +"', '" + this.email + "');";
  121.  
  122.             Statement stmt = conn.createStatement();
  123.  
  124.             ResultSet rs = stmt.executeQuery(sql);
  125.  
  126.             stmt.close();
  127.             conexao.desconectar(conn);
  128.  
  129.         } catch (SQLException ex) {
  130.             System.out.println("Erro " + ex);
  131.         }
  132.  
  133.     }
  134.    
  135.     public void alterar() {
  136.  
  137.         Conexao conexao = new Conexao();
  138.         Connection conn = conexao.conectar();
  139.        
  140.  
  141.         try {
  142.             String sql = "UPDATE `contatos` SET `nome` = '"+this.nome+"', `telefone` = '"+this.telefone+"', `email` = '"+this.email+"' WHERE `contatos`.`idcontato` = "+this.idContato;
  143.  
  144.             Statement stmt = conn.createStatement();
  145.  
  146.             ResultSet rs = stmt.executeQuery(sql);
  147.  
  148.             stmt.close();
  149.             conexao.desconectar(conn);
  150.            
  151.  
  152.         } catch (SQLException ex) {
  153.             System.out.println("Erro " + ex);
  154.         }
  155.  
  156.     }
  157.     public void excluir() {
  158.  
  159.         Conexao conexao = new Conexao();
  160.         Connection conn = conexao.conectar();
  161.        
  162.  
  163.         try {
  164.             String sql = "Delete from `contatos`  WHERE `contatos`.`idcontato` = "+this.idContato;
  165.  
  166.             Statement stmt = conn.createStatement();
  167.  
  168.             ResultSet rs = stmt.executeQuery(sql);
  169.  
  170.             stmt.close();
  171.             conexao.desconectar(conn);
  172.            
  173.  
  174.         } catch (SQLException ex) {
  175.             System.out.println("Erro " + ex);
  176.         }
  177.  
  178.     }
  179.  
  180. }
  181.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement