我试图输入城市名称,如果数据库中存在城市名称(否则错误消息),然后我想打印选定的城市名称、人口、纬度和经度。其余代码运行良好。
public static void city(){
ResultSet rs = null;
Scanner k = new Scanner (System.in);
System.out.println("Enter the name");
String Name = k.nextLine ();
try {
rs = stmt.executeQuery("SELECT * FROM cities WHERE name ="+Name);
// At least a record selected
if(rs.isBeforeFirst ()){
//Iterates through each record
while (rs.next ()){
String name = rs.getString (i:1);
int population = rs.getInt(i:2);
double latitude = rs.getDouble (i:3);
double longitude = rs.getDouble(i:4);
System.out.println("Name: " + name);
System.out.println("Population: " + population);
System.out.println("Latitude: " + latitude);
System.out.println("Longitude: " + longitude);
System.out.println();
}
}
else {
System.out.println("No records selected.");
}
}
catch(SQLException e) {
System.out.println("Error selecting from table: " + e.getMessage());
}
finally {
try {
rs.close();
}
catch(Exception e) {
//ResultSet still null/nothing to close
}
}
}
宝慕林4294392
子衿沉夜
慕虎7371278
相关分类