0

我试图用我的数据库表中的数据填充 java swing 中的 jTable。这是我设置连接的代码。

    package DBController.database;

    import java.sql.*;

    public class DBController {
private Connection con;

public void setUp(String dsn) {
    try {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");          
    } catch (Exception e) {
        System.out.println("Load driver error");
    }
    try {

        String s = "jdbc:odbc:" + dsn;
        con = DriverManager.getConnection(s, "", "");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public ResultSet readRequest(String dbQuery) {
    ResultSet rs = null;
    try {
        Statement stmt = con.createStatement();
        rs = stmt.executeQuery(dbQuery);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return rs;
}

public int updateRequest(String dbQuery) {
    int count = 0;
    try {
        Statement stmt = con.createStatement();
        count = stmt.executeUpdate(dbQuery);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return count;
}

public void terminate() {
    try {
        con.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

这是我尝试为 sql 语句编写的代码。

    public boolean retrieveDiscussion() {
    boolean success = false;
    ResultSet rs = null;
    DBController db = new DBController();
    db.setUp("IT Innovation Project");
    String dbQuery = "SELECT * FROM forumTopics WHERE topic_description = '" + description
            + "' and topic_category = '" + category
            + "' and topic_title = '" + title 
            + "' and topic_by = '" + postedBy + "'";
    rs = db.readRequest(dbQuery);
    db.terminate();
    return success;
}

这是我在用户界面中的表格的代码。

    public static void main(String[] args) throws Exception {
    // The Connection is obtained

    eForumTopics topics = new eForumTopics();
    topics.retrieveDiscussion();

    // It creates and displays the table
    JTable table = new JTable(buildTableModel(null));
    JOptionPane.showMessageDialog(null, new JScrollPane(table));

    // Closes the Connection
}
public static DefaultTableModel buildTableModel(ResultSet rs)
throws SQLException {

    ResultSetMetaData metaData = rs.getMetaData();

    // names of columns
    Vector<String> columnNames = new Vector<String>();
    int columnCount = metaData.getColumnCount();
    for (int column = 1; column <= columnCount; column++) {
        columnNames.add(metaData.getColumnName(column));
    }

    // data of the table
    Vector<Vector<Object>> data = new Vector<Vector<Object>>();
    while (rs.next()) {
        Vector<Object> vector = new Vector<Object>();
        for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
            vector.add(rs.getObject(columnIndex));
        }
        data.add(vector);
    }

    return new DefaultTableModel(data, columnNames);

    }

但是,当我尝试运行该应用程序时,它给了我

    java.lang.NullPointerException

错误信息。那我该怎么办?提前致谢。

4

1 回答 1

1

看起来bug是

JTable table = new JTable(buildTableModel(null));
于 2012-12-20T11:28:30.760 回答