六月婷婷综合激情-六月婷婷综合-六月婷婷在线观看-六月婷婷在线-亚洲黄色在线网站-亚洲黄色在线观看网站

明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

數據庫jdbc封裝

[摘要]經過幾天的努力終于搞好了,這個類可以不用管數據庫字段,不用寫dao類,直接map添加,添加刪除和修改,jdbc鏈接,分享給大家,用的話記得加上連接池,尊重原創,轉載請注明package jdbc;i...


經過幾天的努力終于搞好了,這個類可以不用管數據庫字段,不用寫dao類,直接map添加,添加刪除和修改,jdbc鏈接,分享給大家,用的話記得加上連接池,尊重原創,轉載請注明

package jdbc;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;/**
 * 操作數據庫工具類
 *
 *
 */public class domain {

    /**
     * 連接數據
     *
     * @return conn
     */
    public static Connection getConnection(String url, String username, String password) {
        Connection conn = null;        
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/" + url, username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }        return conn;
    }    /**
     * 關閉連接對象
     *
     * @param conn
     *            連接對象
     * @param pstmt
     *            預編譯對象
     * @param rs
     *            結果集
     */
    public static void closeAll(Connection conn, PreparedStatement pstmt, ResultSet rs) {        
    try {            
    if (rs != null) {
                rs.close();
            }            
            if (pstmt != null) {
                pstmt.close();
            }            
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }    /**
     * 增刪改操作
     *
     * @param sql
     *            SQL命令
     * @param param
     *            參數
     * @return
     */
    public static int executUpdate(Connection conn, String sql, Object[] param) {        
    int result = 0;
        PreparedStatement pstmt = null;        
        try {
            System.out.println(sql);
            pstmt = conn.prepareStatement(sql);            
            if (param != null) {                
            for (int i = 0; i < param.length; i++) {
                    pstmt.setObject(i + 1, param[i]);
                }
            }
            result = pstmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll(conn, pstmt, null);
        }        return result;
    }    /**
     * 查詢
     *
     * @return int
     * @date 2015-7-25 上午11:10:06
     */
    public static ResultSet executQuery(Connection conn, String sql, String[] param) {
        PreparedStatement pstmt = null;
        ResultSet result = null;        
        try {
            pstmt = conn.prepareStatement(sql);            
            if (param != null) {                
            for (int i = 0; i < param.length; i++) {
                    pstmt.setString(i + 1, param[i]);
                }
            }
            result = pstmt.executeQuery();
        } catch (Exception e) {
            e.printStackTrace();
        }        return result;
    }    /**
     * 簡單增刪改SQL語句執行,復雜sql執行executUpdate方法
     * 字符串類型需要加單引號
     * @param url
     *            數據庫
     * @param username
     *            數據庫用戶名
     * @param password
     *            數據庫密碼
     * @param map
     *            參數列,值
     * @param tableName
     *            表名
     * @param typeSql
     *            SQL類型 insert,dell,update
     * @param oldLine
     *            刪除時為要刪除的列名,更新的時候為where前的條件列名(僅當且使用dell和update時填寫,insert時不作用)
     * @param newLine
     *            更新時where候的條件列名(僅當且使用update時填寫,dell,insert時不作用)
     * @param oldCondition
     *            刪除時為要刪除的條件,更新的時候為where前的條件(僅當且使用dell和update時填寫,insert時不作用)
     * @param newCondition
     *            更新的時候為where后的條件(僅當且使用update時填寫,dell,insert時不作用)
     */

    public static void sql(String url, String username, String password, Map<String, Object> map, String tableName,
            String typeSql, String oldLine, String oldCondition, String newLine, String newCondition) {
        String sql = "";
        Connection conn = getConnection(url, username, password);
        Object[] valueArray = null;        if (typeSql.equals("insert")) {
            List<Object> key = new ArrayList<Object>();
            List<Object> value = new ArrayList<Object>();
            StringBuffer sb = new StringBuffer();
            StringBuffer wen = new StringBuffer();            for (Object string : map.keySet()) {
                key.add(string);
                value.add(map.get(string));
            }

            Object[] keyArray = key.toArray();
            valueArray = value.toArray();            for (int i = 0; i < keyArray.length; i++) {
                sb.append(keyArray[i] + ",");
                wen.append("?,");
            }
            String string = sb.toString();
            string = string.substring(0, string.length() - 1);
            String wenStr = wen.toString();
            wenStr = wenStr.substring(0, wenStr.length() - 1);

            sql = "INSERT INTO " + tableName + "(" + string + ") VALUES (" + wenStr + ")";
        } else if (typeSql.equals("dell")) {
            sql = "DELETE FROM " + tableName + " WHERE " + oldLine + " = " + oldCondition;
        } else if (typeSql.equals("update")) {
            sql = "UPDATE " + tableName + " SET " + oldLine + "= " + oldCondition + " WHERE " + newLine + " = "
                    + newCondition;
        }        int executUpdate = executUpdate(conn, sql + ";", valueArray);
        System.out.println(executUpdate);
    }//測試
    public static void main(String[] args) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("appid", "lisi");         for (int i = 0; i < 100; i++) {
         System.out.println(i);
         map.put("id",i);         // 增加
          sql("test", "root", "", map, "user", "insert", "", "", "", "");          // 刪除
          sql("test", "root", "", map, "user", "dell", "id", i+"","", "");          // 修改//        
          sql("test", "root", "", map, "user", "update", "appid", "'zhang'", "id", "0");

         }        // map.put("password", "123");

    }
}

以上就是數據庫jdbc封裝的詳細內容,更多請關注php中文網其它相關文章!


學習教程快速掌握從入門到精通的SQL知識。




主站蜘蛛池模板: 亚洲免费高清 | 日本韩国三级在线观看 | 综合色视频 | 日韩电源 | 日本视频一区在线观看免费 | 在线免费h视频 | 一二三四高清在线手机视频 | 亚洲影视一区 | 亚洲系列中文字幕一区二区 | 婷婷色香五月激情综合2020 | 色噜噜狠狠狠色综合久 | 青青草原伊人网 | 亚洲免费三级 | 天天弄天天干 | 日韩精品成人a在线观看 | 日本免费黄色网址 | 日本国产成人精品视频 | 思思99思思久久精品 | 日本一区二区三区高清在线观看 | 中文字幕高清在线天堂网 | 日韩中文字幕在线不卡 | 四虎tv | 亚洲欧美风情 | 天天拍天天干天天操 | 一级十八以 下岁女子毛片 一级人做人爰a全过程免费视频 | 婷婷丁香九月 | 日韩精品一区二区在线观看 | 日韩一级特黄毛片在线看 | 亚洲hh | 亚洲高清视频在线 | 四虎精品成人a在线观看 | 四虎免费最新在线永久 | 日本欧美久久久久免费播放网 | 欧美在线看欧美高清视频免费 | 天堂网在线最新版官网 | 四虎在线视频免费观看 | 天天色天天草 | 日本噜噜影院 | 午夜视频在线观看免费高清 | 速度与激情九 | 欧洲成品大片在线播放 |