数据库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 ...
随机推荐
- fatal: refusing to merge unrelated histories问题解决
git中pull或merge时偶尔出现
- 剑指offer-字符串的排列-数组-递归-动态规划-python
题目描述 输入一个字符串,按字典序打印出该字符串中字符的所有排列.例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba. 输入描述: 输 ...
- 【测试环境】TCPCopy 使用方法
https://blog.csdn.net/ronmy/article/details/65657691 TCPCopy是一种请求复制(所有基于tcp的packets)工具,可以把在线请求导入到测试系 ...
- c#HtmlAgilityPack解析html
通过HtmlAgilityPack实现对html页面解析HtmlDocument doc = new HtmlDocument(); doc.Load(yourStream); var itemLis ...
- Malloc与Free不调用构造函数与析构函数
例子: #include "stdafx.h" #include <new> #include <iostream> using namespace std ...
- spring中spEL常用应用场景
spEL表达式表示:#{} 一.基本类型值运算操作 {}可以放置数字,字符串,布尔型,表达式(运算,正则,逻辑).这个应用场景和EL表达式一样的,实际中用的也不多. 注意:在XML中表示==,> ...
- python中reload(sys)作用
python在安装时,默认的编码是ascii,当程序中出现非ascii编码时,python的处理常常会报错UnicodeDecodeError: 'ascii' codec can't decode ...
- git每次更新都需要输入账号密码,如何解决?
//如果https请求pull每次都需要输入账号密码,则 git config --global credential.helper store 那么,在下一次更新,需要输入账号密码,然后git就 ...
- bootstrap-table给每一行数据添加按钮,并绑定事件
https://blog.csdn.net/mht1829/article/details/72633100 https://blog.csdn.net/qq_39215166/article/det ...
- STM32CubeMX FreeRTOS定时器的使用
配置STM32CubeMX如下 生成的Keil代码的创建启动定时器如下 /* Create the timer(s) */ /* definition and creation of myTimer0 ...