Java關於資料庫查詢的一些問題
阿新 • • 發佈:2018-11-01
當同時查詢多個數據集合時,需要定義多個statement,因為每個若同時使用一個statement,那麼資料會不斷進行覆蓋。
while(rs.next()) {
cnt++;
String provinceid = rs.getString("provinceId");
String provinceName = rs.getString("provinceName");
String sql2 = "select * from tcity where provinceId=" +provinceid;
ResultSet rs2 = stat2.executeQuery(sql2);
out.println("<tr>");
out.println("<th colspan = \"2\" id = \"firstrow\">"+provinceName+"</th>");
out.println("</tr>");
out.println("<tr>" );
out.println("<th>郵政編碼</th>");
out.println("<th>城市名稱</th>");
out.println("</tr>");
while(rs2.next()) {
String cityName = rs2.getString("cityName");
String cityCode = rs2. getString("cityCode");
out.println("<tr>");
out.println("<th>"+cityCode+"</th>");
out.println("<th>"+cityName+"</th>");
out.println("</tr>");
// out.print(provinceName+" "+city);
// out.print("<br>");
}
}
out.println("</table>");