2016-11-13 70 views
0

因此,我試圖打印出我的程序中處理某些嚴重程度的醫療條件(高,中,低)的所有醫院,這些醫療條件是從我的程序的另一部分傳遞。嘗試檢索多重JDBC結果

最初,它會找到正確的嚴重性級別,因此它會檢查數據庫中的另一個表以查找醫院並打印它們。然而,它只打印了其中一張(其中有兩張是嚴格測試的),但不是另一張。

任何幫助將不勝感激,我覺得我的循環是錯誤的。

感謝。

// Create selection query, get relevant hospitals 
String select = "SELECT * FROM hospitals WHERE hospitalSeverityHandling = '" + medicalCondition + "'"; 


// Get results hospitals in the database 
ResultSet results = statement.executeQuery(select); 

// When hospitals have been found 

if (results.next()) 
{ 
    results.first(); 

    // Get hospitals with relevant severity 
    //String hospital = results.getString("hospitalName"); 
    //System.out.println(hospital); 

    // Respond to user, get hospital selection 
    // TODO Hospital Selection 
    JOptionPane.showMessageDialog(null, "A list of hospitals have been retrieved for severity level: " + medicalCondition, "Select available hospital", JOptionPane.INFORMATION_MESSAGE); 

    // Loop hospitals 
    while(results.next()) 
    { 
     String hospital = results.getString("hospitalName"); 
     System.out.println(hospital); 
    } 
} 
+0

不要叫'results.first()'前'while'環

相反,你可以按如下方式使用它。我認爲你正在跳過第一個記錄。另外,您的查詢看起來很容易被注入。考慮使用準備好的聲明。考慮使用試用資源。 – bradimus

+0

謝謝。有人已經提出了修正意見,但是當我試圖將其標記爲正確時,它已被刪除。 –

回答

0

由於您正在使用results.next()兩次,所以缺少一條記錄。

String select = "SELECT * FROM hospitals WHERE  
     hospitalSeverityHandling = '" + medicalCondition + "'"; 

    // Get results hospitals in the database 
    ResultSet results = statement.executeQuery(select); 

    // Loop hospitals 
     while(results.next()) 
     { 
      String hospital = results.getString("hospitalName"); 
      System.out.println(hospital); 
     } 
    } 
0

修正:

while(results.next())    
      { 
       // Put into interactive list 
       String hospital = results.getString("hospitalName"); 

       // Loops each hospital via popup, needs to be added to a selection menu 
       JOptionPane.showMessageDialog(null, hospital, "Hospital List", JOptionPane.INFORMATION_MESSAGE);