zoukankan      html  css  js  c++  java
  • 数据库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.导入world.sql

     2.编写代码

    • 查询世界上超过“你学号前边七位并把最后一位家到最高位,最高位为0时置1”(比如学号20165201,超过3016520;学号20165208,超过1016520)的所有城市列表
    /**
     * Created by wff on 2019/5/4.
     */
    import java.sql.*;
    public class test2_1 {
        public static void main(String args[]) {
            Connection con=null;
            Statement sql;
            ResultSet rs;
            try{  Class.forName("com.mysql.jdbc.Driver"); //加载JDBC_MySQL驱动
            }
            catch(Exception e){}
            String uri = "jdbc:mysql://localhost:3306/world?useSSL=true";
            String user ="root";
            String password ="";
            try{
                con = DriverManager.getConnection(uri,user,password); //连接代码
            }
            catch(SQLException e){ }
            try {
                sql=con.createStatement();
                rs=sql.executeQuery("SELECT * FROM city"); //查询mess表
                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>8015523){    //20175236
                        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);
            }
        }
    }

    • 查询人口
    import java.sql.*;
    public class test2_2 {
        public static void main(String [] args) {
            Connection con=null;
            Statement sql;
            ResultSet rs;
            String sqlStr = "select * from country where Region = 'Middle East'";
            try{  Class.forName("com.mysql.jdbc.Driver"); //加载JDBC_MySQL驱动
            }
            catch(Exception e){}
            String uri = "jdbc:mysql://localhost:3306/world?useSSL=true";
            String user ="root";
            String password ="";
            try{
                con = DriverManager.getConnection(uri,user,password); //连接代码
            }
            catch(SQLException e){ }
            try {
                sql=con.createStatement();
                rs = sql.executeQuery(sqlStr);
                int sum=0;
                while(rs.next()) {
                    int population=rs.getInt(7);
                    sum =sum+population;
                }
                System.out.printf("中东国家的总人口为:"+sum);
                con.close();
            }
            catch(SQLException e) {
                System.out.println(e);
            }
        }
    }

    • 查询世界上的平均寿命最长和最短的国家
    import java.sql.*;
    public class test2_3 {
        public static void main(String [] args) {
            Connection con=null;
            Statement sql;
            ResultSet rs;
            float smzd=1000.0f,smzc=0.0f;
            String c=new String("");
            String d=new String("");
            String sqlStr =
                    "select * from country order by LifeExpectancy";
            try{  Class.forName("com.mysql.jdbc.Driver"); //加载JDBC_MySQL驱动
            }
            catch(Exception e){}
            String uri = "jdbc:mysql://localhost:3306/world?useSSL=true";
            String user ="root";
            String password ="";
            try{
                con = DriverManager.getConnection(uri,user,password); //连接代码
            }
            catch(SQLException e){ }
            try {
                sql=con.createStatement();
                rs = sql.executeQuery(sqlStr);
                while(rs.next()) {
                    String Name=rs.getString(2);
                    Float LifeExpectancy=rs.getFloat(8);
                    if(LifeExpectancy>smzc) {
                        smzc =LifeExpectancy;
                        c =Name;
                    }
                    else if(LifeExpectancy<smzd){
                        {
                            smzd = LifeExpectancy;
                            d = Name;
                        }
                    }
    
                }
                con.close();
                System.out.printf("世界上平均寿命最长的国家为:"+c+",平均寿命为:"+smzc+"
    ");
                System.out.printf("世界上平均寿命最短的国家为:"+d+",平均寿命为:"+smzd+"
    ");
            }
            catch(SQLException e) {
                System.out.println(e);
            }
        }
    }

  • 相关阅读:
    Java finally语句到底是在return之前还是之后执行?
    图文介绍openLDAP在windows上的安装配置
    ES之十:ElasticSearch监控工具 cerebro
    ES之八:ES数据库重建索引——Reindex(数据迁移)
    ES index的数据量大于1万的特殊处理场景
    Java中的Hashtable实现方法
    form表单回车提交问题(转)
    ES之九:ES Java客户端Elasticsearch Java client(ES Client 简介、Java REST Client、Java Client、Spring Data Elasticsearch)
    NetBeans 7.0 安装Python插件的方法
    python学习笔记
  • 原文地址:https://www.cnblogs.com/wff666999/p/10816750.html
Copyright © 2011-2022 走看看