java jdbc 兩層輕量封裝

package db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import dao.DemoDao;

public class DB {

    private final static String username = "root";
    private final static String password = "tiger";
    private final static String url = "jdbc:mysql://localhost/test?characterEncoding=utf8&useSSL=false&serverTimezone=UTC";

    private final static String url1 = "jdbc:mysql://localhost/yaycrawler?characterEncoding=utf8&useSSL=false&serverTimezone=UTC";

    public static Connection getConnection() throws SQLException {

        Connection conn = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");// 加載jdbc驅動
            // 加載完後可以通過DriverManager獲取數據庫連接
            conn = DriverManager.getConnection(url, username, password);
            conn.setAutoCommit(false);// 關閉事務自動提交
        } catch (ClassNotFoundException e) {
            System.out.println("找不到驅動");
            e.printStackTrace();
            return null;
        }
        return conn;
    }

    // 關閉連接方法
    public static void closeConnection(Connection conn) throws SQLException {
        if (conn != null) {
            conn.close();
        }
    }

    // 關閉結果集方法
    public static void closeResultSet(ResultSet rs) throws SQLException {
        if (rs != null) {
            rs.close();
        }
    }

    // 關閉查詢器方法
    public static void closePreparedStatement(PreparedStatement ps) throws SQLException {
        if (ps != null) {
            ps.close();
        }
    }

    public static List<Map<String, Object>> toMap(ResultSet rs) throws SQLException {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        int maxCol;
        ResultSetMetaData rsmd = rs.getMetaData();
        maxCol = rsmd.getColumnCount();
        // System.out.println("查詢時指定列的數量,爲*時則是表的列數maxCol: " + maxCol);

        while (rs != null && rs.next()) {// 通過result.next()遍歷結果
            Map<String, Object> map = new LinkedHashMap<String, Object>();
            int row = rs.getRow();
            System.out.print("當前遍歷的行數: " + row + " ");
            // 第一列的列號爲1,所以i最小爲1
            for (int i = 1; i <= maxCol; i++) {
                System.out.print(rsmd.getColumnName(i) + " " + rs.getString(i) + " ");
                // 存入列名和值
                map.put(rsmd.getColumnName(i), rs.getString(i));
            }
            System.out.println("");
            list.add(map);

        }
        DB.closeResultSet(rs);// 結果集用完後關閉
        return list;
    }

    public static void main(String[] args) throws SQLException {
        Connection conn = DB.getConnection();

        DemoDao.insertTest(conn, "我是用戶名");

        conn.commit();// 更新操作要提交事務才能生效

        List<Map<String, Object>> list = DemoDao.findTest(conn);
        for (Map<String, Object> map : list) {
            System.out.print(map.get("id") + " " + map.get("username") + " " + map.get("update_time") + " "
                    + map.get("create_time"));
            System.out.println();
        }

        DB.closeConnection(conn);

    }

}

