Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Principal {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- Cliente cliente = new Cliente(01, null, null);
- cliente.setNome("Joao");
- cliente.setEmail("joao@gmail.com");
- cliente.salvar();
- Cliente cliente2 = new Cliente(02, null, null);
- cliente2.setNome("Maria");
- cliente2.setEmail("maria@gmail.com");
- cliente2.salvar();
- }
- }
- import java.sql.*;
- /**
- *
- * @author ALUNO
- */
- public class Conexao {
- public static String USUARIO = "root";
- public static String SENHA = "";
- public static String CONNECTION_URL = "jdbc:mysql://localhost/apsf";
- public Connection getConexao() throws SQLException {
- Connection conn = DriverManager.getConnection(CONNECTION_URL, USUARIO, SENHA);
- return conn;
- }
- }
- import java.util.List;
- /**
- *
- * @author ALUNO
- */
- public interface ICliente {
- public void salvar();
- public List<Cliente> listar();
- }
- import java.sql.*;
- import java.util.*;
- /**
- *
- * @author ALUNO
- */
- public class Cliente implements ICliente {
- private int id;
- private String nome;
- private String email;
- public Cliente(int id, String nome, String email) {
- this.id = id;
- this.nome = nome;
- this.email = email;
- }
- public Cliente() {
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getNome() {
- return nome;
- }
- public void setNome(String nome) {
- this.nome = nome;
- }
- public String getEmail() {
- return email;
- }
- public void setEmail(String email) {
- this.email = email;
- }
- public void salvar() {
- try {
- Connection conexao = new Conexao().getConexao();
- String sql = "insert into tb_cliente(nome, email) values (?,?)";
- PreparedStatement pstmt = conexao.prepareStatement(sql);
- pstmt.setString(1, this.getNome());
- pstmt.setString(2, this.getEmail());
- pstmt.execute();
- pstmt.close();
- conexao.close();
- System.out.println("Cliente salvo");
- } catch (SQLException e) {
- throw new RuntimeException(e.getMessage());
- }
- }
- public List<Cliente> listar() {
- List<Cliente> clientes = new ArrayList<Cliente>();
- try {
- Connection conexao = new Conexao().getConexao();
- String sql = "select * from tb_cliente";
- PreparedStatement pstmt = conexao.prepareStatement(sql);
- ResultSet rs = pstmt.executeQuery();
- while (rs.next()) {
- Cliente cliente = new Cliente(getId(), getNome(), getEmail());
- cliente.setId(rs.getInt("id"));
- cliente.setNome(rs.getString("nome"));
- cliente.setEmail(rs.getString("email"));
- clientes.add(cliente);
- }
- rs.close();
- pstmt.close();
- conexao .close();
- return clientes;
- } catch (SQLException e) {
- throw new RuntimeException(e.getMessage());
- }
- }
- }
- import java.util.*;
- /**
- *
- * @author ALUNO
- */
- public class InterfaceLinhaComando {
- private Scanner scanner;
- public InterfaceLinhaComando() {
- this.scanner = new Scanner(System.in);
- }
- public Cliente lerCliente() {
- Cliente cliente = new Cliente();
- System.out.println("Digite o nome:");
- cliente.setNome(scanner.nextLine());
- System.out.println("Digite o email:");
- cliente.setEmail(scanner.nextLine());
- return cliente;
- }
- public void exibirListaClientes() {
- Cliente clienteConsulta = new Cliente();
- for (Cliente cliente: clienteConsulta.listar()) {
- System.out.println("Id: " + cliente.getId() +
- ", Nome: " + cliente.getNome() +
- ", Email: " + cliente.getEmail());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement