Aqui estão os codigos:
Cliente.java
public class Cliente {
public Cliente(){
}
public void setNome(String nome){
this.nome = nome;
}
public String getNome(){
return this.nome;
}
public void setRG(String rg){
this.rg = rg;
}
public String getRG(){
return this.rg;
}
public void setDataNascimento(String dataNascimento){
this.dataNascimento = dataNascimento;
}
public String getDataNascimento(){
return this.dataNascimento;
}
public void setSexo(String sexo){
this.sexo = sexo;
}
public String getSexo(){
return this.sexo;
}
private String nome;
private String rg;
private String dataNascimento;
private String sexo;
}
ClienteDAO.java
import java.sql.*;
import java.util.ArrayList;
public class ClienteDAO {
private Connection connection;
public ClienteDAO(){
try{
this.connection = ConnectionFactory.getConnection();
}catch(Exception e){
e.printStackTrace();
}
}
public void adiciona(Cliente cliente){
try{
PreparedStatement stmt =
this.connection.prepareStatement("INSERT INTO cliente VALUES (?,?,?,?)");
stmt.setString(1, cliente.getNome());
stmt.setString(2, cliente.getRG());
stmt.setString(3, cliente.getDataNascimento());
stmt.setString(4, cliente.getSexo());
javax.swing.JOptionPane.showMessageDialog(null,stmt);
stmt.execute();
stmt.close();
}catch(Exception e){
e.getStackTrace();
}
}
public void deleta(Cliente cliente) throws Exception{
PreparedStatement stmt = this.connection.prepareStatement("delete from Cliente where nome=?");
stmt.setString(1, cliente.getNome());
stmt.execute();
stmt.close();
}
public ArrayList buscar(String buscaNome){
ArrayList<Cliente> lista = new ArrayList<Cliente>();
Cliente aux = null;
ResultSet rs;
try{
PreparedStatement stmt = this.connection.prepareStatement("SELECT * FROM Cliente WHERE nome LIKE ?;");
stmt.setString(1,"'%"+buscaNome+"%'");
rs = stmt.executeQuery();
while (rs.next()){
aux = new Cliente();
aux.setNome(rs.getString(0));//nomes dos campos das tabelas escritos
aux.setRG(rs.getString(1));
aux.setDataNascimento(rs.getString(2));
aux.setSexo(rs.getString(3));
lista.add(aux);
}
stmt.close();
}catch(SQLException e){
e.printStackTrace();
}
return lista;
}
}
FmClientes.java
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class FmClientes implements ActionListener{
private JFrame janelaClientes = new JFrame("Clientes");
private JButton btNovo = new JButton("Novo");
private JButton btExcluir = new JButton("Excluir");
private JMenuBar mnPrincipal = new JMenuBar();
private JMenu mnArquivo = new JMenu("Arquivo");
private JMenuItem itNovo = new JMenuItem("Novo");
private JMenuItem itExcluir = new JMenuItem("Excluir");
private JMenuItem itSair = new JMenuItem("Sair");
public FmClientes(){
this.janelaClientes.setVisible(true);
this.janelaClientes.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.janelaClientes.setResizable(false);
this.janelaClientes.setLayout(new GridLayout(1,2));
this.janelaClientes.setJMenuBar(this.mnPrincipal);
this.mnPrincipal.add(this.mnArquivo);
this.mnArquivo.add(this.itNovo);
this.mnArquivo.add(this.itExcluir);
this.mnArquivo.add(this.itSair);
this.btNovo.addActionListener(this);
this.itNovo.addActionListener(this);
this.btExcluir.addActionListener(this);
this.itExcluir.addActionListener(this);
this.itSair.addActionListener(this);
this.janelaClientes.add(this.btNovo);
this.janelaClientes.add(this.btExcluir);
this.janelaClientes.pack();
}
public static void main(String[] args) {
new FmClientes();
}
@Override
public void actionPerformed(ActionEvent evt) {
if (evt.getSource() == this.itNovo || evt.getSource() == this.btNovo){
new FmInserirCadastro();
}else if(evt.getSource() == this.itExcluir || evt.getSource() == this.btExcluir){
new FmExcluirCadastro();
}else if(evt.getSource() == itSair){
System.exit(0);
}
}
}
FmExcluirCadastro.java
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class FmExcluirCadastro implements ActionListener{
private JFrame janela = new JFrame("Excluir Cadastro");
private JPanel pnBusca = new JPanel();
private JPanel pnResult = new JPanel();
private JLabel lbBuscar = new JLabel("Buscar: ");
private JTextField tfBusca = new JTextField();
private JButton btBuscar = new JButton("Ir");
private JButton btExcluir = new JButton("Excluir");
private JList ltClientes = new JList();
private DefaultListModel lmClientes = new DefaultListModel();
public FmExcluirCadastro(){
this.janela.setVisible(true);
this.janela.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.janela.setResizable(false);
this.janela.setLayout(new GridLayout(1,2));
this.tfBusca.setText(" ");
this.btBuscar.addActionListener(this);
this.pnBusca.add(this.lbBuscar);
this.pnBusca.add(this.tfBusca);
this.pnBusca.add(this.btBuscar);
this.pnResult.setLayout(new GridLayout(2,2));
this.pnResult.add(this.ltClientes);
this.pnResult.add(this.btExcluir);
this.janela.add(this.pnBusca);
this.janela.add(this.pnResult);
this.lmClientes.add(0,"Clientes:");
this.ltClientes.setModel(this.lmClientes);
this.janela.pack();
}
public static void main(String[] args) {
new FmExcluirCadastro();
}
@Override
public void actionPerformed(ActionEvent evt) {
if(evt.getSource() == this.btBuscar){
try {
ClienteDAO dao = new ClienteDAO();
ArrayList<Cliente> clientes = new ArrayList<Cliente>();
clientes = dao.buscar(this.tfBusca.getText());
for (Cliente cliente : clientes) {
this.lmClientes.add(0,cliente.getNome());
}
this.ltClientes.setModel(this.lmClientes);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
FmInserirCadastro.java
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextField;
public class FmInserirCadastro implements ActionListener{
private JFrame janela = new JFrame("Inserir Cadastro");
private JMenuBar menuBar =new JMenuBar();
private JMenu Principal = new JMenu("Principal");
private JMenuItem sair = new JMenuItem("Sair");
private JTextField tfNome = new JTextField();
private JTextField tfRg = new JTextField();
private JTextField tfNascimento = new JTextField();
private JComboBox cbSexo = new JComboBox();
private JButton btSalvar = new JButton("Salvar");
private JLabel lbNome = new JLabel("Nome: ");
private JLabel lbRg = new JLabel("RG: ");
private JLabel lbSexo = new JLabel("Sexo: ");
private JLabel lbNascimento = new JLabel("Data de Nascimento: ");
private Cliente novoCliente = new Cliente();
//construtor
public FmInserirCadastro(){
janela.setVisible(true);
janela.setDefaultCloseOperation(janela.DISPOSE_ON_CLOSE);
janela.setResizable(false);
this.Principal.add(this.sair);
this.menuBar.add(this.Principal);
janela.setJMenuBar(this.menuBar);
janela.setLayout(new GridLayout(5,2));
janela.add(this.lbNome);
janela.add(this.tfNome);
janela.add(this.lbRg);
janela.add(this.tfRg);
janela.add(this.lbSexo);
janela.add(this.cbSexo);
this.cbSexo.addItem("Masculino");
this.cbSexo.addItem("Feminino");
janela.add(this.lbNascimento);
janela.add(this.tfNascimento);
janela.add(this.btSalvar);
this.btSalvar.addActionListener(this);
this.sair.addActionListener(this);
janela.pack();
}
public static void main(String[] args) {
new FmInserirCadastro();
}
@Override
public void actionPerformed(ActionEvent evt) {
if (evt.getSource() == this.btSalvar){
//////verificar
if(!this.tfNome.getText().equals("") && !this.tfNascimento.getText().equals("") &&
!this.tfRg.getText().equals("")){
this.novoCliente.setNome(this.tfNome.getText());
this.novoCliente.setRG(this.tfRg.getText());
this.novoCliente.setDataNascimento(this.tfNascimento.getText());
String sexo = (String)this.cbSexo.getSelectedItem();
this.novoCliente.setSexo(sexo);
try{
ClienteDAO dao = new ClienteDAO();
dao.adiciona(novoCliente);
}catch(Exception e){
javax.swing.JOptionPane.showMessageDialog(null, "Erro ao inserir no banco de dados");
}
}else{
javax.swing.JOptionPane.showMessageDialog(null,"Todos os campos devem ser preenchidos!");
}
}else if(evt.getSource() == this.sair){
this.janela.dispose();
}
}
}
ConectionFactory.java
import java.sql.*;
public class ConnectionFactory {
public static Connection getConnection() throws Exception{
try {
Class.forName ("org.postgresql.Driver").newInstance();
return DriverManager.getConnection("jdbc:postgresql://localhost:5432/java_clientes","java", "java");
} catch (ClassNotFoundException e) {
throw new SQLException(e.getMessage());
}
}
}