0

我尝试使用 java swing 编写一个论坛。首先,在点击jTable时,它会导致eForumContent.class一起传入id。

    jTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent e) {
                int id = 0;
                eForumTopics topics = new eForumTopics(id);
                topics.retrieveDiscussion();
                eForumThreadContent myWindow = new eForumThreadContent(id);
                myWindow.getJFrame().setVisible(true);
            }
        });

我先初始化id。但是我将数据库表中的 id 设置为自动编号。然后我调用retrieveDiscussion 方法从数据库中获取id 并将其传递给eForumThreadContent。这是我的retrieveDiscussion 方法的代码。

        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_id = '" + id
            + "'";
    rs = db.readRequest(dbQuery);
    db.terminate();
    return success;
}
    }

然后,在 eForumThreadContent 中,我想使用表格显示所选线程的内容。所以我使用我刚才传入的id并插入到我的sql语句中。这是我的代码。

        public eForumThreadContent(int id) {
    this.id = id;
}

    if (jTable == null) {
        Vector columnNames = new Vector(); // Vector class allows dynamic
                                            // array of objects
        Vector data = new Vector();
        try {
            DBController db = new DBController();
            db.setUp("IT Innovation Project");
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
            String dsn = "IT Innovation Project";
            String s = "jdbc:odbc:" + dsn;
            Connection con = DriverManager.getConnection(s, "", "");
            String sql = "Select topic_title,topic_description,topic_by from forumTopics WHERE topic_id = '"+id+"'";
            java.sql.Statement statement = con.createStatement();
            ResultSet resultSet = statement.executeQuery(sql);
            ResultSetMetaData metaData = resultSet.getMetaData();
            int columns = metaData.getColumnCount();
            for (int i = 1; i <= columns; i++) {
                columnNames.addElement(metaData.getColumnName(i));
            }
            while (resultSet.next()) {
                Vector row = new Vector(columns);
                for (int i = 1; i <= columns; i++) {
                    row.addElement(resultSet.getObject(i));
                }
                data.addElement(row);
            }
            resultSet.close();
            ((Connection) statement).close();
        } catch (Exception e) {
            System.out.println(e);
        }
        jTable = new JTable(data, columnNames);
        TableColumn column;
        for (int i = 0; i < jTable.getColumnCount(); i++) {
            column = jTable.getColumnModel().getColumn(i);
            if (i == 1) {
                column.setPreferredWidth(400); // second column is bigger
            }else {
                column.setPreferredWidth(200);
            }
        }
        String header[] = { "Title", "Description", "Posted by" };

        for (int i = 0; i < jTable.getColumnCount(); i++) {
            TableColumn column1 = jTable.getTableHeader().getColumnModel()
                    .getColumn(i);

            column1.setHeaderValue(header[i]);
        }
        jTable.getTableHeader().setFont( new Font( "Dialog" , Font.PLAIN, 20 ));
        jTable.getTableHeader().setForeground(Color.white);
        jTable.getTableHeader().setBackground(new Color(102, 102, 102));
        jTable.setEnabled(false);
        jTable.setRowHeight(100);
        jTable.getRowHeight();
        jTable.setFont( new Font( "Dialog" , Font.PLAIN, 18 ));
    }
    return jTable;
}

但是,即使我单击了某个线程,eForumThreadContent 中的表格也是空的。它还给了我一条错误消息。

    [Microsoft][ODBC Microsoft Access Driver] Data type mismatch in criteria expression.
at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcStatement.execute(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcStatement.executeQuery(Unknown Source)
at DBController.database.DBController.readRequest(DBController.java:27)
at kioskeForum.entity.eForumTopics.retrieveDiscussion(eForumTopics.java:67)
at kioskeForum.ui.eForumDiscussion$3.mouseClicked(eForumDiscussion.java:296)

我在线研究以了解使用 id 的主题视图。但我的桌子没有显示任何东西。有人可以告诉我如何解决它吗?或者在java swing中显示主题视图的任何其他方式?提前致谢。

4

0 回答 0