Pregunta

I have a table which displays all the information in a database in netbeans. The table displays perfectly with the scroll bars and information, but the headings are all the same thing. The fifth element in the database table. So insead of the tble heading being name, type, price...they all say example2, example2...

I can't figure out why. I feel like if the vector concept work to fill all of the information in, it should work to fill in the titles as well.

Thanks for any help.

/*
 * FullDatabaseTable.java
 *
 * 
 */
package brainstormer95;

import java.awt.Color;
import java.awt.Dimension;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

/**
 *
 * @author ME
 */
public class FullDatabaseTable extends JPanel{
    Vector stringInfo;

    /** Creates new form FullDatabaseTable */
    public FullDatabaseTable() throws SQLException {

        //initComponents();
    }

    public void createTable() throws SQLException {
        Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/brainstormer", "name", "name");
        Statement stmt = con.createStatement();
        PreparedStatement psmt = con.prepareStatement(" SELECT ITEM_NAME,SPECIFIC_PURPOSE,EXAMPLE_1,EXAMPLE_2 FROM APP.INVENTORY");
        ResultSet rs = psmt.executeQuery();

        for (int i=1;i<=rs.getRow();i++){
        String tableTitle = (rs.getMetaData().getTableName(i));
        JLabel title = new JLabel(tableTitle);
        }

        int columnCount = rs.getMetaData().getColumnCount();
        Vector columns = new Vector(columnCount);

        for (int i = 1; i <= columnCount; i++) {
            columns.add(rs.getMetaData().getColumnName(columnCount));
        }

        Vector stringVector = new Vector();
        while (rs.next()) {
            stringInfo = new Vector(columnCount);
            for (int i = 1; i <= columnCount; i++) {
                stringInfo.add(rs.getString(i));
            }
            stringVector.add(stringInfo);
        }

        //Format
        JScrollPane tableScrollPane;
        Dimension tableSize = new Dimension();
        tableSize.setSize(1400,800);
        Dimension scrollSize = new Dimension();
        scrollSize.setSize(1000,600);
        JTable table = new JTable(stringVector,columns);
        table.setPreferredSize(tableSize);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        tableScrollPane = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        tableScrollPane.setPreferredSize(scrollSize);
        table.setShowGrid(true);
        table.setGridColor(Color.BLACK);
        table.setEnabled(false);
        JPanel jPanel = new JPanel();
        jPanel.add(tableScrollPane);
        add(jPanel);
        setVisible(true);
        setBackground(Color.white);

    }
     public JComponent returnGUI() {
        return this;

    }
}
¿Fue útil?

Solución

This doesn't look correct. You are using columnCount each iteration.

   for (int i = 1; i <= columnCount; i++) {
        columns.add(rs.getMetaData().getColumnName(columnCount));
    }

You probably meant this:

   for (int i = 1; i <= columnCount; i++) {
        columns.add(rs.getMetaData().getColumnName(i));
    }

This would account for why you are seeing the wrong header names.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top