1.在官網上下載並安裝MySQLhtml
2.在IDEA中輸入測試代碼ConnectionDemo.javajava
import static java.lang.System.out; import java.sql.*; public class ConnectionDemo { public static void main(String[] args) throws ClassNotFoundException, SQLException { Class.forName("com.mysql.jdbc.Driver"); String jdbcUrl = "jdbc:mysql://localhost:3306/demo"; String user = "root"; String passwd = ""; try(Connection conn = DriverManager.getConnection(jdbcUrl, user, passwd)) { out.printf("已%s數據庫鏈接%n", conn.isClosed() ? "關閉" : "打開"); } } }
1.使用老師推薦的XAMPP,在官網進行下載mysql
2.成功鏈接MySQL,以下圖
git
3.用瀏覽器輸入http://127.0.0.1
打開XAMPP。
sql
4.SQL創建數據庫:數據庫
CREATE SCHEMA demo;
5.創建數據庫表:瀏覽器
use demo; CREATE TABLE t_message ( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, name CHAR(20) NOT NULL, email CHAR(40), msg TEXT NOT NULL ) CHARSET=UTF8;
1.在官網https://dev.mysql.com/downloads/connector/j/ 上下載驅動學習
mysql-connector-java-5.1.41-bin.jar
2.在IDEA中添加驅動,以下圖:測試
3.運行示例代碼,顯示成功鏈接以下圖
設計
try{ Statement sql=con.createStatement(); } catch(SQLException e ){}
2.處理查詢結果
SQL查詢語句對數據庫的查詢操做將返回一個ResultSet
對象,ResultSet
對象是按「列」(字段)組織的數據行構成
ResultSet rs = sql.executeQuery("SELECT * FROM students");
下圖爲若干方法:
3.關閉鏈接:
con.close();
ResultSet對象和數據庫鏈接對象(Connection對象)實現了緊密的綁定,一旦鏈接對象被關閉,ResultSet對象中的數據馬上消失
4.順序查找
使用next()
方法移到下一個數據行
5.控制遊標
爲了獲得一個可滾動的結果集,需使用下述方法得到一個Statement對象
Statement stmt = con.createStatement(int type ,int concurrency);
經常使用的滾動查詢方法:
Public boolean absolute(int row):
將遊標移到參數row的指定行後Public int getRow();
獲得當前遊標所指向的行號6.條件與排序查找
通常格式: select 字段 from 表名 where 條件
(1) 字段值和固定值比較,例如:select name,height from mess where name='李四'
(2) 字段值在某個區間範圍,例如:select * from mess where height>1.60 and height<
=1.8
用order by
子語句對記錄排序
1.更新
update 表 set 字段 = 新值 where <條件子句>
2.添加
insert into 表(字段列表) values (對應的具體的記錄)
或
insert into 表 values (對應的具體的記錄)
3.刪除
delete from 表名 where <條件子句>
select * from mess where name like '%林%' order by name
教材學習有問題先去https://shimo.im/doc/1i1gldfsojIFH8Ip/看看,若是別人沒有提出相同問題,能夠編輯文檔添加,而後把本身提出的問題複製到下面:
計算機管理
->服務
中中止MySQL的進程import static java.lang.System.out; import java.sql.*; public class ConnectionDemo { public static void main(String[] args) throws ClassNotFoundException, SQLException { Class.forName("com.mysql.cj.jdbc.Driver"); String jdbcUrl = "jdbc:mysql://localhost:3306/demo?serverTimezone=UTC"; String user = "root"; String passwd = ""; try(Connection conn = DriverManager.getConnection(jdbcUrl, user, passwd)) { out.printf("已%s數據庫鏈接%n", conn.isClosed() ? "關閉" : "打開"); } } }
修改了兩部分
Class.forName("com.mysql. cj .jdbc.Driver");
爲根據錯誤提示添加驅動。String jdbcUrl = "jdbc:mysql://localhost:3306/demo ?serverTimezone=UTC ";
是根據錯誤提示添加time zone
信息。修改後成功鏈接
教材中代碼調試有問題先去https://shimo.im/doc/1i1gldfsojIFH8Ip/看看,若是別人沒有提出相同問題,能夠編輯文檔添加,而後把本身提出的問題複製到下面:
import static java.lang.System.out; import java.util.Scanner; public class MessageDAODemo { public static void main(String[] args) throws Exception { MessageDAO dao = new MessageDAO( "jdbc:mysql://localhost:3306/demo?" + "serverTimezone=UTC", "root", ""); Scanner console = new Scanner(System.in, "Big5"); while(true) { out.print("(1) 顯示留言 (2) 新增留言:"); switch(Integer.parseInt(console.nextLine())) { case 1: dao.get().forEach(message -> { out.printf("%d\t%s\t%s\t%s%n", message.getId(), message.getName(), message.getEmail(), message.getMsg()); }); break; case 2: out.print("姓名:"); String name = console.nextLine(); out.print("郵件:"); String email = console.nextLine(); out.print("留言:"); String msg = console.nextLine(); dao.add(new Message(name, email, msg)); } } } }
What is the output of the following code?(下面代碼的運行結果是?) LocalDate date = LocalDate.of(2018, Month.APRIL, 40); System.out.println(date.getYear() + " " + date.getMonth() + " "+ date.getDayOfMonth()); A .2018 APRIL 4 B .2018 APRIL 30 C .2018 MAY 10 D .Another date E .The code does not compile. F .A runtime exception is thrown.
**正解爲F,由於一個月沒有第四十天,因此報錯
** ## 結對及互評
教材學習中的問題和解決過程, 一個問題加1分
代碼調試中的問題和解決過程, 一個問題加1分
基於評分標準,我給本博客打分:XX分。得分狀況以下:xxx
xxx
xxx
代碼行數(新增/累積) | 博客量(新增/累積) | 學習時間(新增/累積) | 重要成長 | |
---|---|---|---|---|
目標 | 5000行 | 30篇 | 400小時 | |
第一週 | 6/6 | 1/1 | 20/20 | |
第二週 | 245/251 | 1/2 | 18/38 | |
第三週 | 633/884 | 1/3 | 22/60 | |
第四周 | 305/1189 | 1/4 | 30/90 | |
第五週 | 410/1599 | 3/7 | 30/120 | |
第六週 | 1135/2734 | 3/10 | 30/150 | |
第七週 | 781/3515 | 3/13 | 30/180 | |
第八週 | 710/4225 | 3/16 | 30/210 | |
第九周 | 775/5000 | 2/18 | 30/240 |
嘗試一下記錄「計劃學習時間」和「實際學習時間」,到期末看看能不能改進本身的計劃能力。這個工做學習中很重要,也頗有用。
耗時估計的公式
:Y=X+X/N ,Y=X-X/N,訓練次數多了,X、Y就接近了。
計劃學習時間:XX小時
實際學習時間:XX小時
改進狀況:
(有空多看看現代軟件工程 課件
軟件工程師能力自我評價表)