gpt4 book ai didi

java - 尝试检索多个 JDBC 结果

转载 作者:行者123 更新时间:2023-11-29 19:59:56 26 4
gpt4 key购买 nike

因此,我试图打印出我的程序中处理某些严重程度的医疗状况(高、中和低)的所有医院,这些医院是从我的程序的另一部分传递过来的。

最初,它会找到正确的严重级别,因此它会检查数据库中的另一个表以查找医院并打印它们。然而,它只打印其中一家医院(有两家医院的严重程度很高,我正在测试),而不是另一家。

任何帮助将不胜感激,我觉得我的循环是错误的。

谢谢。

// 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);
}
}

最佳答案

您丢失了一条记录,因为您使用了 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);
}
}

关于java - 尝试检索多个 JDBC 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40579514/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com