0

我必须向表模型添加两个查询,以便它显示在表上。这是一个对足球进行预测的程序(准确地说是 EPL),我需要显示球队在主场和客场比赛时的所有结果。第一个查询是获取他们在家打球的所有比赛,第二个查询是他们何时打客场。这是代码:

public void showResultsTotalTeam(){
    deleteAllRows(dTableModel); // deleta all rows in the table
    try {
        conn = DriverManager.getConnection(connection.conn_url, connection.conn_user, connection.conn_pass);// connect to database server
        Statement sqlState = conn.createStatement();// create statement for sql
        String selectStuff = "SELECT games_team1, games_team2, games_winner, games_draw, games_team1_score, games_team2_score, games_month, games_day FROM games WHERE games_team1 = '" + cbxTeam1.getSelectedItem() + "'";// ststement for MySQL
        rows = sqlState.executeQuery(selectStuff);  // execute statement
        String selectStuff2 = "SELECT games_team1, games_team2, games_winner, games_draw, games_team1_score, games_team2_score, games_month, games_day FROM games WHERE games_team2 = '" + cbxTeam1.getSelectedItem() + "'";// ststement for MySQL

        rows2 = sqlState.executeQuery(selectStuff); // execute statement
        Object[] tempRow;// create object array to store queried results
        Object[] tempRow2;

        while(rows.next()){ // while there are still values to be seen to
            tempRow = new Object[]{rows.getString(1), rows.getString(2), rows.getString(3), rows.getString(4), rows.getString(5), rows.getString(6), rows.getString(7), rows.getString(8)};// add data to array
            tempRow2 = new Object[]{rows2.getString(1), rows2.getString(2), rows2.getString(3), rows2.getString(4), rows2.getString(5), rows2.getString(6), rows2.getString(7), rows2.getString(8)};
            dTableModel.addRow(tempRow); // add array to table model
            dTableModel.addRow(tempRow2);
        }

    } catch (SQLException ex) {
        // TODO Auto-generated catch block
        System.out.println(ex.getMessage());
    }
}

现在这段代码不起作用,什么也没有出现。

请帮忙?任何建议都会很棒。

4

2 回答 2

0

您尚未加载驱动程序类

Class.forName("Driver Class Name");

你还没有关闭任何东西。连接和结果集

于 2013-10-02T05:48:59.473 回答
0

除非行数 == 行数2,否则您在迭代时可能会遇到麻烦rows.next

还建议你迭代你是否在两个不同的块中编码,即一个用于rows和一个用于rows2

编辑

这也可以通过一个查询来完成

SELECT games_team1, games_team2, games_winner, games_draw, games_team1_score, 
games_team2_score, games_month, games_day FROM games 
WHERE games_team1 = '" + cbxTeam1.getSelectedItem() + "' " 
or games_team2 = '" + cbxTeam1.getSelectedItem() + "' "
于 2013-10-02T04:54:06.590 回答