20175314薛勐 数据库MySQL(课下作业,必做)
数据库MySQL(课下作业,必做)
要求
- 下载附件中的world.sql.zip, 参考Intellj IDEA 简易教程:数据库,导入world.sql,提交导入成功截图
- 编写程序,查询世界上超过“你学号前边七位并把最后一位家到最高位,最高位为0时置1”(比如学号20165201,超过3016520;学号20165208,超过1016520)的所有城市列表,提交运行结果截图
- 编写程序,查询世界上的所有中东国家的总人口
- 编写程序,查询世界上的平均寿命最长和最短的国家
思路
- 比较说明需要用到where 子语句,保证查询的字段值在某个区间内
- 我的学号是20175314,所以应该输出超过6017531的所有城市列表
代码
GetDBConnection.java(启动数据库)
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;
}
}
getCity.java
package MySQL;
import java.sql.*;
public class getCity {
public static void main(String[] args) {
Connection con;
Statement sql;
ResultSet rs;
con = GetDBConnection.connectDB("world","root","");
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>6017531) {
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);
}
}
}
getPopulation.java
package MySQL;
import java.sql.*;
public class getPopulation {
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);
}
}
}
getLife.java
package MySQL;
import java.sql.*;
public class getLife {
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);
}
}
}
测试结果
Navicat Premium 12:world

getCity.java

getPopulation.java

getLife.java

码云链接
参考资料
20175314薛勐 数据库MySQL(课下作业,必做)的更多相关文章
- 20175314薛勐 MyCP(课下作业,必做)
MyCP(课下作业,必做) 要求 编写MyCP.java 实现类似Linux下cp XXX1 XXX2的功能,要求MyCP支持两个参数: java MyCP -tx XXX1.txt XXX2.bin ...
- 数据库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 ...
- 数据库MySQL(课下作业)
一.作业要求 下载附件中的world.sql.zip, 参考http://www.cnblogs.com/rocedu/p/6371315.html#SECDB,导入world.sql,提交导入成功截 ...
- 数据库MySQL(课下作业,必做) 20175225
作业要求: 1.下载附件中的world.sql.zip, 参考http://www.cnblogs.com/rocedu/p/6371315.html#SECDB,导入world.sql,提交导入成功 ...
- 学号20175313 《数据库MySQL(课下作业,必做)》第十周
目录 一.题目要求 二.需求分析 三.关键代码以及运行结果截图 任务一 任务二 任务三 任务四 四.代码实现过程中遇到的问题及其解决方法 五.码云链接 六.心得体会 一.题目要求 下载附件中的worl ...
- MySQL课下作业
目录 MySQL 实验内容 实验代码 实验截图 代码链接 MySQL 实验内容 下载附件中的world.sql.zip, 参考http://www.cnblogs.com/rocedu/p/63713 ...
- 20175314薛勐 MyOD(课下作业,选做)
MyOD(课下作业,选做) 要求 编写MyOD.java 用java MyOD XXX实现Linux下od -tx -tc XXX的功能 思路 伪代码: 读取命令行输入的参数(文件名) 以16为每个字 ...
- 20175234 数据库MySQL(课下作业)
20175234 数据库MySQL(课下作业) 内容: 1.下载附件中的world.sql.zip, 参考http://www.cnblogs.com/rocedu/p/6371315.html#SE ...
随机推荐
- 关于使用layui中的tree的一个坑
最近几天,因为项目需要,所以自学了下layui,在使用之前就对其比较感兴趣,毕竟封装的东西也不错(个人见解),在接触到layui之后,现在有个需要就是将部门做成tree的样子,开始觉得不怎么难,毕竟都 ...
- django 验证码图片生成视图函数
def verify_code(request): import random # 定义验证码图片背景颜色 宽和高 bgcolor = (random.randrange(20,180),random ...
- scrapy mid中间件一般处理方法
import user_agent import requests class UA_midd(object): def process_request(self,request,spider): r ...
- 【PG】Greenplum-db-6.2.1的安装部署
目录 1配置host文件(所有节点) 2 配置用户 3 配置/etc/sysctl.conf文件 4 limit文件,后面添加[不影响安装] 5 安装greenplum-db-6.2.1-rhel7- ...
- jmeter 性能测试基本过程及示例
jmeter 为性能测试提供了一下特色: jmeter 可以对测试静态资源(例如 js.html 等)以及动态资源(例如 php.jsp.ajax 等等)进行性能测试jmeter 可以挖掘出系统最大能 ...
- Http报文和Request和Response的常用方法
简述 它是HTTP应用程序之间发送的数据块.这些数据块以一些文本形式的元信息开头,这些信息描述了报文的内容及含义,后面跟着可选的数据部分.这些报文都是在客户端.服务器和代理之间流动. HTTP报文的流 ...
- python深浅拷贝&垃圾回收&上下文管理(with语句)
深浅拷贝 在Python中使用copy模块用于对象的拷贝操作. 该模块提供了两个主要的方法:浅拷贝 copy.copy() 深拷贝 copy.deepcopy() 1.浅拷贝(copy) 浅拷贝: 不 ...
- Mol Cell Proteomics. | MARMoSET – Extracting Publication-ready Mass Spectrometry Metadata from RAW Files
本文是马克思普朗克心肺研究所的三名研究者Marina Kiweler.Mario Looso和Johannes Graumann发表在8月刊的MCP的一篇文章. 由于Omics实验经常涉及数百个数据文 ...
- LoadRunner录制HTTPS协议脚本
学习LoadRunner录制HTTPS协议脚本,其实是一个意外的收获.当我拿到要测试的URL时,我像以前的步骤一样录制脚本,但是录制结束后,发现并没有生成脚本,开始以为是LoadRunner的原因,我 ...
- ajax结合sweetalert弹出框删除数据
思路: