Victor Mendoza
Victor Mendoza

Reputation: 35

JTable displaying the same row from Mysql table

I'm showing the method were the JTable is constructed, the error is when adding the rows inside the for (int i = 1; i <= numero_columnas; i++) loop, or the way the DefaultTableModel model = new DefaultTableModel(); is declared, I can't find the error.


public void verTablaTable (Connection db, String nombre) throws Exception{
    Statement stmt=db.createStatement();
    ResultSet sst_ResultSet = stmt.executeQuery("SELECT * FROM "+nombre);
    ResultSetMetaData md = sst_ResultSet.getMetaData();
    int numero_columnas = md.getColumnCount();
    DefaultTableModel model = new DefaultTableModel();
    for (int i=1;i<=numero_columnas; i++){
        model.addColumn(md.getColumnName(i));
    }
    JTable tabla =new JTable(model);
    DefaultTableModel model1 = (DefaultTableModel) tabla.getModel();
    Vector row = new Vector();
    row.setSize(numero_columnas);
    while (sst_ResultSet.next()){
        for (int i = 1; i <= numero_columnas; i++){
            row.set(i-1,sst_ResultSet.getString(i));
        }
        model1.addRow(row);
    }
    tabla.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    JScrollPane sp_vertabla = new JScrollPane(tabla);
    sp_vertabla.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    sp_vertabla.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    sp_vertabla.setBounds(50,30,700,500);
    JPanel cont_vertabla = new JPanel(null);
    cont_vertabla.setPreferredSize(new Dimension(750,600));
    cont_vertabla.add(sp_vertabla);
    f_vertabla.setContentPane(cont_vertabla);
    f_vertabla.pack();
    f_vertabla.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    //f_vertabla.setResizable(false);
    f_vertabla.setVisible(true);
    f_vertabla.addWindowListener(this);
}

this is the way the JTable looks JTable displaying the same row


The row listed in the above pic, is the last one in the mysql table

Upvotes: 0

Views: 70

Answers (1)

Parasu
Parasu

Reputation: 192

Try adding the line

Vector row = new Vector();

inside the while loop.

Upvotes: 2

Related Questions