目录

完成结果

要求 1 :导入world.sql

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

  • 截图:

要求 2 :CityWanna.java

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

  • 截图:

CityWanna.java

import java.sql.*;
import java.util.Scanner;
/**
* @author 10542
*/
public class CityWanna {
public static void main(String[] args) throws SQLException {
Connection con;
Statement sql;
ResultSet rs;
String url = "jdbc:mysql://localhost:3306/world";
String user = "root";
String password = "";
con = DriverManager.getConnection(url, user,password);
if (con == null) {
return;
}
//输入学号20175223得:5017522
//magicNumber[] 替换魔法值
int [] magicNumber = new int[]{10,1000000};
int studentId ,frist ,last;
System.out.println ("Input your student's id:");
Scanner reader = new Scanner (System.in);
studentId = reader.nextInt ();
frist = studentId/10;
last = studentId%10;
frist = frist + last*1000000;
if (frist/magicNumber[1]==magicNumber[0]) {
frist=(frist-10000000)+1000000;
}
else if (frist/magicNumber[1]>magicNumber[0]) {
frist=frist-10000000;
}
System.out.println ("Result:" +frist);
try {
//Statement sql = con.createStatement(); -> 向数据库发送SQL查询语句
sql = con.createStatement();
//ResultSet rs = sql.executeQuery(sqlStr); -> 处理查询结果
rs = sql.executeQuery("select*from city where population>"+Integer.toString (frist));
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 :CountryWanna.java

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

  • 截图:

CountryWanna.java

import java.sql.*;
/**
* @author 10542
*/
public class CountryWanna {
public static void main(String[] args) throws SQLException {
Connection con;
Statement sql;
ResultSet rs;
String uri = "jdbc:mysql://localhost:3306/world";
String user = "root";
String password = "";
con = DriverManager.getConnection(uri, user,password);
if (con == null) {
return;
}
try {
sql = con.createStatement();
rs = sql.executeQuery("select Name,Population from country where Region = 'Middle East'");
int allPopulation = 0;
while (rs.next()) {
String name = rs.getString(1);
int population = rs.getInt(2);
System.out.printf("The population of %s is %d\n", name, population);
allPopulation = allPopulation + population;
}
System.out.println("The population of Middle East" + allPopulation);
} catch (SQLException e) {
System.out.println("Error:" + e);
} }
}

要求 4 :LifeWanna.java

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

  • 截图:

LifeWanna.java

import java.sql.*;
/**
* @author 10542
*/
public class LifeWanna {
public static void main(String[] args) throws SQLException {
Connection con;
Statement sql;
ResultSet rs;
String uri = "jdbc:mysql://localhost:3306/world";
String user = "root";
String password = "";
con = DriverManager.getConnection(uri, user,password);
if (con == null) {
return;
}
try {
sql = con.createStatement();
rs = sql.executeQuery("select Name,LifeExpectancy from country order by LifeExpectancy");
/**
* rs.next() 跳读取下一行信息
* 若有,返回true,继续循环
* 若无,返回false,停止循环
*/
while (rs.next()) {
float life = rs.getInt(2);
String name;
//获取第一条数据的信息
rs.first();
while (life == 0) {
//获取下一条数据的信息
rs.next();
life = rs.getInt(2);
}
name = rs.getString(1);
System.out.println("The shortest life expectancy in the world:" + name);
System.out.println ("LifeExpectancy is:" + rs.getInt (2));
//获取最后一条数据的信息
rs.last();
name = rs.getString(1);
System.out.println("The longest life expectancy in the world:" + name);
System.out.println ("LifeExpectancy is:" + rs.getInt (2)); }
} catch (SQLException e) {
System.out.println("Error:" + e);
}
}
}

过程中问题及解决

1. XAMPP无法启用 MySQL 程序。

  • 问题 1 解决方法:

    在安装xampp之前电脑上装过mysql,然后默认启动的是以前的mysql。

    修改注册表:[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\MySQL]ImagePath 修改成新的xampp中位置 <xampp>\mysql\bin\mysqld MySQL

最新文章

  1. Linux平台oracle 11g单实例 安装部署配置 快速参考
  2. 卓越精Forsk.Atoll.v3.3.2.10366无线网络
  3. D3的参考样例
  4. C语言学习笔记(一)_hello world
  5. How To Use RUN_PRODUCT In Oracle Forms
  6. IOS 作业项目(3) 霓虹灯效果
  7. Java多线程技术学习笔记(一)
  8. Spring Cloud Netflix多语言/非java语言支持之Spring Cloud Sidecar
  9. Oracle的Recyclebin策略
  10. iOS 单利模式实现/优缺点
  11. jquery mobile 表单提交 图片/文件 上传
  12. 1.用互联网的产品思维打造一本app后端的书
  13. 【git】如何去解决fatal: refusing to merge unrelated histories
  14. Struts2_参数获得方式
  15. [Android] Android Studio 修改Gradle使用国内源
  16. Go 环境变量相关操作
  17. Educational Codeforces Round 14 A. Fashion in Berland 水题
  18. shiro+springmvc 都使用缓存
  19. [总结] Visual Studio 报价已经对比
  20. KnockoutJS Select 标签 Options绑定

热门文章

  1. PWA 应用
  2. 前端每日实战:103# 视频演示如何用纯 CSS 创作一只监视眼
  3. Python Django 编写一个简易的后台管理工具4-添加admin模版
  4. CENTER OS7关闭防火墙
  5. 【Python-Django讲义】针对django的ppt讲义
  6. QT简介及下载
  7. 设计模式--简单工厂(Simple Factory)
  8. Web 请求之--性能相关
  9. shell input value from console
  10. 2019HDU多校训练第二场 Longest Subarray