2013-10-02 2 views
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()); 
    } 
} 

이제이 코드는 작동하지 않으며 아무 것도 나타나지 않습니다.

도와주세요. 어떤 충고라도 잘 될 것입니다. 당신이 rows.next

을 반복 할 때 행의 수는 == rows2의 수는 다음 문제가 발생할 수 있습니다하지 않는 한

+0

'이제이 코드는 작동하지 않습니다.'- 그게 무슨 뜻입니까? 쿼리에서 반환 된 값을보기 위해 display 문과 같은 기본적인 디버깅 작업을 수행 했습니까? 코드를 간단하게 유지하고 한 번에 하나의 쿼리 만 수행하는 것이 좋습니다. 각 쿼리 후 모델을 업데이트하십시오. – camickr

답변

0

또한 당신이 rows2

에 대한 두 가지 블록 즉, rows 하나 하나에 당신에게 코드를 할 반복 제안

편집이 또한 하나 개의 쿼리와 함께 할 수

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() + "' " 
+0

하지만 테이블 모델에 추가 할 수 없습니까? 그게 내가 먼저 시도한거야. 행의 길이는 동일합니다. – VaMoose

+0

왜 두 가지 쿼리로 귀찮게합니까? 내 편집 된 답변보기 –

0

당신은 THR 드라이버 클래스를

Class.forName("Driver Class Name"); 

을로드하지 않은 그리고 당신은 아무것도를 폐쇄하지 않았습니다. Connecton & ResultSet

+0

Java 7에서 드라이버 이름을 선언 할 필요가 없습니다. – VaMoose

관련 문제