zoukankan      html  css  js  c++  java
  • 20175202 葛旭阳 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.IDEA与数据库连接,并运行测试代码

    2.下载附件中的world.sql.zip, 参考http://www.cnblogs.com/rocedu/p/6371315.html#SECDB,导入world.sql,提交导入成功截图

    过程:1.下载相关附件并解压。
    2.在数据库单击右键,运行sql文件,选择sql文件,点击开始。
    3.导入完成后。

    3.编写程序,查询世界上超过“你学号前边七位并把最后一位家到最高位,最高位为0时置1”(比如学号20165201,超过3016520;学号20165208,超过1016520)的所有城市列表,提交运行结果截图

    实验代码:

    import java.sql.*;
    
    public class MySQL1 {
        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>4017520";
            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);
            }
    
    
        }
    }
    
    

    4.编写程序,查询世界上的所有中东国家的总人口

    实验代码:

    
    import java.sql.*;
    public class MySQL2 {
        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);
            }
        }
    }
    

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

    实验代码:

    import java.sql.*;
    public class MySQL3 {
        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);
            }
        }
    }
    

  • 相关阅读:
    2. 两数相加
    1. 两数之和
    x-pack elasticsearch
    简单的文档
    PHP imagepng函数 问题
    Nginx 配置
    nginx内置变量
    TCP通信
    mysql 的一些操作
    ubuntu 软件包降级
  • 原文地址:https://www.cnblogs.com/gexvyang/p/10819416.html
Copyright © 2011-2022 走看看