zoukankan      html  css  js  c++  java
  • 20175314薛勐 数据库MySQL(课下作业,必做)

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

    要求

    1. 下载附件中的world.sql.zip, 参考Intellj IDEA 简易教程:数据库,导入world.sql,提交导入成功截图
    2. 编写程序,查询世界上超过“你学号前边七位并把最后一位家到最高位,最高位为0时置1”(比如学号20165201,超过3016520;学号20165208,超过1016520)的所有城市列表,提交运行结果截图
    3. 编写程序,查询世界上的所有中东国家的总人口
    4. 编写程序,查询世界上的平均寿命最长和最短的国家

    思路

    • 比较说明需要用到where 子语句,保证查询的字段值在某个区间内
    • 我的学号是20175314,所以应该输出超过6017531的所有城市列表

    代码

    GetDBConnection.java(启动数据库)

    package MySQL;
    import java.sql.*;
    public class GetDBConnection {
        public static Connection connectDB(String DBName,String id,String p) {
            Connection con = null;
            String
                    uri = "jdbc:mysql://localhost:3306/"+DBName+"?serverTimezone=GMT%2B8&characterEncoding=utf-8";
            try{  Class.forName("com.mysql.cj.jdbc.Driver");//加载JDBC-MySQL驱动
            }
            catch(Exception e){}
            try{
                con = DriverManager.getConnection(uri,id,p); //连接代码
            }
            catch(SQLException e){}
            return con;
        }
    }
    

    getCity.java

    package MySQL;
    import java.sql.*;
    public class getCity {
        public static void main(String[] args) {
            Connection con;
            Statement sql;
            ResultSet rs;
            con = GetDBConnection.connectDB("world","root","");
            if(con == null) {
                return;
            }
            try {
                sql=con.createStatement();
                rs = sql.executeQuery("SELECT * FROM city");
                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>6017531) {
                        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(e);
            }
        }
    }
    

    getPopulation.java

    package MySQL;
    import java.sql.*;
    public class getPopulation {
        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 country where Region = 'Middle East'";
            try {
                sql = con.createStatement();
                rs = sql.executeQuery(sqlStr);
                long totalpopulation = 0;
                while(rs.next()) {
                    int Population = rs.getInt(7);
                    totalpopulation +=Population;
                }
                System.out.println("中东国家的总人口为"+totalpopulation);
                con.close();
            }
            catch (SQLException e) {
                System.out.println(e);
            }
        }
    }
    

    getLife.java

    package MySQL;
    import java.sql.*;
    public class getLife {
        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 country order by LifeExpectancy";
            try {
                sql = con.createStatement();
                rs = sql.executeQuery(sqlStr);
                rs.first();
                String highcountry,lowcountry;
                float number1 = rs.getInt(8);
                while(number1 == 0) {
                    rs.next();
                    number1 = rs.getInt(8);
                }
                lowcountry = rs.getString(2);
                System.out.println("世界上平均寿命最短的国家为:"+lowcountry+" 寿命为"+number1);
                rs.last();
                float number2 = rs.getInt(8);
                highcountry = rs.getString(2);
                System.out.println("世界上平均寿命最长的国家为:"+highcountry+" 寿命为"+number2);
                con.close();
            }
            catch (SQLException e) {
                System.out.println(e);
            }
        }
    }
    

    测试结果

    getCity.java

    getPopulation.java

    getLife.java

    码云链接

    参考资料

    Intellj IDEA 简易教程:数据库

  • 相关阅读:
    Linux中ctrl+z 、ctrl+c、 ctrl+d区别
    linux文件与用户权限的设置
    查找jdk的安装目录
    linux上修改防火墙操作
    linux上打包与压缩操作
    安装hive操作参考视频
    linux上设置环境变量每次需要source /etc/profile问题处理
    【c语言趣味编程100例】出售金鱼
    【c语言趣味编程100例】三色球问题
    【c语言】递归实现strlen()函数
  • 原文地址:https://www.cnblogs.com/SANFENs/p/10809190.html
Copyright © 2011-2022 走看看