• 数据库MySQL(课下作业,必做)


    数据库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. 编写程序,查询世界上的平均寿命最长和最短的国家

    实现步骤:

    导入world.sql

    • 下载zip文件,解压后得到world.sql

    • 右键点击数据库,运行sql文件,选择sql文件,点击开始

    • 刷新数据库,发现导入world成功

    任务一:查询人口超过1017530的所有城市列表

    代码实现如下:

    /**
     * MysqlTest1
     *
     * @author Fomalhaut20175308
     * @date 2019/4/29
     */
    
    import java.sql.*;
    
    public class MysqlTest1 {
        public static void main(String[] args) {
            Connection con;
            Statement sql;
            ResultSet rs;
            con = GetDBConnection.connectDB("world", "root", "**********");
            if (con == null) {
                return;
            }
            String sqlStr = "select*from city where population>1017530";
            try {
                sql = con.createStatement();
                rs = sql.executeQuery(sqlStr);
                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);
                    System.out.printf("%d	", id);
                    System.out.printf("%s	", name);
                    System.out.printf("%s	", countryCode);
                    System.out.printf("%s	", district);
                    System.out.printf("%d
    ", population);
                }
                con.close();
            } catch (SQLException e) {
                System.out.println("Error:" + e);
            }
    
    
        }
    }
    

    关键解释:

    • 我们打开country表查看要返回的数据格式,建立int类型的id和population(或long类型),String类型的name、countryCode、district用来存储得到的信息
    • 对应着列索引调用rs.getXXXX()方法获得信息并输出

    运行截图:


    任务二:查询中东国家的总人口

    代码实现如下:

    /**
     * MysqlTest2
     *
     * @author Fomalhaut20175308
     * @date 2019/4/29
     */
    
    import java.sql.*;
    
    public class MysqlTest2 {
        public static void main(String[] args) {
            Connection con;
            Statement sql;
            ResultSet rs;
            con = GetDBConnection.connectDB("world", "root", "**********");
            if (con == null) {
                return;
            }
            String sqlStr = "select Name,Population from country where Region = 'Middle East'";
            try {
                sql = con.createStatement();
                rs = sql.executeQuery(sqlStr);
                int total = 0;
                while (rs.next()) {
                    String name = rs.getString(1);
                    int population = rs.getInt(2);
                    System.out.printf("%s的人口为%d
    ", name, population);
                    total += population;
                }
                System.out.println("中东国家的总人口为:" + total);
            } catch (SQLException e) {
                System.out.println("Error:" + e);
            }
    
        }
    }
    

    关键解释:

    • 此任务要调查中东国家的总人口,我先分别输出了所有中东国家各自的人口数,最后输出总和。
    • 本任务中地区限定为中东,需要输出名字和人口数,因此只需在表中获得名字信息和人口数即可,sql语句为:String sqlStr = "select Name,Population from country where Region = 'Middle East'";
    • 根据sql语句,此次列索引只有两个,一个用来得到名字,一个用来得到人口数
    • 定义total用来得到总人口数,每查找到一个中东国家,就执行total += population;

    运行截图:

    任务三:查询世界上的平均寿命最长和最短的国家

    代码实现如下:

    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    /**
     * MysqlTest3
     *
     * @author Fomalhaut20175308
     * @date 2019/4/30
     */
    public class MysqlTest3 {
        public static void main(String[] args) {
            Connection con;
            Statement sql;
            ResultSet rs;
            con = GetDBConnection.connectDB("world", "root", "************");
            if (con == null) {
                return;
            }
            String sqlStr = "select Name,LifeExpectancy from country order by LifeExpectancy";
            try {
                sql = con.createStatement();
                rs = sql.executeQuery(sqlStr);
                while (rs.next()) {
                    float life = rs.getInt(2);
                    String name = rs.getString(1);
                    rs.first();
                    while (life == 0) {
                        rs.next();
                        life = rs.getInt(2);
                    }
                    name = rs.getString(1);
                    System.out.println("世界上平均寿命最短的国家为:" + name);
                    rs.last();
                    name = rs.getString(1);
                    System.out.println("世界上平均寿命最长的国家为:" + name);
    
                }
            } catch (SQLException e) {
                System.out.println("Error:" + e);
            }
        }
    }
    

    关键解释:

    • 由于要查询寿命最长和最短的国家,因此一定要对表中信息进行排序,sql语句String sqlStr = "select Name,LifeExpectancy from country order by LifeExpectancy";将会查找按照LifeExpectancy排序后的列表
    • rs.first()rs.last()分别控制游标移动到表中第一位和最后一位
    • 在查找中发现,有数个国家的平均寿命值没有记录,会干扰实际有效值,因此需要添加循环语句,如果平均寿命为空继续向下找

    运行截图:

    任务总结

    这次的任务完全针对数据库的操作,刚刚学完数据库的有关操作,这次的任务即可看作是一次对于学习内容的小测试,也是对于数据库相关知识的巩固练习。总体来说不是很难,掌握情况尚可。除了教材中的内容,这次任务第一次实地下载了sql文件进行操作,让我深感数据库功能强大。在任务进行中没有遇到什么大问题,类似寿命为空影响查找的情况在代码解释中已有提及,就不单列出阐述了。

  • 相关阅读:
    占位
    阳光服务平台-敏捷开发
    两种方法实现带验证码的用户登录
    红警大战JAVA简单版
    JPanel与JFrame的区别
    java中import详解
    敏捷开发
    GitHub:本地项目上传与团队协作
    从结缘计算机到未来规划
    (三)微信小程序首页的分类功能和搜索功能的实现笔记
  • 原文地址:https://www.cnblogs.com/darklord0604/p/10794284.html
Copyright © 2020-2023  润新知