Bom galera, eu sou novo aqui no GUJ e também em java, consegui fazer um cadastro de clientes que antes era em jsp e migrei ele para Java Desktop. É um sistema simples e gostaria da ajuda de vocês para alguma melhora no meu programa. Segue o Código das classes:
[size=15]Classe DAO do pacote persistence[/size]
package persistence;
import java.sql.*;
public class DAO {
Connection con;
PreparedStatement pstmt;
ResultSet rs;
public void OpenDatabase()throws Exception{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/banco";
String user = "root";
String password = "123";
con = DriverManager.getConnection(url, user, password);
}
public void CloseDatabase()throws Exception{
if(con !=null) {
con.close();
}
}
}
[size=18]Classe ClienteDAO do Pacote persistence[/size]
package persistence;
import model.Cliente;
import java.util.ArrayList;
import java.util.List;
public class ClienteDAO extends DAO{
public void cadastrar(Cliente c)throws Exception{
OpenDatabase();
String SQL = "INSERT INTO tbl_cliente VALUES(?,?,?,?,?)";
pstmt = con.prepareStatement(SQL);
pstmt.setInt(1, c.getCod_cliente());
pstmt.setString(2, c.getNome());
pstmt.setString(3, c.getTelefone());
pstmt.setString(4, c.getEmail());
pstmt.setString(5, c.getSite());
pstmt.execute();
CloseDatabase();
}
public Cliente buscaporId(Integer id)throws Exception{
OpenDatabase();
String SQL = "SELECT * FROM tbl_cliente WHERE cod_cliente=?";
pstmt = con.prepareStatement(SQL);
pstmt.setInt(1, id);
rs = pstmt.executeQuery();
Cliente c = new Cliente();
if(rs.next())
c.setCod_cliente(rs.getInt("cod_cliente"));
c.setNome(rs.getString("nome"));
c.setTelefone(rs.getString("telefone"));
c.setEmail(rs.getString("email"));
c.setSite(rs.getString("site"));
return c;
}
public void delete(Cliente c)throws Exception{
OpenDatabase();
String SQL = "DELETE from tbl_cliente WHERE cod_cliente=?";
pstmt = con.prepareStatement(SQL);
pstmt.setInt(1, c.getCod_cliente());
pstmt.execute();
}
public List selecionar(Cliente c)throws Exception{
OpenDatabase();
String SQL = "SELECT * FROM tbl_cliente WHERE cod_cliente = "+c.getCod_cliente();
pstmt = con.prepareStatement(SQL);
rs = pstmt.executeQuery();
List<Cliente> listaClientes = new ArrayList<Cliente>();
while(rs.next()){
c = new Cliente();
c.setCod_cliente(rs.getInt("cod_cliente"));
c.setNome(rs.getString("nome"));
c.setTelefone(rs.getString("telefone"));
c.setEmail(rs.getString("email"));
c.setSite(rs.getString("site"));
listaClientes.add(c);
}
rs.close();
pstmt.close();
return listaClientes;
}
public void update(Cliente c)throws Exception{
OpenDatabase();
String SQL = "UPDATE tbl_cliente SET nome=?, telefone=?, email=?, site=?" +
" where cod_cliente=?";
pstmt = con.prepareStatement(SQL);
pstmt.setString(1, c.getNome());
pstmt.setString(2, c.getTelefone());
pstmt.setString(3, c.getEmail());
pstmt.setString(4, c.getSite());
pstmt.setInt(5, c.getCod_cliente());
pstmt.executeUpdate();
}
}
[size=15]Classe Cliente do Pacote model[/size]
package model;
public class Cliente {
private Integer cod_cliente;
private String nome;
private String telefone;
private String email;
private String site;
public Integer getCod_cliente() {
return cod_cliente;
}
public void setCod_cliente(Integer cod_cliente) {
this.cod_cliente = cod_cliente;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getSite() {
return site;
}
public void setSite(String site) {
this.site = site;
}
public String getTelefone() {
return telefone;
}
public void setTelefone(String telefone) {
this.telefone = telefone;
}
}
[size=15]Classe Principal do Pacote view [/size]
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package view;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
/**
*
* @author Renan
*/
public class Principal extends javax.swing.JFrame {
Connection con;
PreparedStatement pstmt;
ResultSet rs;
/**
* Creates new form Principal
*/
public Principal() {
initComponents();
setLocationRelativeTo(null);
}
public void OpenDatabase()throws Exception{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/banco";
String user = "root";
String password = "123";
con = DriverManager.getConnection(url, user, password);
}
public void CloseDatabase()throws Exception{
if(con !=null) {
con.close();
}
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jPanel2 = new javax.swing.JPanel();
bttestar = new javax.swing.JButton();
conexao = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jSeparator1 = new javax.swing.JPopupMenu.Separator();
Atualizar = new javax.swing.JMenuItem();
jSeparator2 = new javax.swing.JPopupMenu.Separator();
Pesquisar = new javax.swing.JMenuItem();
jSeparator3 = new javax.swing.JPopupMenu.Separator();
Excluir = new javax.swing.JMenuItem();
jSeparator4 = new javax.swing.JPopupMenu.Separator();
jMenuItem4 = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
jMenuItem2 = new javax.swing.JMenuItem();
jMenuItem3 = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Cadastro de Clientes");
setBackground(new java.awt.Color(255, 51, 51));
setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Cadastro de Clientes"));
jPanel1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Conexão com Banco de Dados", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 0, 10), java.awt.Color.darkGray)); // NOI18N
bttestar.setText("Testar");
bttestar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bttestarActionPerformed(evt);
}
});
conexao.setForeground(new java.awt.Color(255, 0, 0));
conexao.setText(" ");
conexao.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
conexao.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(bttestar)
.addGap(54, 54, 54)
.addComponent(conexao, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 14, Short.MAX_VALUE))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(31, 31, 31)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(bttestar)
.addComponent(conexao, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(48, Short.MAX_VALUE))
);
jLabel1.setForeground(new java.awt.Color(0, 0, 204));
jLabel1.setText(" ");
jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/view/img/sair.png"))); // NOI18N
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 314, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(44, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(32, 32, 32)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 49, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(23, 23, 23))))
);
jMenu1.setText("Menu");
jMenu1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenu1ActionPerformed(evt);
}
});
jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, 0));
jMenuItem1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/view/img/cadastra.png"))); // NOI18N
jMenuItem1.setText("Cadastrar");
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem1ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem1);
jMenu1.add(jSeparator1);
Atualizar.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, 0));
Atualizar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/view/img/atualiza.png"))); // NOI18N
Atualizar.setText("Atualizar");
Atualizar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
AtualizarActionPerformed(evt);
}
});
jMenu1.add(Atualizar);
jMenu1.add(jSeparator2);
Pesquisar.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_P, 0));
Pesquisar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/view/img/pesquisar_peq.gif"))); // NOI18N
Pesquisar.setText("Pesquisar");
Pesquisar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
PesquisarActionPerformed(evt);
}
});
jMenu1.add(Pesquisar);
jMenu1.add(jSeparator3);
Excluir.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_E, 0));
Excluir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/view/img/excluir.gif"))); // NOI18N
Excluir.setText("Excluir");
Excluir.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ExcluirActionPerformed(evt);
}
});
jMenu1.add(Excluir);
jMenu1.add(jSeparator4);
jMenuItem4.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, 0));
jMenuItem4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/view/img/sair.png"))); // NOI18N
jMenuItem4.setText("Sair");
jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem4ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem4);
jMenuBar1.add(jMenu1);
jMenu2.setText("Ajuda");
jMenu2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenu2ActionPerformed(evt);
}
});
jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_H, 0));
jMenuItem2.setText("Ajuda do Software");
jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem2ActionPerformed(evt);
}
});
jMenu2.add(jMenuItem2);
jMenuItem3.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_J, 0));
jMenuItem3.setText("Remover Ajuda");
jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem3ActionPerformed(evt);
}
});
jMenu2.add(jMenuItem3);
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
pack();
}// </editor-fold>
private void bttestarActionPerformed(java.awt.event.ActionEvent evt) {
try {
OpenDatabase();
conexao.setText(" Conectado ao Banco!");
} catch (Exception ex) {
conexao.setText(" Erro ao conectar-se!");
}
}
private void jMenu2ActionPerformed(java.awt.event.ActionEvent evt) {
}
private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {
jLabel1.setText("Clique no Menu para realizar as operações do Software!");
}
private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {
jLabel1.setText("");
}
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
new Cadastrar().setVisible(true);
}
private void AtualizarActionPerformed(java.awt.event.ActionEvent evt) {
new Atualizar().setVisible(true);
}
private void PesquisarActionPerformed(java.awt.event.ActionEvent evt) {
new Pesquisar().setVisible(true);
}
private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {
this.dispose();
}
private void ExcluirActionPerformed(java.awt.event.ActionEvent evt) {
new Excluir().setVisible(true);
}
private void jMenu1ActionPerformed(java.awt.event.ActionEvent evt) {
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
this.dispose();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Principal().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JMenuItem Atualizar;
private javax.swing.JMenuItem Excluir;
private javax.swing.JMenuItem Pesquisar;
private javax.swing.JButton bttestar;
private javax.swing.JLabel conexao;
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JMenuItem jMenuItem3;
private javax.swing.JMenuItem jMenuItem4;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPopupMenu.Separator jSeparator1;
private javax.swing.JPopupMenu.Separator jSeparator2;
private javax.swing.JPopupMenu.Separator jSeparator3;
private javax.swing.JPopupMenu.Separator jSeparator4;
// End of variables declaration
}
[size=15]Classe Cadastrar do pacote view [/size]
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package view;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import persistence.ClienteDAO;
import model.Cliente;
/**
*
* @author Renan
*/
public class Cadastrar extends javax.swing.JFrame {
/**
* Creates new form Cadastrar
*/
public Cadastrar() {
initComponents();
setLocationRelativeTo(null);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
txtnome = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
txttelefone = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
txtemail = new javax.swing.JTextField();
jLabel4 = new javax.swing.JLabel();
txtsite = new javax.swing.JTextField();
btcadastrar = new javax.swing.JButton();
btlimpar = new javax.swing.JButton();
btsair = new javax.swing.JButton();
jLabel5 = new javax.swing.JLabel();
txtcodigo = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Cadastrar Cliente"));
jLabel1.setText("Nome:");
jLabel2.setText("Telefone:");
jLabel3.setText("Email:");
jLabel4.setText("Site:");
btcadastrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/view/img/cadastra.png"))); // NOI18N
btcadastrar.setText("Cadastrar");
btcadastrar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btcadastrarActionPerformed(evt);
}
});
btlimpar.setIcon(new javax.swing.ImageIcon("C:\\Users\\Renan\\Downloads\\limpar.gif")); // NOI18N
btlimpar.setText("Limpar");
btlimpar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btlimparActionPerformed(evt);
}
});
btsair.setIcon(new javax.swing.ImageIcon(getClass().getResource("/view/img/sair.png"))); // NOI18N
btsair.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btsairActionPerformed(evt);
}
});
jLabel5.setText("Código:");
org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel1Layout.createSequentialGroup()
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
.add(jPanel1Layout.createSequentialGroup()
.add(jLabel5)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(txtcodigo))
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
.add(txtemail)
.add(jLabel1)
.add(txtnome)
.add(jLabel3)
.add(btcadastrar, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.add(49, 49, 49)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
.add(jLabel4)
.add(jLabel2)
.add(txttelefone)
.add(txtsite)
.add(btlimpar, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE))
.add(0, 29, Short.MAX_VALUE))
.add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
.add(0, 0, Short.MAX_VALUE)
.add(btsair, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 34, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel1Layout.createSequentialGroup()
.add(20, 20, 20)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jLabel1)
.add(jLabel2))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(txtnome, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(txttelefone, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(37, 37, 37)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jLabel3)
.add(jLabel4))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(txtemail, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(txtsite, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(18, 18, 18)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jLabel5)
.add(txtcodigo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 21, Short.MAX_VALUE)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(btcadastrar)
.add(btlimpar))
.add(22, 22, 22)
.add(btsair))
);
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>
private void btsairActionPerformed(java.awt.event.ActionEvent evt) {
this.dispose();
}
private void btlimparActionPerformed(java.awt.event.ActionEvent evt) {
//Limpar Campos
txtnome.setText("");
txtemail.setText("");
txttelefone.setText("");
txtsite.setText("");
}
private void btcadastrarActionPerformed(java.awt.event.ActionEvent evt) {
//Validação dos campos
if (txtnome.getText().trim().equals("") || txttelefone.getText().trim().equals("") || txtemail.getText().trim().equals("") || txtsite.getText().trim().equals("")) {
JOptionPane.showMessageDialog(null, "Não pode inserir campos vazios!");
} else {//inicio do else
try {
//CRIACAO DOS OBJETOS:
Cliente c = new Cliente();
ClienteDAO dc = new ClienteDAO();
//ENVIA DADOS PARA MINHA CLASSE CADASTRO
c.setCod_cliente(Integer.parseInt(txtcodigo.getText()));
c.setNome(txtnome.getText());
c.setTelefone(txttelefone.getText());
c.setEmail(txtemail.getText());
c.setSite(txtsite.getText());
try {
//COMANDO QUE ADD DADOS NA TABELA:
dc.cadastrar(c);
} catch (Exception ex) {//Se der erro na inserção
Logger.getLogger(Cadastrar.class.getName()).log(Level.SEVERE, null, ex);
}
//SE NAO OCORRER NENHUM ERRO A SEGUINTE MENSAGEM E EXIBIDA:
JOptionPane.showMessageDialog(null, "Registro Salvo com sucesso!");
//Fecha a Janela
setVisible(false);
} catch (Exception e) {
}
}//Fim do Else
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Cadastrar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Cadastrar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Cadastrar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Cadastrar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Cadastrar().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btcadastrar;
private javax.swing.JButton btlimpar;
private javax.swing.JButton btsair;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JPanel jPanel1;
private javax.swing.JTextField txtcodigo;
private javax.swing.JTextField txtemail;
private javax.swing.JTextField txtnome;
private javax.swing.JTextField txtsite;
private javax.swing.JTextField txttelefone;
// End of variables declaration
}