數據庫MySQL(課下做業,必作) 20175225

做業要求:

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類因爲系統版本不一樣,改成html

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

步驟

1、world.sql

1.下載相關附件並解壓。
2.在數據庫單擊右鍵,運行sql文件,選擇sql文件,點擊開始。
3.導入完成後,從新打開鏈接會有顯示。
java

2、編寫程序,查詢世界上超過「你學號前邊七位並把最後一位家到最高位,最高位爲0時置1」

1.學號爲20175225,查詢爲超過7017522
2.實驗代碼mysql

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.相關截圖
sql

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.截圖
3d

4、編寫程序,查詢世界上的平均壽命最長和最短的國家

1.相關代碼code

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.截圖
server

相關文章
相關標籤/搜索