作业要求:

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的更多相关文章

  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. 学号20175313 《数据库MySQL(课下作业,必做)》第十周

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

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

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

  6. MySQL课下作业

    目录 MySQL 实验内容 实验代码 实验截图 代码链接 MySQL 实验内容 下载附件中的world.sql.zip, 参考http://www.cnblogs.com/rocedu/p/63713 ...

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

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

  8. 20175333曹雅坤MyCP(课下作业,必做)

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

  9. ###20175311MyCP(课下作业,必做)

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

随机推荐

  1. [LeetCode] 227. 基本计算器 II

    题目链接: https://leetcode-cn.com/problems/basic-calculator-ii 难度:中等 通过率:33.2% 题目描述: 实现一个基本的计算器来计算一个简单的字 ...

  2. event.target和event.currentTarget的区别----0605加深理解

    target:触发事件的元素.currentTarget:事件绑定的元素.两者在没有冒泡的情况下,是一样的值,但在用了事件委托的情况下,就不一样了,例如: <ul id="ulT&qu ...

  3. 03-Spring基于xml的IOC配置--spring的依赖注入

    1.概念 依赖注入:Dependency Injection(简称DI注入).它是 spring 框架核心 ioc 的具体实现. 简单理解:可以在一个类中不通过new的方式依赖其它对象.目的是为了解耦 ...

  4. laravel 的lnmp 的配置

    装了lnmp后,一般用 lnmp vhost add 添加网站 一般 只用重写和ssl功能 再发laravel官方的配置 server { listen 80; server_name example ...

  5. IBM产品系列和AIX系统版本

      AIX系统版本 AIX 7.2 No supported AIX levels. AIX 7.1 Technology Level     Base Level     Recommended L ...

  6. tensorflow 学习教程

    tensorflow 学习手册 tensorflow 学习手册1:https://cloud.tencent.com/developer/section/1475687 tensorflow 学习手册 ...

  7. Qt常见错误

    fatal error: QApplication: No such file or directory 在.pro文件中 添加 QT += widgets fatal error: QTcpSock ...

  8. xavier_uniform/xavier_normal

    import math from torch.autograd import Variable import torch import torch.nn as nn import warnings w ...

  9. 上传图片,正在加载,loading

    https://blog.csdn.net/yansong_8686/article/details/50361573

  10. deepin 常见快捷键及常用命令

    常用命令 1)安装软件命令行:dpkg -i <.deb file name>示例:dpkg -i avg71flm_r28-1_i386.deb2)安装一个目录下面所有的软件包命令行:d ...