课堂实践--数据库

课上数据库作业

  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、导入Word:
1296452-20180423213727032-453740808.png

3、编写程序,查询世界上的所有中东国家的总人口
中东国家人口:
code:

import java.sql.*;
public class Example2 {
    public static void main(String [] args) {
        Connection con=null;
        Statement sql;
        ResultSet rs;
        String c1=" Region Like 'Middle East'";
        String sqlStr =
                "select * from country where "+c1;
        try{  Class.forName("com.mysql.jdbc.Driver");
        }
        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);
            long totalPopulation=0;
            while(rs.next()) {
                int Population=rs.getInt(7);
                totalPopulation +=Population;
            }
            System.out.printf("中东总人口为"+totalPopulation);
            con.close();
        }
        catch(SQLException e) {
            System.out.println(e);
        }
    }
}

结果截图:

1296452-20180423214544401-124628289.png

4、编写程序,查询世界上的平均寿命最长和最短的国家

世界上平均寿命最长和最短的国家
code:

import java.sql.*;
public class Example3 {
    public static void main(String [] args) {
        Connection con=null;
        Statement sql;
        ResultSet rs;
        float min=100.0f,max=0.0f;
        int i=0;
        String minname=new String("");
        String maxname=new String("");
        String sqlStr =
                "select * from country order by LifeExpectancy";
        try{  Class.forName("com.mysql.jdbc.Driver");
        }
        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>max) {
                    max =LifeExpectancy;
                    maxname=Name;
                }
                else if((LifeExpectancy<min)&&(LifeExpectancy!=0.0)){
                    {
                        min = LifeExpectancy;
                        minname = Name;
                    }
                }

            }
            con.close();
            System.out.printf("寿命最长的国家为:"+maxname+",平均寿命"+max+"\n");
            System.out.printf("寿命最短的国家为:"+minname+",平均寿命"+min+"\n");
        }
        catch(SQLException e) {
            System.out.println(e);
        }
    }
}

结果截图:

1296452-20180423214600209-1604847075.png

转载于:https://www.cnblogs.com/yjtblog/p/8884619.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值