尝试检索多个JDBC结果

时间:2016-11-13 22:29:33

标签: java mysql database eclipse 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);
    }
}

2 个答案:

答案 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);
        }
    }

答案 1 :(得分: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);