Exemplo n.º 1
0
  public void insert(Cliente cliente) {
    try {
      connection = Conexao.getInstance().obterConexao();
      connection.setAutoCommit(false);
      statement = connection.createStatement();
      statement.executeUpdate(
          "INSERT INTO CLIENTE (ID, NOME, RG, CPF, ENDERECO, TELEFONE) VALUES ("
              + ""
              + cliente.getId()
              + ", "
              + "'"
              + cliente.getNome()
              + "', "
              + "'"
              + cliente.getRg()
              + "', "
              + "'"
              + cliente.getCpf()
              + "', "
              + "'"
              + cliente.getEndereco()
              + "', "
              + "'"
              + cliente.getTelefone()
              + "')");
      connection.commit();
      System.out.println("Insert Cliente feito com sucesso!");

    } catch (Exception e) {
      // TODO: handle exception
      try {
        if (connection != null && !connection.isClosed()) {
          connection.rollback();
        }
      } catch (SQLException e2) {
        // TODO: handle exception
        e2.printStackTrace();
      } finally {
        try {
          if (resultSet != null) {
            resultSet.close();
          }
          if (statement != null) {
            statement.close();
          }
          if (connection != null) {
            connection.close();
          }
        } catch (Exception e3) {
          // TODO: handle exception
        }
      }
    }
  }
Exemplo n.º 2
0
  public List<Cliente> select() {
    List<Cliente> clientes = new ArrayList<Cliente>();
    Cliente cliente;
    try {
      connection = Conexao.getInstance().obterConexao();
      connection.setAutoCommit(false);
      statement = connection.createStatement();

      resultSet = statement.executeQuery("SELECT * FROM CLIENTE");
      while (resultSet.next()) {
        cliente = new Cliente();
        cliente.setId(resultSet.getInt("id"));
        cliente.setNome(resultSet.getString("nome"));
        cliente.setRg(resultSet.getString("rg"));
        cliente.setCpf(resultSet.getString("cpf"));
        cliente.setEndereco(resultSet.getString("endereco"));
        cliente.setTelefone(resultSet.getString("telefone"));
        clientes.add(cliente);
      }
    } catch (Exception e) {
      // TODO: handle exception
      e.printStackTrace();
      try {
        if (connection != null && !connection.isClosed()) {
          connection.rollback();
        }
      } catch (Exception e2) {
        // TODO: handle exception
        e2.printStackTrace();
      } finally {
        try {
          if (resultSet != null) {
            resultSet.close();
          }
          if (statement != null) {
            statement.close();
          }
          if (connection != null) {
            connection.close();
          }
        } catch (Exception e3) {
          // TODO: handle exception
        }
      }
    }
    return clientes;
  }
Exemplo n.º 3
0
  public Cliente selectByNome(String nome) {
    Cliente cliente = new Cliente();
    try {
      connection = Conexao.getInstance().obterConexao();
      connection.setAutoCommit(false);
      statement = connection.createStatement();

      resultSet = statement.executeQuery("SELECT * FROM CLIENTE WHERE NOME = '" + nome + "'");
      resultSet.next();
      cliente.setId(resultSet.getInt("id"));
      cliente.setRg(resultSet.getString("rg"));
      cliente.setCpf(resultSet.getString("cpf"));
      cliente.setNome(resultSet.getString("nome"));
      cliente.setEndereco(resultSet.getString("endereco"));
      cliente.setTelefone(resultSet.getString("telefone"));

    } catch (Exception e) {
      // TODO: handle exception
      try {
        if (connection != null && !connection.isClosed()) {
          connection.rollback();
        }
      } catch (SQLException e2) {
        // TODO: handle exception
        e2.printStackTrace();
      } finally {
        try {
          if (resultSet != null) {
            resultSet.close();
          }
          if (statement != null) {
            statement.close();
          }
          if (connection != null) {
            connection.close();
          }
        } catch (Exception e3) {
          // TODO: handle exception
        }
      }
    }

    return cliente;
  }
Exemplo n.º 4
0
  public Integer criarID() {
    Integer id = 1;
    try {
      connection = Conexao.getInstance().obterConexao();
      connection.setAutoCommit(false);
      statement = connection.createStatement();

      resultSet = statement.executeQuery("SELECT Count(*) numMax FROM CLIENTE");
      resultSet.next();
      id += resultSet.getInt("numMax");
      System.out.println(id);

    } catch (Exception e) {
      // TODO: handle exception
      try {
        if (connection != null && !connection.isClosed()) {
          connection.rollback();
        }
      } catch (SQLException e2) {
        // TODO: handle exception
        e2.printStackTrace();
      } finally {
        try {
          if (resultSet != null) {
            resultSet.close();
          }
          if (statement != null) {
            statement.close();
          }
          if (connection != null) {
            connection.close();
          }
        } catch (Exception e3) {
          // TODO: handle exception
        }
      }
    }

    return id;
  }
Exemplo n.º 5
0
 public ClienteDAO() {
   try {
     connection = Conexao.getInstance().obterConexao();
     connection.setAutoCommit(false);
     statement = connection.createStatement();
     statement.executeUpdate(
         "CREATE TABLE IF NOT EXISTS CLIENTE ("
             + "ID INTEGER PRIMARY KEY NOT NULL, "
             + "NOME VARCHAR(50), "
             + "RG VARCHAR(20), "
             + "CPF VARCHAR(20), "
             + "ENDERECO VARCHAR(60), "
             + "TELEFONE VARCHAR(20))");
     connection.commit();
   } catch (Exception e) {
     // TODO Auto-generated catch block
     try {
       if (connection != null && !connection.isClosed()) {
         connection.rollback();
       }
     } catch (SQLException e2) {
       // TODO: handle exception
       e2.printStackTrace();
     } finally {
       try {
         if (resultSet != null) {
           resultSet.close();
         }
         if (statement != null) {
           statement.close();
         }
         if (connection != null) {
           connection.close();
         }
       } catch (Exception e3) {
         // TODO: handle exception
       }
     }
   }
 }