// 查詢時指定列的數量,爲*時則是表的列數maxCol: 4
// 當前遍歷的行數: 1 id 1 username user1 update_time 2017-05-29 10:19:39 create_time
// 2017-05-29 10:19:39
// 當前遍歷的行數: 2 id 2 username user2 update_time 2017-05-29 10:20:15 create_time
// 2017-05-29 10:20:15
// 當前遍歷的行數: 3 id 5 username user2 update_time 2017-05-29 10:22:12 create_time
// 2017-05-29 10:22:12
// 當前遍歷的行數: 4 id 12 username user3 update_time 2017-05-29 10:42:10 create_time
// 2017-05-29 10:42:10
// 當前遍歷的行數: 5 id 13 username user3 update_time 2017-05-29 10:42:12 create_time
// 2017-05-29 10:42:12
// 當前遍歷的行數: 6 id 14 username user3 update_time 2017-05-29 10:42:17 create_time
// 2017-05-29 10:42:17
// 當前遍歷的行數: 7 id 15 username user3 update_time 2017-05-29 10:43:34 create_time
// 2017-05-29 10:43:34
// 當前遍歷的行數: 8 id 16 username user3 update_time 2017-05-29 10:43:42 create_time
// 2017-05-29 10:43:42
// 當前遍歷的行數: 9 id 17 username user3 update_time 2017-05-29 10:44:33 create_time
// 2017-05-29 10:44:33
// 當前遍歷的行數: 10 id 18 username user3 update_time 2017-05-29 10:44:42 create_time
// 2017-05-29 10:44:42
// 當前遍歷的行數: 11 id 19 username user3 update_time 2017-05-29 10:56:58 create_time
// 2017-05-29 10:56:58
// 當前遍歷的行數: 12 id 20 username user3 update_time 2017-05-29 11:28:13 create_time
// 2017-05-29 11:28:13
// 當前遍歷的行數: 13 id 21 username user3 update_time 2017-05-29 11:29:02 create_time
// 2017-05-29 11:29:02
// 當前遍歷的行數: 14 id 22 username user3 update_time 2017-06-22 13:02:19 create_time
// 2017-06-22 13:02:19
// 當前遍歷的行數: 15 id 23 username user3 update_time 2017-06-22 19:27:53 create_time
// 2017-06-22 19:27:53
// 當前遍歷的行數: 16 id 24 username user3 update_time 2017-06-22 19:28:01 create_time
// 2017-06-22 19:28:01
// 當前遍歷的行數: 17 id 25 username user3 update_time 2017-06-22 19:28:03 create_time
// 2017-06-22 19:28:03
// 當前遍歷的行數: 18 id 26 username user3 update_time 2017-06-22 19:28:05 create_time
// 2017-06-22 19:28:05
// 當前遍歷的行數: 19 id 27 username user3 update_time 2017-06-22 19:28:12 create_time
// 2017-06-22 19:28:12
// 當前遍歷的行數: 20 id 28 username user3 update_time 2017-06-24 00:10:06 create_time
// 2017-06-24 00:10:06
// 當前遍歷的行數: 21 id 29 username user3 update_time 2017-06-24 00:11:50 create_time
// 2017-06-24 00:11:50
// 當前遍歷的行數: 22 id 30 username user3 update_time 2017-06-24 00:13:24 create_time
// 2017-06-24 00:13:24
// 1 user1 2017-05-29 10:19:39 2017-05-29 10:19:39
// 2 user2 2017-05-29 10:20:15 2017-05-29 10:20:15
// 5 user2 2017-05-29 10:22:12 2017-05-29 10:22:12
// 12 user3 2017-05-29 10:42:10 2017-05-29 10:42:10
// 13 user3 2017-05-29 10:42:12 2017-05-29 10:42:12
// 14 user3 2017-05-29 10:42:17 2017-05-29 10:42:17
// 15 user3 2017-05-29 10:43:34 2017-05-29 10:43:34
// 16 user3 2017-05-29 10:43:42 2017-05-29 10:43:42
// 17 user3 2017-05-29 10:44:33 2017-05-29 10:44:33
// 18 user3 2017-05-29 10:44:42 2017-05-29 10:44:42
// 19 user3 2017-05-29 10:56:58 2017-05-29 10:56:58
// 20 user3 2017-05-29 11:28:13 2017-05-29 11:28:13
// 21 user3 2017-05-29 11:29:02 2017-05-29 11:29:02
// 22 user3 2017-06-22 13:02:19 2017-06-22 13:02:19
// 23 user3 2017-06-22 19:27:53 2017-06-22 19:27:53
// 24 user3 2017-06-22 19:28:01 2017-06-22 19:28:01
// 25 user3 2017-06-22 19:28:03 2017-06-22 19:28:03
// 26 user3 2017-06-22 19:28:05 2017-06-22 19:28:05
// 27 user3 2017-06-22 19:28:12 2017-06-22 19:28:12
// 28 user3 2017-06-24 00:10:06 2017-06-24 00:10:06
// 29 user3 2017-06-24 00:11:50 2017-06-24 00:11:50
// 30 user3 2017-06-24 00:13:24 2017-06-24 00:13:24
package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import db.DB;

public class DemoDao {
    // 插入Test表數據
    public static int insertTest(Connection conn, String username) {
        PreparedStatement ps = null;
        String sql = "INSERT INTO test (username,create_time) VALUES (?,NOW())";
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, username);
            return ps.executeUpdate();
        } catch (SQLException e) {
            try {
                conn.rollback();//回滾事務
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
            return 0;
        } finally {
            try {
                DB.closePreparedStatement(ps);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    // 查詢test表數據
    public static List<Map<String, Object>> findTest(Connection conn) {
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "select id,username,update_time,create_time from test";
        try {
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            return DB.toMap(rs);
        } catch (SQLException e) {
            try {
                conn.rollback();//回滾事務
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
            return null;
        } finally {
            try {
                DB.closePreparedStatement(ps);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章