201621123063《java程式設計》第14周實驗總結

來源:互聯網
上載者:User

標籤:圖資料庫   系統   連續   失敗   ext   amp   注釋   play   jtable   

1. 本周學習總結1.1 以你喜歡的方式(思維導圖或其他)歸納總結與資料庫相關內容。

2. 使用資料庫技術改造你的系統2.1 簡述如何使用資料庫技術改造你的系統。要建立什麼表?你的表設計。

圖書借閱系統使用資料庫儲存館藏圖書資訊,要建立圖書資訊表。

id作為主鍵

將圖書表從資料庫中讀出:
資料庫圖表:

成功讀取並放入JTable顯示:

2.2 系統中使用到了JDBC中什麼關鍵類?

Connection:獲得與資料庫的串連
DriverManger:驅動管理器,註冊驅動
PreparedStatement:Statement子類,先行編譯SQL語句後讓資料庫執行
ResultSet:擷取查詢結果集

2.3 資料庫相關模組的關鍵代碼。關鍵行需要加註釋。

3. 代碼量統計3.1 統計本周完成的代碼量

需要將每周的代碼統計情況融合到一張表中。

周次 行數 新增行數 檔案數 新增檔案數
1 226 226 45 45
2 377 377 7 7
3 712 281 42 9
4 166 166 2 2
5 253 253 4 4
6 484 484 9 9
7 269 269 1 1
8 301 32 5 4
9 447 447 7 7
10 401 26 9 0
11 1262 1262 18 18
12 965 965 12 12
13 887 887 9 9
14 675 675 10 10
選做:4. 資料庫學習指導1. MySQL資料庫基本操作

建立資料庫,將自己的姓名、學號作為一條記錄插入。(,需出現自己的學號、姓名)

在自己建立的資料庫上執行常見SQL語句

2. 使用JDBC串連資料庫與Statement2.1 使用Statement操作資料庫。完成實驗任務書-題目2。其中的public static void displayAll()與public static int insert(Student stu)的關鍵代碼並出現自己的學號。

2.2 如果要完成根據指定姓名查詢學生資料,即完成函數public Student findStuByName(String name),其中的sql語句怎麼寫?
String sql = "select * from students WHERE name = "+name;
2.3 你認為使用JDBC操作資料庫的套路是什嗎?

①載入資料庫驅動
②建立資料庫連接
③建立資料庫操作對象
④定義操作的SQL語句
⑤執行資料庫操作
⑥擷取並操作結果集
⑦close()關閉對象
參考:實驗任務書-題目2

3. PreparedStatement與參數化查詢3.1 使用PreparedStatement,編寫public Student findStuByName(String name),實現根據name進行尋找, 。(粘貼關鍵代碼及運行,運行中需出現學號,比較2.2,說明兩種實現的不同)
public static Student findStuByName(String name)    {        String URL = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false";        String userName = "root";        String password = "123456";        Connection conn = null;        PreparedStatement preparedStatement = null;        ResultSet resultSet = null;        Student student = null;        String sql = "select*from students where name = ?";                try {            conn = DriverManager.getConnection(URL, userName, password);            preparedStatement = conn.prepareStatement(sql);            preparedStatement.setString(1, name);            resultSet = preparedStatement.executeQuery();            while(resultSet.next())            {                student = new Student(resultSet.getInt("id"),resultSet.getString("stuno"),resultSet.getString("name"),resultSet.getInt("age"),resultSet.getDate("birthdate"));            }        } catch (SQLException e) {            e.printStackTrace();        } finally {             if (resultSet != null) {                    try {                        resultSet.close();                        resultSet.close();                    } catch (SQLException e) {                        e.printStackTrace();                    }                }                if (preparedStatement != null) {                    try {                        preparedStatement.close();                        preparedStatement = null;                    } catch (SQLException e) {                        e.printStackTrace();                    }                }                if (conn != null)                    try {                        conn.close();                        conn = null;                    } catch (SQLException e) {                        e.printStackTrace();                    }         }            return student;    }

運行:

不同:PreparedStatement 的執行效率高於Statement,Statement每次執行sql語句,相關資料庫都要執行sql語句的編譯,Preparedstatement是先行編譯的。

3.2 如果要實現模糊尋找,怎麼修改?比如編寫public List
public static List<Student> findStusByName(String name)    {        String URL = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false";        String userName = "root";        String password = "123456";        Connection conn = null;        PreparedStatement preparedStatement = null;        ResultSet resultSet = null;        String sql = "select*from students where name like ?";        List<Student> students = new ArrayList<>();        try {            conn = DriverManager.getConnection(URL, userName, password);            preparedStatement = conn.prepareStatement(sql);            preparedStatement.setString(1, "%"+name+"%");            resultSet = preparedStatement.executeQuery();            while(resultSet.next())            {                Student student = new Student(resultSet.getInt("id"),resultSet.getString("stuno"),resultSet.getString("name"),resultSet.getInt("age"),resultSet.getDate("birthdate"));                students.add(student);            }        } catch (SQLException e) {            e.printStackTrace();        } finally {             if (resultSet != null) {                    try {                        resultSet.close();                        resultSet.close();                    } catch (SQLException e) {                        e.printStackTrace();                    }                }                if (preparedStatement != null) {                    try {                        preparedStatement.close();                        preparedStatement = null;                    } catch (SQLException e) {                        e.printStackTrace();                    }                }                if (conn != null)                    try {                        conn.close();                        conn = null;                    } catch (SQLException e) {                        e.printStackTrace();                    }         }        return students;    }

測試資料:

運行:

6. 交易處理6.1 使用代碼與運行結果證明你確實實現了交易處理功能,並說明交易處理時在編碼時需要注意哪些?注意:要比較運行成功與運行失敗兩種情況下對資料庫的更改情況。(粘貼一段你認為比較有價值的代碼,出現學號)


測試資料:

更改前:

更改後:

運行失敗:資料庫中資料沒有變化

注意:資料庫操作要分步進行,不能同時進行多個操作

6.2 你覺得什麼時候需要使用交易處理?

對資料庫的資料進行連續操作時,為了保證資料的一致性和正確性,需要使用交易處理。當對資料庫的資料進行操作失敗時,交易處理可以讓所有的資料復原到進行操作之前的資料。

選做:5.課外閱讀5.2 代碼結構中Dao,Service,Controller,Util,Model是什麼意思,為什麼劃分

Dao: Data Access Object,負責資料存取
Service:服務,完成一些指定的工作
Controller:控制器,所有的指令都由控制器發出
Util:工具,常用來處理和商務邏輯沒有關係的資料
Model:模型,描述了一個類型資料的定義,通常是各資料的集合

劃分好處:
①劃分代碼結構可以讓代碼具有良好的可移植性
②可以讓多人協作並行開發更容易
③開發時能對程式的結構有更清晰的概念

5.3 mysq資料庫管理工具navicat基本使用方法


可以直接在可視化圖表中對資料進行一些常規操作,在查詢中可以用代碼語句對資料庫進行增刪改查和事務控制

201621123063《java程式設計》第14周實驗總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.