《数据库MySQL》

《数据库MySQL》

一、题目要求

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

二、具体步骤

task1

首先现将附件下载好,由于数据库下载的是书上给出的Navicat for MySQL,因此和老师博客中给出的导入方法不同,在此简单给出操作步骤:

  • 导入前的数据库

  • 在数据库名字上右击选择“运行SQL文件”

  • 在弹出的对话框中选择你的导入路径,点击“开始”开始导入,当进度条全部变绿并且上部百分比为100%时导入完成,点击“关闭”

  • 重启数据库后即可看到成功导入的文件(注意一定要关闭数据库再重新打开,开始时因为没有重启所以没有显示导入好的数据库,一直在反复导入)

  • 导入成功图片

task2

  • 待查询的人口数为大于1017520
  • 代码如下:
package MySQL;
import java.sql.*;
public class task2 {    
    public static void main(String[] args) {        
        Connection con;        
        Statement sql;        
        ResultSet rs;        
        con = GetDBConnection.connectDB("world","root","wzh990213");        
        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>1017520) {                    
                    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);        
        }    
    }
}
  • 代码说明:在其中的city表中进行查询,当人口大于1017520时,输出该国家city表中的所有信息
  • 运行结果

task3

  • 代码如下
package MySQL;
import java.sql.*;
public class task3 {    
    public static void main(String[] args) {        
        Connection con;        
        Statement sql;        
        ResultSet rs;        
        con = GetDBConnection.connectDB("world","root","wzh990213");        
        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);        
        }   
    }
}
  • 代码说明:在country表中进行查询通过where Region = 'Middle East' 作为判断条件进行查询,并对人口进行累加
  • 运行结果

task4

  • 代码如下
package MySQL;
import java.sql.*;
public class task4 {   
    public static void main(String[] args) {        
        Connection con;        
        Statement sql;        
        ResultSet rs;        
        con = GetDBConnection.connectDB("world","root","wzh990213");        
        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);        
        }    
    }
}
  • 代码说明:首先通过order by LifeExpectancy 对country表进行排序,然后将游标指向第一行判断对应位置的内容是否为空,为空则下移直到找到第一个不是空的进行输出;再将游标移至最后一行进行输出
  • 运行结果

额外代码

在完成该任务时也用到了书上的GetDBConnection代码进行数据库的链节操作,代码如下:

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;    
    }
}

三、过程中出现的问题

  • 问题:在完成第四个任务是出下了输出的平均寿命为0的情况,在数据库中的country表中进行查找该国家后发现该国家的LifeExpectancy为空,因此输出的寿命为0

  • 解决方法:在对country表用关键字LifeExpectancy进行排序后发现前12个国家对应的平均寿命值为空,因此应该控制游标位置输出第一个不为空的国家,更改后的结果如下

四、代码托管

五、感想

本次任务比较简单,之前学习书上的内容时基本都己经完成了对数据库的配置,因此操作起来没有大问题,在完成本次任务时也是按照书上的例子进行部分修改,改成自己需要的条件即可,难度不大

参考资料

原文地址:https://www.cnblogs.com/wangzihong0213/p/10792379.html