Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.37 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. /*
  7.  * JanelaClientes.java
  8.  *
  9.  * Created on 8/Fev/2011, 15:58:05
  10.  */
  11. package basedados1;
  12.  
  13. import java.sql.SQLException;
  14. import java.util.Vector;
  15. import javax.swing.table.DefaultTableModel;
  16.  
  17. /**
  18.  *
  19.  * @author rui
  20.  */
  21. public class JanelaClientes extends javax.swing.JFrame {
  22.  
  23.     /** Creates new form JanelaClientes */
  24.     public JanelaClientes() {
  25.         initComponents();
  26.  
  27.         //Vector registos1 = new Vector();
  28.  
  29.         //registos1.add("teste");
  30.         //registos1.add("12345");
  31.         //registos1.add("42525");
  32.         //registos1.add("229299");
  33.  
  34.         this.carregarTabela();
  35.  
  36.  
  37.     }
  38.  
  39.     public void carregarTabela() {
  40.  
  41.         java.sql.Connection con;
  42.  
  43. //fazer a ligação ao mysql
  44.  
  45.         try {
  46.             Class.forName("org.gjt.mm.mysql.Driver");
  47.             con = java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "suzana13");
  48.  
  49.         } catch (Exception e) {
  50.             e.printStackTrace();
  51.             throw new java.lang.RuntimeException("erro ao ligar");
  52.  
  53.         }
  54.  
  55.         String comando = "select * from Cliente";
  56.  
  57.         Vector<Vector> dados = new Vector<Vector>();
  58.  
  59.         try {
  60.             java.sql.Statement stmt = con.createStatement();
  61.             java.sql.ResultSet rs = stmt.executeQuery(comando);
  62.             while (rs.next()) {
  63.  
  64.                 //criar um vector para cada registo lido
  65.  
  66.                 //Vector registoActual = new Vector<String>()
  67.                 //Vector registo  da base de dados
  68.                 int codigo = rs.getInt("codigo");
  69.                 String nome = rs.getString("nome");
  70.                 String telefone = rs.getString("telefone");
  71.                 String rg = rs.getString("rg");
  72.                 String cpf = rs.getString("cpf");
  73.  
  74.                 Vector registosLidos = new Vector();
  75.  
  76.  
  77.                 registosLidos.add(codigo);
  78.                 registosLidos.add(nome);
  79.                 registosLidos.add(telefone);
  80.                 registosLidos.add(rg);
  81.                 registosLidos.add(cpf);
  82.                 //adiciona os registos ao vector
  83.                 dados.add(registosLidos);
  84.  
  85.             }
  86.  
  87.  
  88.             stmt.close();
  89.             con.close();
  90.         } catch (java.sql.SQLException e) {
  91.             throw new java.lang.RuntimeException(e.getMessage());
  92.  
  93.         }
  94.         try {
  95.             con.close();
  96.  
  97.         } catch (Exception e) {
  98.             e.printStackTrace();
  99.             throw new java.lang.RuntimeException("erro ao fechar");
  100.         }
  101.         //cast
  102.         DefaultTableModel dtm = (DefaultTableModel) this.jTable1.getModel();
  103.  
  104.  
  105.         //percorre todos os elementos
  106.         for (int i = 0; i < dados.size(); i++) {
  107.             dtm.addRow(dados.get(i));
  108.         }
  109.  
  110.     }
  111.  
  112.     /** This method is called from within the constructor to
  113.      * initialize the form.
  114.      * WARNING: Do NOT modify this code. The content of this method is
  115.      * always regenerated by the Form Editor.
  116.      */
  117.     @SuppressWarnings("unchecked")
  118.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  119.     private void initComponents() {
  120.  
  121.         jButton1 = new javax.swing.JButton();
  122.         jScrollPane1 = new javax.swing.JScrollPane();
  123.         jTable1 = new javax.swing.JTable();
  124.         jButton2 = new javax.swing.JButton();
  125.         jButton3 = new javax.swing.JButton();
  126.  
  127.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  128.  
  129.         jButton1.setText("Adicionar");
  130.         jButton1.addActionListener(new java.awt.event.ActionListener() {
  131.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  132.                 jButton1ActionPerformed(evt);
  133.             }
  134.         });
  135.  
  136.         jTable1.setModel(new javax.swing.table.DefaultTableModel(
  137.             new Object [][] {
  138.  
  139.             },
  140.             new String [] {
  141.                 "Codigo", "Nome", "Telefone", "rg", "cpf"
  142.             }
  143.         ));
  144.         jScrollPane1.setViewportView(jTable1);
  145.  
  146.         jButton2.setText("Alterar");
  147.         jButton2.addActionListener(new java.awt.event.ActionListener() {
  148.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  149.                 jButton2ActionPerformed(evt);
  150.             }
  151.         });
  152.  
  153.         jButton3.setText("Apagar");
  154.         jButton3.addActionListener(new java.awt.event.ActionListener() {
  155.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  156.                 jButton3ActionPerformed(evt);
  157.             }
  158.         });
  159.  
  160.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  161.         getContentPane().setLayout(layout);
  162.         layout.setHorizontalGroup(
  163.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  164.             .addGroup(layout.createSequentialGroup()
  165.                 .addContainerGap()
  166.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  167.                     .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
  168.                     .addGroup(layout.createSequentialGroup()
  169.                         .addComponent(jButton1)
  170.                         .addGap(18, 18, 18)
  171.                         .addComponent(jButton2)
  172.                         .addGap(18, 18, 18)
  173.                         .addComponent(jButton3)))
  174.                 .addContainerGap(13, Short.MAX_VALUE))
  175.         );
  176.         layout.setVerticalGroup(
  177.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  178.             .addGroup(layout.createSequentialGroup()
  179.                 .addContainerGap()
  180.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  181.                     .addComponent(jButton1)
  182.                     .addComponent(jButton2)
  183.                     .addComponent(jButton3))
  184.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  185.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 288, javax.swing.GroupLayout.PREFERRED_SIZE)
  186.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  187.         );
  188.  
  189.         pack();
  190.     }// </editor-fold>                        
  191.  
  192.     private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  193.         // TODO add your handling code here:
  194.  
  195.  
  196.         this.carregarTabela();
  197.         int linhaSelecionada = this.jTable1.getSelectedRow();
  198.         String codigo = this.jTable1.getValueAt(linhaSelecionada, 0).toString();
  199.         String nome = this.jTable1.getValueAt(linhaSelecionada, 1).toString();
  200.         String telefone = this.jTable1.getValueAt(linhaSelecionada, 2).toString();
  201.         String rg = this.jTable1.getValueAt(linhaSelecionada, 3).toString();
  202.         String cpf = this.jTable1.getValueAt(linhaSelecionada, 4).toString();
  203.  
  204.         new JanelaAlterar(codigo, nome, telefone, rg, cpf).setVisible(true);
  205. //refresca a tabela
  206.        
  207.     }                                        
  208.  
  209.     private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  210.         // TODO add your handling code here:
  211.         int linha = this.jTable1.getSelectedRow();
  212.         String codigo = this.jTable1.getValueAt(linha, 0).toString();
  213. new JanelaApaga(codigo).setVisible(true);
  214.     }                                        
  215.  
  216.     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  217.         // TODO add your handling code here:
  218.         new JanelaNovoCliente().setVisible(true);
  219.     }                                        
  220.  
  221.     /**
  222.      * @param args the command line arguments
  223.      */
  224.     public static void main(String args[]) {
  225.         java.awt.EventQueue.invokeLater(new Runnable() {
  226.  
  227.             public void run() {
  228.                 new JanelaClientes().setVisible(true);
  229.             }
  230.         });
  231.     }
  232.     // Variables declaration - do not modify                    
  233.     private javax.swing.JButton jButton1;
  234.     private javax.swing.JButton jButton2;
  235.     private javax.swing.JButton jButton3;
  236.     private javax.swing.JScrollPane jScrollPane1;
  237.     private javax.swing.JTable jTable1;
  238.     // End of variables declaration                  
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement