数据库MySQL(课下作业,必做) 20175225
作业要求:
1.下载附件中的world.sql.zip, 参考http://www.cnblogs.com/rocedu/p/6371315.html#SECDB,导入world.sql,提交导入成功截图
2.编写程序,查询世界上超过“你学号前边七位并把最后一位家到最高位,最高位为0时置1”(比如学号20165201,超过3016520;学号20165208,超过1016520)的所有城市列表,提交运行结果截图
3.编写程序,查询世界上的所有中东国家的总人口
4.编写程序,查询世界上的平均寿命最长和最短的国家
相关问题
GetDBCnnection类由于系统版本不同,改为
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;
}
}
步骤
一、world.sql
1.下载相关附件并解压。
2.在数据库单击右键,运行sql文件,选择sql文件,点击开始。
3.导入完成后,重新打开连接会有显示。

二、编写程序,查询世界上超过“你学号前边七位并把最后一位家到最高位,最高位为0时置1”
1.学号为20175225,查询为超过7017522
2.实验代码
import java.sql.*;
public class MySQL1 {
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>7017522";
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("Error:" + e);
}
}
}
3.相关截图


三、编写程序,查询世界上的所有中东国家的总人口
1.相关代码
import java.sql.*;
public class MySQL2 {
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 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);
}
}
}
2.截图

四、编写程序,查询世界上的平均寿命最长和最短的国家
1.相关代码
import java.sql.*;
public class MySQL3 {
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);
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);
}
}
}
2.截图

数据库MySQL(课下作业,必做) 20175225的更多相关文章
- 数据库MySQL(课下作业,必做)
数据库MySQL(课下作业,必做) 题目要求: 下载附件中的world.sql.zip, 参考http://www.cnblogs.com/rocedu/p/6371315.html#SECDB,导入 ...
- 20175221 曾祥杰 数据库MySQL(课下作业,必做)
数据库MySQL(课下作业,必做) 题目要求: 1. 下载附件中的world.sql.zip, 参考http://www.cnblogs.com/rocedu/p/6371315.html#SECDB ...
- 20175314薛勐 数据库MySQL(课下作业,必做)
数据库MySQL(课下作业,必做) 要求 下载附件中的world.sql.zip, 参考Intellj IDEA 简易教程:数据库,导入world.sql,提交导入成功截图 编写程序,查询世界上超过& ...
- 学号20175313 《数据库MySQL(课下作业,必做)》第十周
目录 一.题目要求 二.需求分析 三.关键代码以及运行结果截图 任务一 任务二 任务三 任务四 四.代码实现过程中遇到的问题及其解决方法 五.码云链接 六.心得体会 一.题目要求 下载附件中的worl ...
- 数据库MySQL(课下作业)
一.作业要求 下载附件中的world.sql.zip, 参考http://www.cnblogs.com/rocedu/p/6371315.html#SECDB,导入world.sql,提交导入成功截 ...
- MySQL课下作业
目录 MySQL 实验内容 实验代码 实验截图 代码链接 MySQL 实验内容 下载附件中的world.sql.zip, 参考http://www.cnblogs.com/rocedu/p/63713 ...
- 20175221 MyCP(课下作业,必做)
MyCP(课下作业,必做) 任务详情 编写MyCP.java 实现类似Linux下cp XXX1 XXX2的功能,要求MyCP支持两个参数: - java MyCP -tx XXX1.txt XXX2 ...
- 20175333曹雅坤MyCP(课下作业,必做)
MyCP(课下作业,必做) 要求 编写MyCP.java 实现类似Linux下cp XXX1 XXX2的功能,要求MyCP支持两个参数: java MyCP -tx XXX1.txt XXX2.bin ...
- ###20175311MyCP(课下作业,必做)
MyCP(课下作业,必做) 作业题目 编写MyCP.java 实现类似Linux下cp XXX1 XXX2的功能,要求MyCP支持两个参数: java MyCP -tx XXX1.txt XXX2.b ...
随机推荐
- SIGCHLD和wait/waipid函数的关系
SIGCHILD只是在子进程退出的时候发送给父进程的一个信号值,这是一种异步通知父进程的方式.父进程可以捕获,忽略这个信号,默认动作是忽略此信号. 常用的使用方式是,当SIGCHILD信号发生时候,主 ...
- Cookie、Session和LocalStorage
前记 前面我已经写了一篇关于Cookie的文章,但是那时候我其实理解的并不是很深刻,会有些搞不懂的地方,今天我就再写一次,博客也是我的学习笔记 Cookie Cookie是服务器发送到用户浏览器并保存 ...
- 【转】golang 交叉编译
问题 golang如何在一个平台编译另外一个平台可以执行的文件.比如在mac上编译Windows和linux可以执行的文件.那么我们的问题就设定成:如何在mac上编译64位linux的可执行文件. 解 ...
- 剑指offer-7:调整数组顺序使奇数位于偶数前面
一.相对位置可以改变 1.题目 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分. 2.分析 不考虑相对位置,可以类比快排,用左右 ...
- markdown的日常使用
# POI前端接口 ``` 描述: 项目poi接口 作者: X-Wolf 时间: -- ``` ------ [TOC] ------ ##声明 ### 请求地址 ``` DOMAIN/strateg ...
- flutter-dart语言初识
dart 官方文档 http://dart.goodev.org/guides/language/language-tour# 重要概念所以能够使用变量引用的都是对象,也就是所以可以赋值给变量的都是对 ...
- vue项目报错1 Vue is a constructor and should be called with the `new` keyword && jquery.js?eedf:3850 Uncaught TypeError: this._init is not a function...
Vue is a constructor and should be called with the `new` keyword Uncaught TypeError: this._init is n ...
- 解决错误:无法在web.xml或使用此应用程序部署的jar文件中解析绝对uri:[http://shiro.apache.org/tags]
服务器错误信息如下: 解决方法: 把shiro包中的tld文件(shiro.tld)解压出来放到WEB-INF文件夹下即解决问题. 参考:http://blog.sina.com.cn/s/blog_ ...
- 裸机开发体验之led快速体验
[root@promote led]# arm-linux-gcc -g -c led.S[root@promote led]# lsled.lds led.o led.S Makefile[root ...
- C语言之带有数量可变的宏参数#define
1.定义格式如下 #define PR(...) printf(__VA_ARGS__) ...表示可变参数,__VA_ARGS__的作用是替换省略号的内容. 2.示例 #define ERROR( ...