Author Topic: Problema con Jtable  (Read 3303 times)

Leovidal

  • Newbie
  • *
  • Posts: 1
  • Karma: 0
    • View Profile
Problema con Jtable
« on: Agosto 12, 2010, 06:59:23 am »
Por favor, chequen mi codigo, realmente lo necesito, aqui les pongo el codigo, si lo suben corregido se los agredeceria como no tienen idea, gracias, al parecer es un objeto que no he inicializado, pero no se como hacerlo, revisen el .zip y modifiquenlo por favor......

Gracias de antemano


Estas son mis clases

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrameConsulta.java
 *
 * Created on 29/07/2010, 08:49:17 AM
 */

package visRSJtable2;

/**
 *
 * @author Administrador
 */
public class FrameConsulta extends javax.swing.JFrame {
private Conexion obj=null;
    /** Creates new form FrameConsulta */
    public FrameConsulta() {
        initComponents();
//        this.visTabla();
    }

    /** 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();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(153, 255, 204));

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 419, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(31, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 234, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(47, Short.MAX_VALUE))
        );

        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(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()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(47, 47, 47)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(72, 72, 72)
                        .addComponent(jButton1)))
                .addContainerGap(32, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                       

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        this.visTabla();
    }                                       

    /**
    * @param args the command line arguments
    */
    public void visTabla()
    {
        jPanel1.setVisible(true);
        jTable1.setModel(obj.consultarDatos());
    }
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FrameConsulta().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration                   

}




/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package visRSJtable2;

import java.sql.*;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Administrador
 */
public class Conexion {

    private Connection con;
    private PreparedStatement st;
//    private ResultSet resultado;

    public Conexion() {

        try {

            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/pruebaleo", "root", "12345");
//            st = con.prepareStatement("SELECT * FROM tbldatos");
//            rs1 = st.executeQuery();


        } catch (SQLException sqlException) {
            System.out.println(sqlException.getMessage() + "Error en la Base de Datos");
            System.exit(1);
        } catch (ClassNotFoundException classNotFound) {

            System.out.println(classNotFound.getMessage() + "Error, No se ha encontrado el Driver");
            System.exit(1);
        }
    }

  public DefaultTableModel consultarDatos() {
      System.out.println("Dentro de consultarDatos");
        DefaultTableModel tabla = new DefaultTableModel();
        ResultSet resultado = null;
        try {
            st = con.prepareStatement("SELECT * FROM costocombustoleo;");
            resultado = st.executeQuery();
            int NumColumn = resultado.getMetaData().getColumnCount();
            for (int i = 0; i < NumColumn; i++) {
                tabla.addColumn(resultado.getMetaData().getColumnName(i + 1));
            }
            while (resultado.next()) {
                Object[] datos = new Object[NumColumn];
                for (int row = 0; row < NumColumn; row++) {
                    datos[row] = resultado.getObject(row + 1);
                }
                tabla.addRow(datos);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tabla;
    }

}
« Last Edit: Agosto 16, 2010, 08:43:51 am by Leovidal »

 

ey