目录

完成结果

要求 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

20175223 MySQL的更多相关文章

  1. Hadoop 中利用 mapreduce 读写 mysql 数据

    Hadoop 中利用 mapreduce 读写 mysql 数据   有时候我们在项目中会遇到输入结果集很大,但是输出结果很小,比如一些 pv.uv 数据,然后为了实时查询的需求,或者一些 OLAP ...

  2. mysql每秒最多能插入多少条数据 ? 死磕性能压测

    前段时间搞优化,最后瓶颈发现都在数据库单点上. 问DBA,给我的写入答案是在1W(机械硬盘)左右. 联想起前几天infoQ上一篇文章说他们最好的硬件写入速度在2W后也无法提高(SSD硬盘) 但这东西感 ...

  3. LINUX篇,设置MYSQL远程访问实用版

    每次设置root和远程访问都容易出现问题, 总结了个通用方法, 关键在于实用 step1: # mysql -u root mysql mysql> Grant all privileges o ...

  4. nodejs进阶(6)—连接MySQL数据库

    1. 建库连库 连接MySQL数据库需要安装支持 npm install mysql 我们需要提前安装按mysql sever端 建一个数据库mydb1 mysql> CREATE DATABA ...

  5. MySQL高级知识- MySQL的架构介绍

    [TOC] 1.MySQL 简介 概述 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB公司开发,目前属于Oracle公司. MySQL是一种关联数据库管理系统,将数据保存在不同的表中,而 ...

  6. 闰秒导致MySQL服务器的CPU sys过高

    今天,有个哥们碰到一个问题,他有一个从库,只要是启动MySQL,CPU使用率就非常高,其中sys占比也比较高,具体可见下图. 注意:他的生产环境是物理机,单个CPU,4个Core. 于是,他抓取了CP ...

  7. 我的MYSQL学习心得(一) 简单语法

    我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(四) 数据类型 我的MYSQL学习心得(五) 运 ...

  8. Entity Framework Core 实现MySQL 的TimeStamp/RowVersion 并发控制

    将通用的序列号生成器库 从SQL Server迁移到Mysql 遇到的一个问题,就是TimeStamp/RowVersion并发控制类型在非Microsoft SQL Server数据库中的实现.SQ ...

  9. Docker笔记一:基于Docker容器构建并运行 nginx + php + mysql ( mariadb ) 服务环境

    首先为什么要自己编写Dockerfile来构建 nginx.php.mariadb这三个镜像呢?一是希望更深入了解Dockerfile的使用,也就能初步了解docker镜像是如何被构建的:二是希望将来 ...

随机推荐

  1. socket | netcat 模拟

    #!/opt/local/bin/python2.7 #coding=utf-8 ''' 取代netcat 两台主机中其中一台控制另一台 得到北控方的shell ''' import sys impo ...

  2. error C2065: ‘_bstr_t’ : undeclared identifier

    转自VC错误:http://www.vcerror.com/?p=828 问题描述: error C2065: '_bstr_t' : undeclared identifier 解决方法: 详细的解 ...

  3. 112、TensorFlow初始化变量

    # 创建一个变量 # 最简单创建一个变量的方法就是调用 tf.get_variable function import tensorflow as tf # 下面创建了一个三维的张量大小是 [1,2, ...

  4. SSAS MDX语句 期末查询简单示例

    WITH Member [Measures].[num Last Day of Month] AS( [时间].[YQMD].CurrentMember.LastChild,[Measures].[门 ...

  5. Asp.Net Core 第05局:读取配置

    前言 本文介绍Asp.Net Core 读取配置文件. 环境 1.Visual Studio 2017 2.Asp.Net Core 2.2 开局 前期准备             1.添加app.j ...

  6. HTML版简历

    页面预览 在线演示

  7. LeetCode 数组中两个数的最大异或值

    题目链接:https://leetcode-cn.com/problems/maximum-xor-of-two-numbers-in-an-array/ 题目大意: 略. 分析: 字典树 + 贪心. ...

  8. MySQL开启SSL认证,以及简单优化

    1.1 MySQL开启SSL认证 #生成一个 CA 私钥 [root@db01 ssl]# openssl genrsa 2048 > ca-key.pem Generating RSA pri ...

  9. QTP调用.net Framework类库实例

    1. 日期时间格式处理 set oDate = DotNetFactory.CreateInstance("System.DateTime").Parse("2-18-2 ...

  10. AttributeError: module 'requests' has no attribute 'get'的错误疑惑

    我发现文件直接用requests.get(url)会提示我AttributeError: module 'requests' has no attribute 'get' 我把问题百度了一下,解决方法 ...