Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- /*
- * JanelaClientes.java
- *
- * Created on 8/Fev/2011, 15:58:05
- */
- package basedados1;
- import java.sql.SQLException;
- import java.util.Vector;
- import javax.swing.table.DefaultTableModel;
- /**
- *
- * @author rui
- */
- public class JanelaClientes extends javax.swing.JFrame {
- /** Creates new form JanelaClientes */
- public JanelaClientes() {
- initComponents();
- //Vector registos1 = new Vector();
- //registos1.add("teste");
- //registos1.add("12345");
- //registos1.add("42525");
- //registos1.add("229299");
- this.carregarTabela();
- }
- public void carregarTabela() {
- java.sql.Connection con;
- //fazer a ligação ao mysql
- try {
- Class.forName("org.gjt.mm.mysql.Driver");
- con = java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "suzana13");
- } catch (Exception e) {
- e.printStackTrace();
- throw new java.lang.RuntimeException("erro ao ligar");
- }
- String comando = "select * from Cliente";
- Vector<Vector> dados = new Vector<Vector>();
- try {
- java.sql.Statement stmt = con.createStatement();
- java.sql.ResultSet rs = stmt.executeQuery(comando);
- while (rs.next()) {
- //criar um vector para cada registo lido
- //Vector registoActual = new Vector<String>()
- //Vector registo da base de dados
- int codigo = rs.getInt("codigo");
- String nome = rs.getString("nome");
- String telefone = rs.getString("telefone");
- String rg = rs.getString("rg");
- String cpf = rs.getString("cpf");
- Vector registosLidos = new Vector();
- registosLidos.add(codigo);
- registosLidos.add(nome);
- registosLidos.add(telefone);
- registosLidos.add(rg);
- registosLidos.add(cpf);
- //adiciona os registos ao vector
- dados.add(registosLidos);
- }
- stmt.close();
- con.close();
- } catch (java.sql.SQLException e) {
- throw new java.lang.RuntimeException(e.getMessage());
- }
- try {
- con.close();
- } catch (Exception e) {
- e.printStackTrace();
- throw new java.lang.RuntimeException("erro ao fechar");
- }
- //cast
- DefaultTableModel dtm = (DefaultTableModel) this.jTable1.getModel();
- //percorre todos os elementos
- for (int i = 0; i < dados.size(); i++) {
- dtm.addRow(dados.get(i));
- }
- }
- /** 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() {
- jButton1 = new javax.swing.JButton();
- jScrollPane1 = new javax.swing.JScrollPane();
- jTable1 = new javax.swing.JTable();
- jButton2 = new javax.swing.JButton();
- jButton3 = new javax.swing.JButton();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- jButton1.setText("Adicionar");
- jButton1.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton1ActionPerformed(evt);
- }
- });
- jTable1.setModel(new javax.swing.table.DefaultTableModel(
- new Object [][] {
- },
- new String [] {
- "Codigo", "Nome", "Telefone", "rg", "cpf"
- }
- ));
- jScrollPane1.setViewportView(jTable1);
- jButton2.setText("Alterar");
- jButton2.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton2ActionPerformed(evt);
- }
- });
- jButton3.setText("Apagar");
- jButton3.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton3ActionPerformed(evt);
- }
- });
- 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()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGroup(layout.createSequentialGroup()
- .addComponent(jButton1)
- .addGap(18, 18, 18)
- .addComponent(jButton2)
- .addGap(18, 18, 18)
- .addComponent(jButton3)))
- .addContainerGap(13, Short.MAX_VALUE))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jButton1)
- .addComponent(jButton2)
- .addComponent(jButton3))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 288, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- );
- pack();
- }// </editor-fold>
- private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- this.carregarTabela();
- int linhaSelecionada = this.jTable1.getSelectedRow();
- String codigo = this.jTable1.getValueAt(linhaSelecionada, 0).toString();
- String nome = this.jTable1.getValueAt(linhaSelecionada, 1).toString();
- String telefone = this.jTable1.getValueAt(linhaSelecionada, 2).toString();
- String rg = this.jTable1.getValueAt(linhaSelecionada, 3).toString();
- String cpf = this.jTable1.getValueAt(linhaSelecionada, 4).toString();
- new JanelaAlterar(codigo, nome, telefone, rg, cpf).setVisible(true);
- //refresca a tabela
- }
- private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- int linha = this.jTable1.getSelectedRow();
- String codigo = this.jTable1.getValueAt(linha, 0).toString();
- new JanelaApaga(codigo).setVisible(true);
- }
- private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- new JanelaNovoCliente().setVisible(true);
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new JanelaClientes().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JButton jButton1;
- private javax.swing.JButton jButton2;
- private javax.swing.JButton jButton3;
- private javax.swing.JScrollPane jScrollPane1;
- private javax.swing.JTable jTable1;
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement