20175324数据库MySQL

数据库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. 编写程序,查询世界上的平均寿命最长和最短的国家

    实现步骤:

    导入world.sql

  • 下载zip文件,解压后得到world.sql
  • 登入localhost网站

任务一:查询人口超过1017530的所有城市列表

代码实现如下:

import java.sql.*;
public class Mysql {
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 city where population>1017530";
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("Error:" + e);
}


}
}

关键解释:

  • 我们打开country表查看要返回的数据格式,建立int类型的id和population(或long类型),String类型的name、countryCode、district用来存储得到的信息
  • 对应着列索引调用rs.getXXXX()方法获得信息并输出

  • 如果你设置了密码,请在password那个引号里输入
  • 运行截图:

任务二:查询

中东国家的总人口

代码实现如下:

import java.sql.*;

public class Mysqltwo {
public static void main(String[] args) {
Connection con;
Statement sql;
ResultSet rs;
con = GetDBConnection.connectDB("world", "root", "");
if (con == null) {
return;
}
String sqlStr = "select Name,Population from country where Region = 'Middle East'";
try {
sql = con.createStatement();
rs = sql.executeQuery(sqlStr);
int total = 0;
while (rs.next()) {
String name = rs.getString(1);
int population = rs.getInt(2);
System.out.printf("%s的人口为%d ", name, population);
total += population;
}
System.out.println("中东国家的总人口为:" + total);
} catch (SQLException e) {
System.out.println("Error:" + e);
}
}
}


关键解释:

  • 此任务要调查中东国家的总人口,我先分别输出了所有中东国家各自的人口数,最后输出总和。
  • 本任务中地区限定为中东,需要输出名字和人口数,因此只需在表中获得名字信息和人口数即可,sql语句为:String sqlStr = "select Name,Population from country where Region = 'Middle East'";
  • 根据sql语句,此次列索引只有两个,一个用来得到名字,一个用来得到人口数
  • 定义total用来得到总人口数,每查找到一个中东国家,就执行total += population;

    运行截图:

任务三:查询世界上的平均寿命最长和最短的国家

代码实现如下:

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class Mysqlthree {
public static void main(String[] args) {
Connection con;
Statement sql;
ResultSet rs;
con = GetDBConnection.connectDB("world", "root", "");
if (con == null) {
return;
}
String sqlStr = "select Name,LifeExpectancy from country order by LifeExpectancy";
try {
sql = con.createStatement();
rs = sql.executeQuery(sqlStr);
while (rs.next()) {
float life = rs.getInt(2);
String name = rs.getString(1);
rs.first();
while (life == 0) {
rs.next();
life = rs.getInt(2);
}
name = rs.getString(1);
System.out.println("世界上平均寿命最短的国家为:" + name);
rs.last();
name = rs.getString(1);
System.out.println("世界上平均寿命最长的国家为:" + name);

}
} catch (SQLException e) {
System.out.println("Error:" + e);
}
}
}

关键解释:

  • 由于要查询寿命最长和最短的国家,因此一定要对表中信息进行排序,sql语句String sqlStr = "select Name,LifeExpectancy from country order by LifeExpectancy";将会查找按照LifeExpectancy排序后的列表
  • rs.first()rs.last()分别控制游标移动到表中第一位和最后一位
  • 在查找中发现,有数个国家的平均寿命值没有记录,会干扰实际有效值,因此需要添加循环语句,如果平均寿命为空继续向下找

    运行截图:

任务总结

这次的任务完全针对数据库的操作,刚刚学完数据库的有关操作,这次的任务即可看作是一次对于学习内容的小测试,也是对于数据库相关知识的巩固练习。在自己对Xampp的调试过程中,通过百度和goole学习了不少知识,解决了一些问题,颇有收获。

原文地址:https://www.cnblogs.com/wcqy/p/10814881.html