mysql数据库项目作业_数据库MySQL(课下作业,必做)

一、任务详情

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

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

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

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

二、源代码

1.任务二源代码:

import java.sql.*;

public class SelectCity {

public static void main(String args[]) {

Connection con;

Statement sql;

ResultSet rs;

con = GetDBConnection.connectDB("world","root","");

if(con == null ) {

return;

}

String c3=" population >7017521";

String sqlStr =

"select * from city where "+c3;

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(e);

}

}

}

2.任务三源代码:

import java.sql.*;

public class SumPopulation {

public static void main(String args[]) {

Connection con;

Statement sql;

ResultSet rs;

con = GetDBConnection.connectDB("world","root","");

if(con == null ) {

return;

}

String c3=" Region='Middle East'";

String sqlStr =

"select * from country where "+c3;

try {

int sum=0;

sql=con.createStatement();

rs = sql.executeQuery(sqlStr);

while(rs.next()) {

int p=rs.getInt(5);

sum+=p;

}

System.out.println("世界上所有中东国家的总人口数为:"+sum);

con.close();

}

catch(SQLException e) {

System.out.println(e);

}

}

}

3.任务四源代码

import java.sql.*;

public class getLifeExpectancy {

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);

String name1,name2;

rs.first();

float f=rs.getFloat(8);

while (f==0.0){

rs.next();

f=rs.getFloat(8);

}

name1=rs.getString(2);

System.out.println("世界上的平均寿命最短的国家是:"+name1);

rs.last();

name2=rs.getString(2);

System.out.println("世界上的平均寿命最长的国家是:"+name2);

con.close();

}

catch(SQLException e) {

System.out.println(e);

}

}

}

三、运行截图

测试截图

打开数据库连接

7a57633fe5a73a496f28429ad2724403.png

留言

193b4e041dfc4c0cc6a004b73ca65140.png

1.任务一运行截图

1c36318f5adcf39ef08c2bb3aadac9e4.png

2.任务二运行截图

6c6cc53682d984e7fa0feb4d6da0a792.png

3.任务三运行截图

f95872df3f4e069fdf4d45cebddd7941.png

4.任务四运行截图

e3e3c588652655abb4d0856180abcada.png

SP2.参考资料

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值