Advertisement
alandmx40

Aula apsf

Nov 23rd, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.02 KB | None | 0 0
  1. public class Principal {
  2.  
  3.     /**
  4.      * @param args the command line arguments
  5.      */
  6.     public static void main(String[] args) {
  7.        
  8.         Cliente cliente = new Cliente(01, null, null);
  9.         cliente.setNome("Joao");
  10.         cliente.setEmail("joao@gmail.com");
  11.         cliente.salvar();
  12.         Cliente cliente2 = new Cliente(02, null, null);
  13.         cliente2.setNome("Maria");
  14.         cliente2.setEmail("maria@gmail.com");
  15.         cliente2.salvar();
  16.        
  17.     }
  18.    
  19. }
  20.  
  21. import java.sql.*;
  22.  
  23. /**
  24.  *
  25.  * @author ALUNO
  26.  */
  27. public class Conexao {
  28.    
  29.     public static String USUARIO = "root";
  30.     public static String SENHA = "";
  31.     public static String CONNECTION_URL = "jdbc:mysql://localhost/apsf";
  32.     public Connection getConexao() throws SQLException {
  33.        
  34.     Connection conn = DriverManager.getConnection(CONNECTION_URL, USUARIO, SENHA);
  35.         return conn;
  36.     }
  37.  
  38. }
  39.  
  40. import java.util.List;
  41. /**
  42.  *
  43.  * @author ALUNO
  44.  */
  45. public interface ICliente {
  46.    
  47.     public void salvar();
  48.     public List<Cliente> listar();
  49.    
  50. }
  51.  
  52. import java.sql.*;
  53. import java.util.*;
  54. /**
  55.  *
  56.  * @author ALUNO
  57.  */
  58. public class Cliente implements ICliente {
  59.    
  60.     private int id;
  61.     private String nome;
  62.     private String email;
  63.  
  64.     public Cliente(int id, String nome, String email) {
  65.         this.id = id;
  66.         this.nome = nome;
  67.         this.email = email;
  68.     }
  69.    
  70.     public Cliente() {
  71.        
  72.     }
  73.  
  74.     public int getId() {
  75.         return id;
  76.     }
  77.  
  78.     public void setId(int id) {
  79.         this.id = id;
  80.     }
  81.  
  82.     public String getNome() {
  83.         return nome;
  84.     }
  85.  
  86.     public void setNome(String nome) {
  87.         this.nome = nome;
  88.     }
  89.  
  90.     public String getEmail() {
  91.         return email;
  92.     }
  93.  
  94.     public void setEmail(String email) {
  95.         this.email = email;
  96.     }
  97.    
  98.     public void salvar() {
  99.         try {
  100.         Connection conexao = new Conexao().getConexao();
  101.         String sql = "insert into tb_cliente(nome, email) values (?,?)";
  102.         PreparedStatement pstmt = conexao.prepareStatement(sql);
  103.         pstmt.setString(1, this.getNome());
  104.         pstmt.setString(2, this.getEmail());
  105.         pstmt.execute();
  106.         pstmt.close();
  107.         conexao.close();
  108.         System.out.println("Cliente salvo");
  109.         } catch (SQLException e) {
  110.         throw new RuntimeException(e.getMessage());
  111.         }
  112.     }
  113.    
  114.     public List<Cliente> listar() {
  115.     List<Cliente> clientes = new ArrayList<Cliente>();
  116.     try {
  117.         Connection conexao = new Conexao().getConexao();
  118.         String sql = "select * from tb_cliente";
  119.         PreparedStatement pstmt = conexao.prepareStatement(sql);
  120.         ResultSet rs = pstmt.executeQuery();
  121.         while (rs.next()) {
  122.             Cliente cliente = new Cliente(getId(), getNome(), getEmail());
  123.             cliente.setId(rs.getInt("id"));
  124.             cliente.setNome(rs.getString("nome"));
  125.             cliente.setEmail(rs.getString("email"));
  126.             clientes.add(cliente);
  127.         }
  128.         rs.close();
  129.         pstmt.close();
  130.         conexao .close();
  131.         return clientes;
  132.         } catch (SQLException e) {
  133.             throw new RuntimeException(e.getMessage());
  134.         }
  135.     }
  136. }
  137.  
  138. import java.util.*;
  139.  
  140. /**
  141.  *
  142.  * @author ALUNO
  143.  */
  144. public class InterfaceLinhaComando {
  145.    
  146.     private Scanner scanner;
  147.    
  148.     public InterfaceLinhaComando() {
  149.         this.scanner = new Scanner(System.in);
  150.     }
  151.    
  152.     public Cliente lerCliente() {
  153.     Cliente cliente = new Cliente();
  154.     System.out.println("Digite o nome:");
  155.     cliente.setNome(scanner.nextLine());
  156.     System.out.println("Digite o email:");
  157.     cliente.setEmail(scanner.nextLine());
  158.     return cliente;
  159.     }
  160.    
  161.     public void exibirListaClientes() {
  162.     Cliente clienteConsulta = new Cliente();
  163.     for (Cliente cliente: clienteConsulta.listar()) {
  164.         System.out.println("Id: " + cliente.getId() +
  165.         ", Nome: " + cliente.getNome() +
  166.         ", Email: " + cliente.getEmail());
  167. }
  168.     }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement