java-jdbc封裝串連資料庫工具

來源:互聯網
上載者:User

標籤:string   影響   使用   通過   prepare   uil   update   資料庫   on()   

在進行資料庫連接時,我們往往進行的是資料庫的串連,關閉操作,執行sql語句書寫在Dao層中操作

現對資料庫操作進行封裝,通過動態參數進行傳遞對象,

通過自建泛型實現返回類的接收,現程式如下:

/************************************************************   Copyright (C), 1988-1999, Huawei Tech. Co., Ltd.   FileName:JDBCUtil.java   Author:   Light     Version :   version 1.0       Date:       2018/7/12Description:      jdbc連結資料庫的封裝// 模組描述         Version:         通過模組化實現jdbc串連資料庫操作,減少開發代碼關於資料庫操作的編寫量// 版本資訊   Function List:     // 主要函數及其功能     1.getConnection 進行資料庫的連結2.close 進行資料庫的關閉3.executeUpdate 執行資料的增刪改4.executeQuery執行資料的查詢 History:   // 曆史修改記錄           <author>  <time>   <version >   <desc>               David    96/10/12     1.0     build this moudle   ***********************************************************/ public class JDBCUtil {    //連結地址,設定編碼可用且為utf-8    public static String URL="jdbc:mysql://192.168.80.131:3306/db1?useUnicode=true&characterEncoding=utf8";    //資料庫使用者名稱    public static String USER="root";    //資料庫密碼    public static String PWD="123456";        /*    *進行資料庫的連結    */    public static Connection getConnection(){                Connection con=null;        try {            //載入驅動            Class.forName("com.mysql.jdbc.Driver");            //建立連結            con=DriverManager.getConnection(URL, USER, PWD);        } catch (ClassNotFoundException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (SQLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        //返回串連        return con;    }        /*    *資料庫關閉    */    public static void Close(Connection con,PreparedStatement pstmt,ResultSet rs){        try {            //判斷是否被操作            if(rs!=null)                rs.close();            if(pstmt!=null)                pstmt.close();            if(con!=null)                con.close();        } catch (SQLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }        /*    *建立資料庫執行操作,返回受影響的行數    *@param String sql    *@param Object params    *@return int result    */    public static int executeUpdate(String sql,Object... params){        //建立連結        Connection con=getConnection();        PreparedStatement pstmt=null;        int result=0;        try {            //先行編譯sql語句,防止sql注入            pstmt=con.prepareStatement(sql);            //傳遞參數,如果參數存在            if(params!=null){                //進行迴圈傳參                for(int i=0;i<params.length;i++){                    pstmt.setObject(i+1, params[i]);                }            }            //執行sql語句,返回受影響行數            result=pstmt.executeUpdate();        } catch (SQLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }finally{            Close(con,pstmt,null);        }        return result;    }    /*    *進行資料的查詢,通過自建泛型RowMap,進行資料類型的定義    *@param String sql    *@param RowMap T    *@param Object params    *@return list<T>    */    public static <T> List<T> executeQuery(String sql,RowMap<T> rowMap,Object... params){        //建立泛型List        List<T> list=new ArrayList<>();        //建立連結        Connection con=getConnection();        PreparedStatement pstmt=null;        ResultSet rs=null;        try {            //綁定sql語句            pstmt=con.prepareStatement(sql);            //迴圈穿參            if(params!=null){                for(int i=0;i<params.length;i++){                    pstmt.setObject(i+1, params[i]);                }            }            //執行語句,用結果集接收            rs=pstmt.executeQuery();            while(rs.next()){                //利用自建泛型實現數組的添加                T t=rowMap.rowMapping(rs);                list.add(t);            }        } catch (SQLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }finally{            Close(con,pstmt,rs);        }        return list;    }        //使用樣本函數    public static int update(){        return executeUpdate("insert into student(name,age,sex) values(?,?,?)","姓名",12,"男");
  }
}

自訂自建泛型

import java.sql.ResultSet;//自建泛型,實現rowMappping返回T類型public interface RowMap<T> {    public T rowMapping(ResultSet rs);}

 

java-jdbc封裝串連資料庫工具

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.