20175223 MySQL

目錄

  • 完成結果
    • 要求 1 :導入world.sql
    • 要求 2 :CityWanna.java
      • CityWanna.java
    • 要求 3 :CountryWanna.java
      • CountryWanna.java
    • 要求 4 :LifeWanna.java
      • LifeWanna.java
  • 過程中問題及解決
    • 1. XAMPP無法啟用 MySQL 程序。

目錄

完成結果

要求 1 :導入world.sql

下載附件中的world.sql.zip, 參考http://www.cnblogs.com/rocedu/p/6371315.html#SECDB,導入world.sql,提交導入成功截圖

  • 截圖:
    image.png

要求 2 :CityWanna.java

編寫程序,查詢世界上超過“你學號前邊七位并把最后一位家到最高位,最高位為0時置1”(比如學號20165201,超過3016520;學號20165208,超過1016520)的所有城市列表,提交運行結果截圖。

  • 截圖:
    image.png

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

編寫程序,查詢世界上的所有中東國家的總人口。

  • 截圖:
    image.png

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

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

  • 截圖:
    image.png

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 程序。

image.png

  • 問題 1 解決方法:
    在安裝xampp之前電腦上裝過mysql,然后默認啟動的是以前的mysql。
    修改注冊表:[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\MySQL]ImagePath 修改成新的xampp中位置 <xampp>\mysql\bin\mysqld MySQL

轉載于:https://www.cnblogs.com/Yogile/p/10815803.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/393854.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/393854.shtml
英文地址,請注明出處:http://en.pswp.cn/news/393854.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

2020運動相機推薦_2020年超有價值入門級微單相機推薦,超高性價比幾款入門級微單相機(選購指南)...

學習攝影專業已經3年多啦&#xff0c;自己喜歡拍攝照片&#xff0c;自己還幫助過一些想學習攝影的朋友快速入門&#xff0c;最近發現周圍學習攝影的朋友也越來越多了&#xff0c;有一些朋友咨詢關于入門微單相機的問題&#xff0c;想讓推薦幾款不錯的入門的微單相機。這篇文章帶…

javascript入門_JavaScript代理快速入門

javascript入門What is a JavaScript proxy? you might ask. It is one of the features that shipped with ES6. Sadly, it seems not to be widely used.什么是JavaScript代理&#xff1f; 你可能會問。 這是ES6附帶的功能之一。 可悲的是&#xff0c;它似乎并未得到廣泛使用…

linux缺少文件操作數,linux 文件的atime,ctime,mtime查看與修改

查看ls -a默認顯示的是修改時間ls -c / --timestatus / --timectime顯示的是狀態修改時間(即權限修改時間)ls -u / --timeuse / --timeaccess / --timeatime表示的是文件訪問時間修改touch: 缺少了文件操作數請嘗試執行“touch --help”來獲取更多信息。[weilocalhost ~]$ touc…

leetcode47. 全排列 II(回溯)

給定一個可包含重復數字的序列&#xff0c;返回所有不重復的全排列。 示例: 輸入: [1,1,2] 輸出: [ [1,1,2], [1,2,1], [2,1,1] ] 代碼 class Solution {List<List<Integer>> cListnew ArrayList<>();public List<List<Integer>> permuteUni…

linux 磁盤查看方式

