jdbc增刪改查進行封裝/數據庫工具類的終極封裝

package javacto.taobao.com;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class DBUtil {
    private  static String driverName;
    private  static String url;
    private  static String usreName;
    private  static String pwd;

    static {
        //加載類之前就會執行
        //調用init()方法
        init();
    }

    private static void init() {
        Properties ps = new Properties();
        String path = "db.properties";
        InputStream is = DBUtil.class.getClassLoader().getResourceAsStream(path);
        try {
            ps.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //請各位同學務必打印輸出 值 是否正確
        System.out.println(ps.get("db.driverName"));
        System.out.println(ps.get("db.url"));
        System.out.println(ps.get("db.username"));
        System.out.println(ps.get("db.pwd"));

        driverName= (String) ps.get("db.driverName");
        url= (String) ps.get("db.url");
        usreName= (String) ps.get("db.username");
        pwd= (String) ps.get("db.pwd");
    }

    /**
     * 建立連接
     * @return
     */
    public static Connection getConnection(){
        //訪問網址:javacto.taobao.com
        Connection conn = null;
        try {
            //1.加載驅動  DriverManager
            Class.forName(driverName);
            //2.建立連接   Connection
            conn = DriverManager.getConnection(url,usreName,pwd);

        }catch (Exception  e ){
            e.printStackTrace();
        }
        return conn;
    }


    /**
     * 釋放資源
     * PreparedStatement extends Statement
     */
    public static void closeAll(Connection conn, Statement pstm, ResultSet rs){
        try {
            if(rs!=null){
                rs.close();
            }

            if(pstm!=null){
                pstm.close();
            }
            if(conn!=null){
                conn.close();
            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 封裝增刪改
     * 訪問網址:javacto.taobao.com
     * @return
     */
    public static   int   executeUpdate(String sql,Object agrs[]){
        int num=0;

        //1.定義變量
        Connection conn =  null;
        PreparedStatement pstm =null;
        try{
            //2.建立連接   Connection
            conn = getConnection();
            // 3.PreparedStatement 處理預編譯sql語句

            //把之前的 DML隨便一個複製過來,然後修改兩步即可
            pstm = conn.prepareStatement(sql);
            // 4.給?賦值
            //我也不知道 Object agrs[] 變量中 會有 多個參數  直接循環,有就就會循環
            for(int i = 0;i<agrs.length;i++){
                pstm.setObject(i+1,agrs[i]);
            }
            // 5.執行預編譯sql語句  DML executeUpdate ()  返回int類型
            num = pstm.executeUpdate();

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            // 6.釋放資源
            closeAll(conn,pstm,null);
        }
        return num;

    }

    /**
     * 列表查詢
     * javacto.taobao.com 訪問網址
     */
    public  static<T> List<T> queryListExecute(String sql, ArrayList<Object> paras, Class<T> cls ){
        Connection conn = getConnection();
        PreparedStatement pst = null;
        ResultSet rs = null;
        T singleObject = null;
        int index = 1;
        List <T> list = new ArrayList<T>();
        try {
            pst = conn.prepareStatement(sql);
            if(paras !=null && paras.size()>0) {
                pst.clearParameters();
                for(int i = 0;i<paras.size();i++) {
                    pst.setObject(index++, paras.get(i));
                }
            }
            rs = pst.executeQuery();
            ResultSetMetaData rsmd = rs.getMetaData();
            int columnCount = rsmd.getColumnCount();
            while(rs.next()) {
                singleObject = cls.newInstance();
                for(int i = 0;i<columnCount;i++) {
                    String columnName = rsmd.getColumnName(i+1);
                    Object columdValue = rs.getObject(columnName);
                    Field field = cls.getDeclaredField(columnName);
                    field.setAccessible(true);
                    field.set(singleObject, columdValue);
                }
                list.add(singleObject);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            closeAll(conn, pst, rs);
        }

        return list;
    }
}

db.properties文件中代碼

db.driverName=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/javacto
db.username=root
db.pwd=學習更多請訪問:javacto.taobao.com

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章