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

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

作业要求:

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


相关问题

GetDBConnection类由于系统版本不同,经过网上搜索改为

 * task1
 *
 * @author Fomalhaut20175204
 * @date 2019/5/4
 */
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;
    }
}

相关步骤

一、world.sql

1.下载相关附件并解压。
2.在数据库单击右键,运行sql文件,选择sql文件,点击开始。
3.导入完成后,重新打开连接会有显示。
1592505-20190505161415262-1869106525.png


二、编写程序,查询世界上超过“你学号前边七位并把最后一位家到最高位,最高位为0时置1”

1.学号为20175204 查询为超过6017520
2.实验代码

 * task1
 *
 * @author Fomalhaut20175204
 * @date 2019/5/4
 */

import java.sql.*;
public class task1 {
    public static void main(String[] args) {
        Connection con;
        Statement sql;
        ResultSet rs;
        con = GetDBConnection.connectDB("world","root","123");
        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>6017520) {
                    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);
        }
    }
}

3.相关截图
1592505-20190505161552498-1481340432.png
1592505-20190505161604281-270414834.png


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

1.相关代码

 * task2
 *
 * @author 20175204
 * @date 2019/5/4
 */

import java.sql.*;
public class task2 {
    public static void main(String[] args) {
        Connection con;
        Statement sql;
        ResultSet rs;
        con = GetDBConnection.connectDB("world","root","123");
        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);
        }
    }
}

2.截图
1592505-20190505161843909-2105198499.png


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

1.相关代码

 * task2
 *
 * @author 20175204
 * @date 2019/5/4
 */
import java.sql.*;
public class task3 {
    public static void main(String[] args) {
        Connection con;
        Statement sql;
        ResultSet rs;
        con = GetDBConnection.connectDB("world","root","123");
        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.截图
1592505-20190505161950254-305244291.png


转载于:https://www.cnblogs.com/zyzgl/p/10813671.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值