JAVA - 使用数据库中的Vector结果集填充JTable

时间:2015-09-27 21:10:35

标签: java sql vector jtable

我正在尝试使该程序打印一个包含从我的SQL数据库中获取的数据的表。我试图运行它时得到NullPointerException。

我有两个类 - 数据库访问类中的GUI和我的方法。

GUI代码(完整)。

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import t3.isprojekt.uppg2.controller.Controller;
import t3.isprojekt.uppg2.dal.DAL;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class View extends JFrame {
    private JFrame frame;
    private JPanel contentPane;
    private JTable table = new JTable();
    private DAL dal;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    View frame = new View();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     * 
     * @throws SQLException
     */
    public View() throws SQLException {

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 903, 701);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(10, 316, 867, 237);
        contentPane.add(scrollPane);

        DefaultTableModel dtm = new DefaultTableModel(fillTable(dal.getEmpPortalSetupData()),
                columnNames(dal.getEmpPortalSetupData()));
        table = new JTable(dtm);
        scrollPane.setViewportView(table);

        JButton btnGetAllEmployees = new JButton("Get data");
        btnGetAllEmployees.setBounds(616, 99, 176, 23);
        btnGetAllEmployees.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                table.setModel(dtm);

            }
        });

        contentPane.add(btnGetAllEmployees);

    }


    private Vector<Vector<String>> fillTable(ResultSet r) throws SQLException {
        ResultSetMetaData metaData = r.getMetaData();
        int columnCount = metaData.getColumnCount();

        Vector<Vector<String>> fillTable = new Vector<Vector<String>>();
        while (r.next()) {
            Vector<String> vector = new Vector<String>();
            for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
                vector.add(r.getString(columnIndex));
            }
            fillTable.add(vector);
        }
        return fillTable;
    }

    private Vector<String> columnNames(ResultSet r) throws SQLException {
        ResultSetMetaData metaData = r.getMetaData();

        Vector<String> columnNames = new Vector<String>();
        int columnCount = metaData.getColumnCount();

        for (int column = 1; column <= columnCount; column++) {
            columnNames.add(metaData.getColumnName(column));
        }
        return columnNames;
    }

}

数据访问层方法

public ResultSet getEmpPortalSetupData() throws SQLException {
    String getEmpPortalSetup = "SELECT [Entry No_], [Employee No_], [From Date], [To Date], [Description] from [CRONUS Sverige AB$Employee Portal Setup]";
    Statement stmt = null;
    stmt = getConn().createStatement();
    ResultSet rset = stmt.executeQuery(getEmpPortalSetup);
    return rset;
}


java.lang.NullPointerException
at View.<init>(View.java:63)
at View$1.run(View.java:36)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

第63行是导致错误的行:

        DefaultTableModel dtm = new DefaultTableModel(fillTable(dal.getEmpPortalSetupData()),
            columnNames(dal.getEmpPortalSetupData()));

0 个答案:

没有答案