数据库MySQL(课下作业)
作业要求
1. 下载附件中的world.sql.zip, 参考http://www.cnblogs.com/rocedu/p/6371315.html#SECDB,导入world.sql,提交导入成功截图
2. 编写程序,查询世界上超过“你学号前边七位并把最后一位家到最高位,最高位为0时置1”(比如学号20165201,超过3016520;学号20165208,超过1016520)的所有城市列表,提交运行结果截图
3. 编写程序,查询世界上的所有中东国家的总人口
4. 编写程序,查询世界上的平均寿命最长和最短的国家
具体步骤
1.导入world.sql
2.编写代码
- 查询世界上超过“你学号前边七位并把最后一位家到最高位,最高位为0时置1”(比如学号20165201,超过3016520;学号20165208,超过1016520)的所有城市列表
/** * Created by wff on 2019/5/4. */ import java.sql.*; public class test2_1 { public static void main(String args[]) { Connection con=null; Statement sql; ResultSet rs; try{ Class.forName("com.mysql.jdbc.Driver"); //加载JDBC_MySQL驱动 } catch(Exception e){} String uri = "jdbc:mysql://localhost:3306/world?useSSL=true"; String user ="root"; String password =""; try{ con = DriverManager.getConnection(uri,user,password); //连接代码 } catch(SQLException e){ } try { sql=con.createStatement(); rs=sql.executeQuery("SELECT * FROM city"); //查询mess表 while(rs.next()) { int id=rs.getInt(1); String name=rs.getString(2); String countrycode=rs.getString(3); String district=rs.getString(4); int population=rs.getInt(5); if(population>8015523){ //20175236 System.out.printf("%d\t",id); System.out.printf("%s\t",name); System.out.printf("%s\t",countrycode); System.out.printf("%s\t",district); System.out.printf("%d\n",population); } } con.close(); } catch(SQLException e) { System.out.println(e); } } }
- 查询人口
import java.sql.*; public class test2_2 { public static void main(String [] args) { Connection con=null; Statement sql; ResultSet rs; String sqlStr = "select * from country where Region = 'Middle East'"; try{ Class.forName("com.mysql.jdbc.Driver"); //加载JDBC_MySQL驱动 } catch(Exception e){} String uri = "jdbc:mysql://localhost:3306/world?useSSL=true"; String user ="root"; String password =""; try{ con = DriverManager.getConnection(uri,user,password); //连接代码 } catch(SQLException e){ } try { sql=con.createStatement(); rs = sql.executeQuery(sqlStr); int sum=0; while(rs.next()) { int population=rs.getInt(7); sum =sum+population; } System.out.printf("中东国家的总人口为:"+sum); con.close(); } catch(SQLException e) { System.out.println(e); } } }
- 查询世界上的平均寿命最长和最短的国家
import java.sql.*; public class test2_3 { public static void main(String [] args) { Connection con=null; Statement sql; ResultSet rs; float smzd=1000.0f,smzc=0.0f; String c=new String(""); String d=new String(""); String sqlStr = "select * from country order by LifeExpectancy"; try{ Class.forName("com.mysql.jdbc.Driver"); //加载JDBC_MySQL驱动 } catch(Exception e){} String uri = "jdbc:mysql://localhost:3306/world?useSSL=true"; String user ="root"; String password =""; try{ con = DriverManager.getConnection(uri,user,password); //连接代码 } catch(SQLException e){ } try { sql=con.createStatement(); rs = sql.executeQuery(sqlStr); while(rs.next()) { String Name=rs.getString(2); Float LifeExpectancy=rs.getFloat(8); if(LifeExpectancy>smzc) { smzc =LifeExpectancy; c =Name; } else if(LifeExpectancy<smzd){ { smzd = LifeExpectancy; d = Name; } } } con.close(); System.out.printf("世界上平均寿命最长的国家为:"+c+",平均寿命为:"+smzc+"\n"); System.out.printf("世界上平均寿命最短的国家为:"+d+",平均寿命为:"+smzd+"\n"); } catch(SQLException e) { System.out.println(e); } } }