使用向量jswing获取正确的表标题

时间:2014-04-18 20:41:24

标签: java swing vector derby

我有一个表格,显示netbeans中数据库中的所有信息。该表与滚动条和信息完美地显示,但标题完全相同。数据库表中的第五个元素。因此,名称,类型,价格......这些都是示例2,示例2 ...

我无法弄清楚原因。我觉得如果矢量概念能够填充所有信息,那么它也应该用来填写标题。

感谢您的帮助。

/*
 * 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;

    }
}

1 个答案:

答案 0 :(得分:2)

这看起来不正确。您每次迭代都使用columnCount

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

你可能意味着这个:

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

这将说明您看到错误的标题名称的原因。