执行jtable显示异常

时间:2017-01-05 17:13:25

标签: jdbc jtable resultset defaulttablemodel

我正在尝试开发一个基于jtable的JDBC程序,该程序创建一个与database.i中存在的表相关的表。我创建了2个方法作为databaseHandler()和graphicsHandler()来处理数据库和图形相关的指令.foldata和rowdata包含列名和resultSet rowdata.But当使用这些值绘制表时,我面临NullPointerException但我无法理解为什么。任何人的帮助都非常感谢。谢谢。

import java.awt.BorderLayout;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class Final {
    int columnno,rowno;
    Connection connection;
    PreparedStatement preparedStatement;
    ResultSet resultSet;
    ResultSetMetaData metaData;
    Vector<String> coldata;
    Vector<Object> rowdata,temp;

    DefaultTableModel tableModel;
    JScrollPane scrollPane;
    JTable table;

    public Final() {
        // TODO Auto-generated constructor stub
        ValueHolder holder = DatabaseHandler();
        GraphicsHandler(holder.getCd(),holder.getRd());
    }
    private void GraphicsHandler(Vector<String> coldata, Vector<Object> rowdata) {
        // TODO Auto-generated method stub
        JFrame frame = new JFrame();
        frame.setLayout(new BorderLayout(0, 20));  //setting up horizontal and vertical gap
        scrollPane.setViewportView(table);
        scrollPane.setWheelScrollingEnabled(true);
        frame.add(scrollPane, BorderLayout.CENTER);
        tableModel = new DefaultTableModel(rowdata,coldata);
        table = new JTable(tableModel);

        frame.setVisible(true);
        frame.pack();
    }
    private ValueHolder DatabaseHandler() {
        // TODO Auto-generated method stub
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/company?user=username&password=password";
        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(url);
            preparedStatement = connection.prepareStatement("select * from product");
            resultSet = preparedStatement.executeQuery();
            metaData = resultSet.getMetaData();
            columnno = metaData.getColumnCount();
            rowno = 1;
            coldata = new Vector<String>();
            for (int i = 1; i <= columnno; i++) {
                coldata.add(metaData.getColumnLabel(i));
            }
            System.out.println();
            rowdata = new Vector<Object>() ; temp = new Vector<Object>();
            while (resultSet.next()) {
                for (int i = 1; i <= columnno; i++) {
                    temp.add(resultSet.getString(i));
                }
                rowdata.add(temp.clone());
                temp.clear();
                rowno++;
            }
        } catch (ClassNotFoundException | SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return new ValueHolder(rowdata, coldata);   
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new Final();
    }

}
class ValueHolder {

    Vector<Object> rd; Vector<String> cd;
    public ValueHolder(Vector<Object> rowdata, Vector<String> coldata) {
        this.rd = rowdata;
        this.cd = coldata;
    }
    public Vector<Object> getRd() {
        return rd;
    }

    public Vector<String> getCd() {
        return cd;
    }

}

1 个答案:

答案 0 :(得分:1)

我只是通过扩展javax.swing.table.AbstractTableModel并添加了对数据库运行查询的功能。一旦这样工作正常,您可以在Swing项目中使用JTable。希望这会有所帮助。

return {
  'a' : {},
  'b' : 'string',
  'c' : 2,
  'd' : false,
}