数据库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\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);
        }
    }
}
  • 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

posted @ 2019-05-05 21:35  20175215丁文韬  阅读(186)  评论(0编辑  收藏  举报