201521123074 《Java程序設計》第14周學習總結

1. 本週學習總結

1.1 以你喜歡的方式(思惟導圖或其餘)概括總結多數據庫相關內容。
mysql


2. 書面做業

Q1. MySQL數據庫基本操做
創建數據庫,將本身的姓名、學號做爲一條記錄插入。(截圖,需出現本身的學號、姓名)
在本身創建的數據庫上執行常見SQL語句(截圖)

-參考:實驗任務書-題目1sql

一、創建數據庫語句:

二、數據庫創建成功:

三、添加學生信息語句:

四、查詢學生信息語句:
數據庫


Q2. 使用JDBC鏈接數據庫與Statement安全

2.1 使用Statement操做數據庫。(粘貼一段你認爲比較有價值的代碼,出現學號)學習

使用Statement操做數據庫,展示數據庫中學生的id/stuno/birthdate信息,以下圖:
url

2.2 使用JDBC操做數據庫主要包含哪幾個步驟?
-參考:實驗任務書-題目2code

一、安裝驅動;
二、創建鏈接、connection;
三、statement語句;
四、RresultSet結果集;
五、Release關閉釋放資源。orm


Q3. PreparedStatement與參數化查詢
參考:實驗任務書-題目3
3.1 使用PreparedStatement根據用戶指定的查詢條件進行查詢。(粘貼一段你認爲比較有價值的代碼,出現學號)blog

//201521123074
Connection con = null;
        PreparedStatement pStatement = null;
        ResultSet rs = null;
        SimpleDateFormat hmFromat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String driverName = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=GBK";
        String userName = "root";
        String password = "gsjy1998041";
        try {
            con = DriverManager.getConnection(url, userName, password);
            String strSql = "select * from students where name = ?";
            pStatement = con.prepareStatement(strSql);
            pStatement.setString(1,name);
            rs = pStatement.executeQuery();
            while (rs.next()) {
                System.out.println(rs.getString("stuno") + "\t");
                System.out.println(rs.getString("name") + "\t");
                System.out.println(rs.getString("gender") + "\t");
                System.out.println(rs.getInt("age") + "\t");
                System.out.println(rs.getDate("birthdate") + "\t");
                System.out.println(rs.getString("major") + "\t");
            }
            pStatement.close();

3.2 批量更新-批量插入1000個學生,統計整個操做所消耗的時間。(使用方法executeBatch)
資源

啊。。這題總是會出現這樣的錯誤T _T運行不了因此測不了時間。。。


Q4. JDBCUtil與DAO
參考:實驗任務書-題目5

4.1 粘貼一段你認爲比較有價值的代碼,出現學號

public class JDBCUtil {
//201521123074
    private static String driverName = "com.mysql.jdbc.Driver";// jdbc4.0之後不須要
    private static String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=GBK";
    private static String userName = "root";
    private static String password = "123456";

    public static void registerDriver() {
        try {
            Class.forName(driverName);// jdbc4.0之前須要這句進行驅動註冊
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.out.println("找不到驅動");
        }
    }

    public static Connection getConnection() throws SQLException {
        Connection conn = null;
        System.out.println("正在鏈接數據庫...");
        conn = DriverManager.getConnection(url, userName, password);
        System.out.println("數據庫已鏈接!");
        return conn;

    }

    public static void closeConnection(Connection conn) {
        System.out.println("正在釋放全部資源...");
        
            if (conn != null) {
                try {
                    conn.close();
                    conn = null;
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        
    }
    
    /*
     * 釋放全部資源
     */
    public static void realeaseAll(ResultSet rs,Statement st,Connection conn){
        if(rs!=null){
            try {
                rs.close();
                rs = null;
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (st!=null){
            try {
                st.close();
                st = null;
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        closeConnection(conn);
    }

4.2 使用DAO模式訪問數據庫有什麼好處?

各個層的代碼分開寫,思路要清晰些,並且方便維護.DAO存在大部分是爲了理清思路,代碼簡潔易懂。


Q5. 使用數據庫改造購物車系統

5.1 使用數據庫改造之前的購物車系統(應有圖形界面)。若是之前爲完成購物車系統,可編寫基於數據庫的學生管理系統。包括對學生的增刪改查,要求使用。

5.2 相比較使用文件,使用數據庫存儲與管理數據有何不同?

文件儲存比較方便管理,但是安全性很低;數據庫存儲比較複雜點,安全性高。


3. 碼雲

3.1. 碼雲代碼提交記錄

在碼雲的項目中,依次選擇「統計-Commits歷史-設置時間段」, 而後搜索並截圖

相關文章
相關標籤/搜索