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

    一、任务详情

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

    二、源代码

    • 1.任务二源代码:
    import java.sql.*;
    public class SelectCity {
        public static void main(String args[]) {
            Connection con;
            Statement sql;
            ResultSet rs;
            con = GetDBConnection.connectDB("world","root","");
            if(con == null ) {
                return;
            }
            String c3=" population >7017521";
            String sqlStr =
                    "select * from city where "+c3;
            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(e);
            }
        }
    }
    
    • 2.任务三源代码:
    import java.sql.*;
    public class SumPopulation {
        public static void main(String args[]) {
            Connection con;
            Statement sql;
            ResultSet rs;
            con = GetDBConnection.connectDB("world","root","");
            if(con == null ) {
                return;
            }
            String c3=" Region='Middle East'";
            String sqlStr =
                    "select * from country where "+c3;
            try {
                int sum=0;
                sql=con.createStatement();
                rs = sql.executeQuery(sqlStr);
                while(rs.next()) {
                    int p=rs.getInt(5);
                    sum+=p;
                }
                System.out.println("世界上所有中东国家的总人口数为:"+sum);
                con.close();
            }
            catch(SQLException e) {
                System.out.println(e);
            }
        }
    }
    
    • 3.任务四源代码
    import java.sql.*;
    public class getLifeExpectancy {
        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);
                String name1,name2;
                rs.first();
                float f=rs.getFloat(8);
                while (f==0.0){
                    rs.next();
                    f=rs.getFloat(8);
                }
                name1=rs.getString(2);
                System.out.println("世界上的平均寿命最短的国家是:"+name1);
                rs.last();
                name2=rs.getString(2);
                System.out.println("世界上的平均寿命最长的国家是:"+name2);
                con.close();
            }
            catch(SQLException e) {
                System.out.println(e);
            }
        }
    }
    

    三、运行截图

    • 测试截图

      • 打开数据库连接
      • 留言
    • 1.任务一运行截图

    • 2.任务二运行截图

    • 3.任务三运行截图

    • 4.任务四运行截图

    SP.码云链接

    SP2.参考资料

    1.数据库MySQL(课下作业)
    2.xampp安装后Apache无法启动解决办法
    3.win7下xampp启动成功但是无法访问localhost和127.0.0.1

  • 相关阅读:
    Oracle优化器模式不同导致索引失效
    Python补零操作
    正则表达式
    python习题(二)
    Linux常见报错及解决方法(持续更新)
    总结(三)----2020上
    总结二
    总结---持更
    python多线程实现方式,最基础的实现方式模块是什么
    python2和python3区别
  • 原文地址:https://www.cnblogs.com/jxxydwt1999/p/10816485.html
Copyright © 2011-2022 走看看