fdisk (查看物理磁盤大小) df (查看文件系統&#xff0c;也就是正在使用磁盤大小) lsblk (查看邏輯磁盤大小)轉載于:https://www.cnblogs.com/MUQINGFENG123/p/10820345.html

ioslabel陰影,UILabel的內陰影

is it possible to create such a UILabel with inner and outer shadow?i only know shadowColor and shadowOffsetzoomed:thanks!解決方案The answer by dmaclach is only suitable for shapes that can easily be inverted. My solution is a custom view that works with …

Webpack初學者介紹

Webpack is a tool that lets you compile JavaScript modules. It’s also known as a module bundler.Webpack是使您可以編譯JavaScript模塊的工具。 也稱為模塊捆綁器 。 Given a large number of files, it generates a single file (or a few files) that run your app.給…

Android Coding利器之掌握小技巧,助你Coding更上一層樓~

本文講的是Android Coding利器之掌握小技巧&#xff0c;助你Coding更上一層樓~&#xff0c;話說前幾天在網上瀏覽到一大牛寫的關于Android布局優化的文章&#xff0c;看后感觸很深&#xff0c;回過頭看看自己寫過的代碼&#xff0c;發現還是有不少需要改進&#xff0c;今天找不…

linux系統報警怎么辦,常見Linux系統故障和解決方法

常見Linux系統故障和解決方法發布時間&#xff1a;2020-06-06 14:48:19來源&#xff1a;億速云閱讀&#xff1a;212作者&#xff1a;Leah欄目&#xff1a;云計算這篇文章給大家分享的是常見的Linux系統故障和解決方法。在使用系統的過程中總會有各種各樣的故障&#xff0c;所以…

Vuex 模塊化與項目實例 (2.0)

Vuex 強調使用單一狀態樹&#xff0c;即在一個項目里只有一個 store&#xff0c;這個 store 集中管理了項目中所有的數據以及對數據的操作行為。但是這樣帶來的問題是 store 可能會非常臃腫龐大不易維護&#xff0c;所以就需要對狀態樹進行模塊化的拆分。 首先貼出一個邏輯比較…

click js自動點擊 vue_vue.js2.0點擊獲取自己的屬性和jquery方法

如下所示&#xff1a;:data-index"index":dt"index"v-on:click"onclick($event,index)":data-d "JSON.stringify( item)"href"http://www.baidu.com" rel"external nofollow" rel"external nofollow"da…

Python:知識目錄

Python目錄 第一篇&#xff1a;數據類型部分文件操作 基礎數據類型---str 基礎數據類型---List 基礎數據類型---dict 基礎數據類型---set 基礎數據類型---bytes 數據類型的總結 文件操作------讀&#xff0c;寫 文件操作------使用方法 第二章&#xff1a;函數模塊 初識函數…

初學者css常見問題_5分鐘內學習CSS-初學者教程

初學者css常見問題關于網絡設計語言的快速教程。 (A quick tutorial on the design language of the web.) CSS (Cascading Style Sheets) is what makes web pages look good and presentable. It’s an essential part of modern web development and a must-have skill for …

leetcode39. 組合總和(回溯)

給定一個無重復元素的數組 candidates 和一個目標數 target &#xff0c;找出 candidates 中所有可以使數字和為 target 的組合。 candidates 中的數字可以無限制重復被選取。 說明&#xff1a; 所有數字&#xff08;包括 target&#xff09;都是正整數。 解集不能包含重復的…

一臉懵逼學習基于CentOs的Hadoop集群安裝與配置(三臺機器跑集群)

1&#xff1a;Hadoop分布式計算平臺是由Apache軟件基金會開發的一個開源分布式計算平臺。以Hadoop分布式文件系統&#xff08;HDFS&#xff09;和MapReduce&#xff08;Google MapReduce的開源實現&#xff09;為核心的Hadoop為用戶提供了系統底層細節透明的分布式基礎架構。 注…

linux批量去掉文件名前綴,linux 批量刪除某個前綴文件

1. 命令 (參考&#xff1a;https://blog.csdn.net/kl28978113/article/details/80271831)find ./ -name updatesites*-* -exec rm {} \;2. 舉例[rootadmin batch-create-sites]# ls2020-02-13-10-10.out logs-2020-04-07-08-00.out updatesites-2020-02-12-01-49-25.xlsx updat…

Docker - 避免啟動container后運行shell腳本執行完成后docker退出container

問題 最近在使用 Dockerfile 啟動容器&#xff0c;發現使用Dockerfile調用容器里面的shell&#xff0c;當shell執行完成以后&#xff0c;docker會退出容器。 分析 Docker 在執行shell的時候&#xff0c;是在后臺執行的&#xff1b;因此&#xff0c;在shell執行完成以后&#xf…

css畫橫線箭頭_用CSS繪制三角形箭頭

用CSS繪制三角形箭頭。使用純CSS&#xff0c;你只需要很少的代碼就可以創作出各種瀏覽器都兼容的三角形箭頭&#xff01;CSS代碼:/* create an arrow that points up */div.arrow-up {width: 0;height: 0;border-left: 5px solid transparent; /* left arrow slant */border-ri…

Jmeter參數化的理解

jmeter參數化有兩種情況&#xff1a; jmeter執行的sql語句中值的參數化&#xff08;如select過濾條件&#xff09;csv data set config參數表示方式${zjhm}jmx腳本的設置屬性參數化&#xff0c;方便命令行調用時修改參數&#xff08;如并發量、執行時間&#xff09;在腳本中調用…

leetcode216. 組合總和 III(回溯)

找出所有相加之和為 n 的 k 個數的組合。組合中只允許含有 1 - 9 的正整數&#xff0c;并且每種組合中不存在重復的數字。 說明&#xff1a; 所有數字都是正整數。 解集不能包含重復的組合。 示例 1: 輸入: k 3, n 7 輸出: [[1,2,4]] 代碼 class Solution {List<List…