MySQL

下载附件中的world.sql.zip, 参考http://www.cnblogs.com/rocedu/p/6371315.html#SECDB,导入world.sql,提交导入成功截图

编写程序,查询世界上超过“你学号前边七位并把最后一位家到最高位,最高位为0时置1”(比如学号20165201,超过3016520;学号20165208,超过1016520)的所有城市列表,提交运行结果截图

编写程序,查询世界上的所有中东国家的总人口

编写程序,查询世界上的平均寿命最长和最短的国家

实验代码

  • 主程序
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
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>1017520) {
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);
}
}
}
  • getyear
package MySQL;
import java.sql.*;
public class getyear {
public static void main(String[] args) {
Connection con;
Statement sql;
ResultSet rs;
con = GetDBConnection.connectDB("world","root","ldc990430");
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);
}
}
}
  • getPeople
package MySQL;

import java.sql.*;

public class getPeople {
public static void main(String[] args) {
Connection con;
Statement sql;
ResultSet rs;
con = GetDBConnection.connectDB("world","root","ldc990430");
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);
}
}
}

实验截图

  • 连接主程序

  • 城市

  • 寿命

  • 总人口

代码链接

20175206李得琛

MySQL课下作业的更多相关文章

  1. 数据库MySQL(课下作业,必做)

    数据库MySQL(课下作业,必做) 题目要求: 下载附件中的world.sql.zip, 参考http://www.cnblogs.com/rocedu/p/6371315.html#SECDB,导入 ...

  2. 20175221 曾祥杰 数据库MySQL(课下作业,必做)

    数据库MySQL(课下作业,必做) 题目要求: 1. 下载附件中的world.sql.zip, 参考http://www.cnblogs.com/rocedu/p/6371315.html#SECDB ...

  3. 20175314薛勐 数据库MySQL(课下作业,必做)

    数据库MySQL(课下作业,必做) 要求 下载附件中的world.sql.zip, 参考Intellj IDEA 简易教程:数据库,导入world.sql,提交导入成功截图 编写程序,查询世界上超过& ...

  4. 数据库MySQL(课下作业)

    一.作业要求 下载附件中的world.sql.zip, 参考http://www.cnblogs.com/rocedu/p/6371315.html#SECDB,导入world.sql,提交导入成功截 ...

  5. 数据库MySQL(课下作业,必做) 20175225

    作业要求: 1.下载附件中的world.sql.zip, 参考http://www.cnblogs.com/rocedu/p/6371315.html#SECDB,导入world.sql,提交导入成功 ...

  6. 学号20175313 《数据库MySQL(课下作业,必做)》第十周

    目录 一.题目要求 二.需求分析 三.关键代码以及运行结果截图 任务一 任务二 任务三 任务四 四.代码实现过程中遇到的问题及其解决方法 五.码云链接 六.心得体会 一.题目要求 下载附件中的worl ...

  7. 20175234 数据库MySQL(课下作业)

    20175234 数据库MySQL(课下作业) 内容: 1.下载附件中的world.sql.zip, 参考http://www.cnblogs.com/rocedu/p/6371315.html#SE ...

  8. 20175221 MyCP(课下作业,必做)

    MyCP(课下作业,必做) 任务详情 编写MyCP.java 实现类似Linux下cp XXX1 XXX2的功能,要求MyCP支持两个参数: - java MyCP -tx XXX1.txt XXX2 ...

  9. 20175221 《Java程序设计》迭代和JDB(课下作业,选做):

    20175221 <Java程序设计> 迭代和JDB(课下作业,选做): 任务详情 1 使用C(n,m)=C(n-1,m-1)+C(n-1,m)公式进行递归编程实现求组合数C(m,n)的功 ...

随机推荐

  1. python里面列表函数的使用及注意

    1.append 在末尾插入一个内容: a= [i for i in range(1,6)] print(a) a.append(100) print(a) 2.insert 指定位置插入内容 ins ...

  2. HDUST-1245 Interpreter(模拟)

    1245: Problem E: Interpreter 时间限制: 1 Sec  内存限制: 128 MB提交: 4  解决: 2[提交][状态][讨论版] 题目描述 Problem E: Inte ...

  3. java.lang.ClassNotFoundException: org.springframework.web.filter.CharacterEncodingFilter的解决方案

    白天在实验室的电脑上的项目搭起来,晚上回到宿舍发现跑不起来了,网上查到的大多不是想要的答案. 最终的解决方案是maven clean一下再重新package.

  4. 如何获取设置display:none元素及子元素的宽高

    由于元素设置了display:none时,页面便不会对其渲染,导致无法获取其元素的宽高.目前一般的做法都是先对其设置display:block,拿到数据再设置其为display:none.如此便可以了 ...

  5. 使用 tablib 来自动化管理测试用例,其他的工具都不用学了

    你在学习 python 自动化测试吗?听过 requests 库吗?tablib 是 requests 库作者常年维护的一个可以操作 Excel 等多种文件格式,将他们变成一种通用数据集的第三方库. ...

  6. selenium谷歌火狐插件安装

    1.首先ctrl+r进入终端输入(pip install selenium)进行python安装selenium2.打开百度浏览器进行分别输入geckodriver和Chromedriver对火狐和谷 ...

  7. RGB颜色值转换成十六进制

    function transferRgbToStr(color) { if (typeof color !== 'string' && !(color instanceof Strin ...

  8. vue.js(2)--v-cloak v-text v-html

    v-cloak v-text  v-html的使用 (1)实例 <!DOCTYPE html> <html lang="en"> <head> ...

  9. elementui 之el-pagination爬坑,属性pager-count的设定

    我想设置总页数为10页,页码条总是显示两个页码,其余省略号显示,我选择了pager-count,如下: <el-pagination @size-change="handleSizeC ...

  10. 2019-11-29-dotnet-core-使用-CoreRT-将程序编译为-Native-程序

    title author date CreateTime categories dotnet core 使用 CoreRT 将程序编译为 Native 程序 lindexi 2019-11-29 08 ...