20175221 曾祥杰 数据库MySQL(课下作业,必做)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了20175221 曾祥杰 数据库MySQL(课下作业,必做),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3018字,纯文字阅读大概需要5分钟。
内容图文
![20175221 曾祥杰 数据库MySQL(课下作业,必做)](/upload/InfoBanner/zyjiaocheng/913/99e147136c834cd29af54871a05c4b98.jpg)
数据库MySQL(课下作业,必做)
题目要求:
1. 下载附件中的world.sql.zip, 参考http://www.cnblogs.com/rocedu/p/6371315.html#SECDB,导入world.sql,提交导入成功截图
2. 编写程序,查询世界上超过“你学号前边七位并把最后一位家到最高位,最高位为0时置1”(比如学号20165201,超过3016520;学号20165208,超过1016520)的所有城市列表,提交运行结果截图
3. 编写程序,查询世界上的所有中东国家的总人口
4. 编写程序,查询世界上的平均寿命最长和最短的国家
实现步骤:
第一部分:
- 打开MySQL,在数据库名字上右击选择“运行SQL文件”,在弹出的对话框中选择导入路径
- 点击“开始”开始导入,导入成功截图如下
第二部分:
- 我的学号为:20175221,所以应该查询超过3017522的城市
- 代码如下:
import java.sql.*; public class City { public static void main(String[] args) { Connection con; Statement sql; ResultSet rs; con = GetDBConnection.connectDB("world","root","zxja31415926"); if(con == null) { return; } try { sql=con.createStatement(); rs = sql.executeQuery("SELECT * FROM city where Population>3017522"); 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>3017522) { 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); } } }
- 关键语句: rs = sql.executeQuery("SELECT * FROM city where Population>3017522");
- 测试截图
第三部分:
- 代码如下:
import java.sql.*; public class Population { public static void main(String[] args) { Connection con; Statement sql; ResultSet rs; con = GetDBConnection.connectDB("world","root","zxja31415926"); 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); } } }
- 测试截图:
第四部分:
- 代码如下:
-
import java.sql.*; public class Life { public static void main(String[] args) { Connection con; Statement sql; ResultSet rs; con = GetDBConnection.connectDB("world","root","zxja31415926"); 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); } } }
- 测试截图:
-
码云链接
-
心得体会:
- 这次的学习内容,其实相当于是对上周学习数据库的一次简单检测,整体来说难度不大,设计思路大同小异,模仿着课本做就差不多出来了。
内容总结
以上是互联网集市为您收集整理的20175221 曾祥杰 数据库MySQL(课下作业,必做)全部内容,希望文章能够帮你解决20175221 曾祥杰 数据库MySQL(课下作业,必做)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。