国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

Swing應(yīng)用開(kāi)發(fā)實(shí)戰(zhàn)系列之一:自定義JdbcTemplate

2019-11-14 14:51:02
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

 

筆者本人真正意義上接觸編程開(kāi)發(fā)是在2004年,最早用的就是VB,然后是Delphi等,后來(lái)轉(zhuǎn)到.Net,中間斷斷續(xù)續(xù)還用過(guò)PowerBuilder等,無(wú)一例外,所研發(fā)設(shè)計(jì)的項(xiàng)目或系統(tǒng)都是WinForm應(yīng)用程序,基于C/S模式的窗口應(yīng)用程序開(kāi)發(fā)那個(gè)時(shí)候還是正道,而Web開(kāi)發(fā)還沒(méi)有現(xiàn)在這么紅火,技術(shù)也沒(méi)有現(xiàn)在這么多姿多彩。

后來(lái),B/S技術(shù)日漸成熟起來(lái),jquery、node.js、flex、sPRing、structs、hibernate等技術(shù)框架相繼涌現(xiàn)出來(lái),進(jìn)行Web開(kāi)發(fā)的門(mén)檻降了下來(lái),開(kāi)發(fā)效率提高了許多,C/S開(kāi)發(fā)慢慢地受到了冷落。

不過(guò),在閑暇之余,筆者本人仍會(huì)時(shí)常設(shè)計(jì)一些窗口小程序,或者用WinForm程序開(kāi)發(fā)的方式測(cè)試Web開(kāi)發(fā)中遇到的算法問(wèn)題。尤其是有些時(shí)候,筆者僅僅是想測(cè)試一個(gè)類(lèi)或者接口方法,如果在Web工程中編碼測(cè)試的話(huà),你就要配置一大堆東西,最后還要啟動(dòng)個(gè)IIS,或者Tomcat,或者JBoss什么的,破費(fèi)周折;當(dāng)然了,你也可以脫離Web工程環(huán)境,寫(xiě)個(gè)類(lèi)似dos的小程序也行,不過(guò)那樣的話(huà)人機(jī)交互性實(shí)在太差勁。就好像下面這個(gè)窗口:

dos_dialog

就以上弊病而言,WinForm開(kāi)發(fā)算是有得天獨(dú)厚的優(yōu)勢(shì)了,考慮到跨平臺(tái)的問(wèn)題,再加上最近一年筆者都在持續(xù)地研究大數(shù)據(jù)方面的技術(shù),基本上都是在用java進(jìn)行技術(shù)研發(fā)和探索,所以很自然地,Swing就成了首選技術(shù)。

感覺(jué)Swing好像被冷落了好多年了,這也難怪,從當(dāng)初的JCreator、JBuilder到NetBean,以及現(xiàn)在的Eclipse等,C/S開(kāi)發(fā)好像都是非主流的,B/S才是主流,而且RAD特性也遠(yuǎn)遠(yuǎn)沒(méi)有微軟的VS好。好在有一點(diǎn)它是非常突出的,那就是跨平臺(tái),可以輕松部署在linux系統(tǒng)中。

經(jīng)過(guò)最近半年多試用Swing,筆者慢慢地積累了一些東西,斷斷續(xù)續(xù)的,偏偏面面的,各位不要太過(guò)于責(zé)怪,筆者會(huì)盡量將其歸類(lèi)整理,并給出相關(guān)的運(yùn)行實(shí)例程序代碼。

首先,在前面幾個(gè)章節(jié),我們會(huì)構(gòu)建個(gè)Java基礎(chǔ)功能包,介紹一些設(shè)計(jì)實(shí)現(xiàn)思路,封裝設(shè)計(jì)一些接口方法,作為我們進(jìn)行Swing應(yīng)用開(kāi)發(fā)的基礎(chǔ),有了這個(gè)基礎(chǔ)功能包,我們接下來(lái)的Swing開(kāi)發(fā)會(huì)有事半功倍的效果。

1、自定義JdbcTemplate

Hibernate中的JdbcTemplate組件功能已經(jīng)很強(qiáng)悍了,不過(guò)現(xiàn)在我們要自定義設(shè)計(jì)個(gè)輕量級(jí)別的JdbcTemplate組件,作為我們Swing開(kāi)發(fā)的基礎(chǔ)功能包的一部分。我們主要對(duì)常用的關(guān)系數(shù)據(jù)庫(kù)進(jìn)行接口封裝,包括OracleMySQL兩種數(shù)據(jù)庫(kù)。

1.1 數(shù)據(jù)庫(kù)類(lèi)型枚舉類(lèi)

/** * Description: 數(shù)據(jù)庫(kù)類(lèi)型。<br> * Copyright: Copyright (c) 2015<br> * Company: 河南電力科學(xué)研究院智能電網(wǎng)所<br> * @author shangbingbing 2015-01-01編寫(xiě) * @version 1.0 */public enum DBType {    ORACLE,    MYSQL}

1.2 數(shù)據(jù)庫(kù)JDBC操作類(lèi)

/** * Description: 關(guān)系數(shù)據(jù)庫(kù)數(shù)據(jù)操作接口模板。<br> * Copyright: Copyright (c) 2015<br> * Company: 河南電力科學(xué)研究院智能電網(wǎng)所<br> * @author shangbingbing 2015-01-01編寫(xiě) * @version 1.0 */public class JdbcTemplate {    private DBType dataBaseType = DBType.ORACLE;    private String userName = "";    private String passWord = "";    private String driver = "";    private String connectionUrl = "";    /**     * 獲取數(shù)據(jù)庫(kù)用戶(hù)名稱(chēng)     * @return     */    public String getUserName() {        return this.userName;    }    /**     * 設(shè)置數(shù)據(jù)庫(kù)用戶(hù)名稱(chēng)     * @param userName     */    public void setUserName(String userName) {        this.userName = userName;    }    /**     * 獲取數(shù)據(jù)庫(kù)用戶(hù)密碼     * @return     */    public String getPassword() {        return this.password;    }    /**     * 設(shè)置數(shù)據(jù)庫(kù)用戶(hù)密碼     * @param password     */    public void setPassword(String password) {        this.password = password;    }    /**     * 獲取數(shù)據(jù)庫(kù)驅(qū)動(dòng)程序     * @return     */    private String getDriver() {        if(this.dataBaseType == DBType.ORACLE) {            this.driver = "oracle.jdbc.driver.OracleDriver";        } else if (this.dataBaseType == DBType.MYSQL) {            this.driver = "com.mysql.jdbc.Driver";        }        return driver;    }    /**     * 獲取數(shù)據(jù)庫(kù)連接URL     * @return     */    public String getConnectionUrl() {        return this.connectionUrl;    }    /**     * 設(shè)置數(shù)據(jù)庫(kù)連接URL     * @param url     */    public void setConnectionUrl(String url) {        this.connectionUrl = url;    }    /**     * 默認(rèn)構(gòu)造函數(shù)。     * @param dataBaseType     */    public JdbcTemplate() { }    /**     * 構(gòu)造函數(shù),并確定數(shù)據(jù)庫(kù)類(lèi)型。     * @param dataBaseType     */    public JdbcTemplate(DBType dataBaseType) {        this.dataBaseType = dataBaseType;    }    /**     * 數(shù)據(jù)庫(kù)連接對(duì)象     */    private Connection conn = null;    /**     * 獲取數(shù)據(jù)庫(kù)連接對(duì)象     * @return     */    public Connection getConnection() {        try {            if(this.conn == null || this.conn.isClosed()) {                Class.forName(this.getDriver());                this.conn = DriverManager.getConnection(getConnectionUrl(),getUserName(),getPassword());            }        } catch (Exception e) {            e.printStackTrace();        }         return this.conn;    }    /**     * SQL語(yǔ)句對(duì)象     */    private Statement stmt = null;    /**     * 執(zhí)行增刪改SQL語(yǔ)句     * @param strSql     * @return     */    public boolean executeSql(String strSql) {        if(strSql == null || strSql.isEmpty()) {            return false;        }        try {            LogInfoUtil.printLog(strSql);            this.stmt = this.getConnection().createStatement();            this.stmt.execute(strSql);            //execute返回值出現(xiàn)問(wèn)題,即使SQL執(zhí)行成功也會(huì)返回false的。            return true;        } catch(Exception e) {            e.printStackTrace();            return false;        }    }        /**     * 批量執(zhí)行增刪該SQL語(yǔ)句     * @param strSqlList     */    public void executeSql(List<String> strSqlList) {        if(strSqlList == null || strSqlList.size() == 0) {            return;        }        try {            this.stmt = this.getConnection().createStatement();            //每500作為一個(gè)插入批量操作            int batchSize = 500;            int count = 0;            for(String strSql : strSqlList) {                LogInfoUtil.printLog(strSql);                this.stmt.addBatch(strSql);                if(++count % batchSize == 0) {                    this.stmt.executeBatch();                }            }            //插入剩余的數(shù)據(jù)            this.stmt.executeBatch();        } catch(Exception e) {            e.printStackTrace();        }    }    /**     * 獲取SQL檢索語(yǔ)句結(jié)果集中的列名稱(chēng)及類(lèi)型信息。     * @param strSql     * @return     */    public TreeMap<String,Integer> getColumnTypeList(String strSql) {        TreeMap<String,Integer> columnTypeList = new TreeMap<String,Integer>();        try {            LogInfoUtil.printLog(strSql);            this.stmt = this.getConnection().createStatement();            ResultSet rs = this.stmt.executeQuery(strSql);            //獲取數(shù)據(jù)集的列信息            ResultSetMetaData rsmd = rs.getMetaData();            int columnCount = rsmd.getColumnCount();            for(int i=1; i<=columnCount; i++) {                String columnName = rsmd.getColumnName(i);                columnTypeList.put(columnName,rsmd.getColumnType(i));            }            rs.close();            return columnTypeList;        } catch(Exception e) {            e.printStackTrace();            return columnTypeList;        }     }    /**     * 獲取SQL檢索語(yǔ)句結(jié)果集中的列名稱(chēng)及類(lèi)型信息。     * @param strSql     * @return     */    public TreeMap<String,String> getColumnTypeNameList(String strSql) {        TreeMap<String,String> columnTypeList = new TreeMap<String,String>();        try {            LogInfoUtil.printLog(strSql);            this.stmt = this.getConnection().createStatement();            ResultSet rs = this.stmt.executeQuery(strSql);            //獲取數(shù)據(jù)集的列信息            ResultSetMetaData rsmd = rs.getMetaData();            int columnCount = rsmd.getColumnCount();            for(int i=1; i<=columnCount; i++) {                String columnName = rsmd.getColumnName(i);                columnTypeList.put(columnName,rsmd.getColumnTypeName(i));            }            rs.close();            return columnTypeList;        } catch(Exception e) {            e.printStackTrace();            return columnTypeList;        }     }    /**     * 執(zhí)行SQL查詢(xún)語(yǔ)句,將結(jié)果以List<Properties>形式組裝返回.     * @param strSql     * @return     */    public List<Properties> executeQuery(String strSql) {        List<Properties> propertiesList = new ArrayList<Properties>();        if(strSql == null || strSql.isEmpty()) {            return propertiesList;        }                try {            LogInfoUtil.printLog(strSql);            this.stmt = this.getConnection().createStatement();            ResultSet rs = this.stmt.executeQuery(strSql);            //獲取數(shù)據(jù)集的列信息            ResultSetMetaData rsmd = rs.getMetaData();            HashMap<String,Integer> columnList = new HashMap<String,Integer>();            int columnCount = rsmd.getColumnCount();            for(int i=1; i<=columnCount; i++) {                String columnName = rsmd.getColumnName(i);                columnList.put(columnName,rsmd.getColumnType(i));            }                        while(rs.next()) {                Properties properties = new Properties();                for(String columnLabel : columnList.keySet()) {                    int columnType = columnList.get(columnLabel);                    Object columnValue = null;                                        switch (columnType) {                        case Types.VARCHAR :                            columnValue = rs.getString(columnLabel);                            break;                        case Types.NUMERIC:                            columnValue = rs.getBigDecimal(columnLabel);                            break;                        case Types.DATE:                            columnValue = rs.getDate(columnLabel).toString() + " " + rs.getTime(columnLabel).toString();                            break;                        case Types.NVARCHAR:                            columnValue = rs.getNString(columnLabel);                            break;                        case Types.DECIMAL:                            columnValue = rs.getBigDecimal(columnLabel);                            break;                        case Types.FLOAT:                            columnValue = rs.getFloat(columnLabel);                            break;                        case Types.DOUBLE:                            columnValue = rs.getDouble(columnLabel);                            break;                        case Types.BOOLEAN:                            columnValue = rs.getBoolean(columnLabel);                            break;                        case Types.INTEGER:                            columnValue = rs.getInt(columnLabel);                            break;                        case Types.BIGINT:                            columnValue = rs.getLong(columnLabel);                            break;                        case Types.TIME:                            columnValue = rs.getTime(columnLabel);                            break;                        case Types.BLOB:                            columnValue = rs.getBlob(columnLabel);                            break;                        case Types.CLOB:                            columnValue = rs.getClob(columnLabel);                            break;                        default:                            columnValue = rs.getObject(columnLabel);                            break;                    }                                        if(columnValue == null) {                        properties.put(columnLabel, "");                    } else {                        properties.put(columnLabel, columnValue);                    }                }                propertiesList.add(properties);            }            rs.close();            return propertiesList;        } catch(Exception e) {            e.printStackTrace();            return propertiesList;        }    }    /**     * 獲取指定表中的記錄總數(shù)     * @param tableName 指定表名稱(chēng)     * @return 表中的記錄總數(shù)     */    public int getTableRecordTotalCount(String tableName) {        int totalCount = 0;        String strSql = "select count(rowid) as RECORD_COUNT from " + tableName;        try {            LogInfoUtil.printLog(strSql);            this.stmt = this.getConnection().createStatement();            ResultSet rs = this.stmt.executeQuery(strSql);            while(rs.next()) {                totalCount = rs.getInt("RECORD_COUNT");            }            rs.close();        } catch (Exception e) {            e.printStackTrace();        }        return totalCount;    }/**     * 釋放連接資源     */    public void close() {        try {            if(this.stmt != null) {                this.stmt.close();                this.stmt = null;            }            if(this.conn != null) {                this.conn.close();                this.conn = null;            }        } catch (Exception e) {            e.printStackTrace();        }    }    /**     * 讀取指定的JDBC Properties文件,初始化數(shù)據(jù)連接信息。      * @param jdbcPropertiesFile     */    public void initJdbcTemplate(String jdbcPropertiesFile) {        HashMap<String,String> pps = PropertiesUtil.readProperties(jdbcPropertiesFile);        String url = pps.get("jdbc.url");        String userName = pps.get("jdbc.username");        String password = pps.get("jdbc.password");        this.connectionUrl = url;        this.userName = userName;        this.password = password;    }}

我們僅僅設(shè)計(jì)了幾個(gè)常用的接口方法,看官可以根據(jù)自己的實(shí)際需要進(jìn)行豐富和完善,譬如可以增加讀寫(xiě)B(tài)lob字段的功能等等。

1.3 應(yīng)用開(kāi)發(fā)實(shí)例

  • 操作Oracle數(shù)據(jù)庫(kù)
JdbcTemplate jdbcTemplate = new JdbcTemplate();或者JdbcTemplate jdbcTemplate = new JdbcTemplate(DBType.ORACLE);jdbcTemplate.setConnectionUrl(“jdbc:oracle:thin:@ ......”);jdbcTemplate.setUserName(“admin”);jdbcTemplate.setPassword(“admin”); String strSql = "SELECT * FROM SYSTEM_USER";List<Properties> list = jdbcTemplate.executeQuery(strSql);
  • 操作MySql數(shù)據(jù)庫(kù)
JdbcTemplate jdbcTemplate = new JdbcTemplate(DBType.MYSQL);jdbcTemplate.setConnectionUrl(“jdbc:mysql://10.231.45.34 ......”);jdbcTemplate.setUserName(“admin”);jdbcTemplate.setPassword(“admin”);String strSql = "SELECT * FROM SYSTEM_USER";List<Properties> list = jdbcTemplate.executeQuery(strSql);

 

【完】

作者:商兵兵

單位:河南省電力科學(xué)研究院智能電網(wǎng)所

QQ:52190634

主頁(yè):http://www.survivalescaperooms.com/shangbingbing

空間:http://shangbingbing.qzone.qq.com


發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 姜堰市| 昭苏县| 天祝| 台中市| 和林格尔县| 涞源县| 阜宁县| 二手房| 大埔县| 绥中县| 英超| 靖江市| 岳西县| 云阳县| 新闻| 方城县| 安新县| 潮州市| 高唐县| 长寿区| 长丰县| 台北县| 牙克石市| 特克斯县| 钟祥市| 于田县| 金川县| 陵川县| 福建省| 章丘市| 崇信县| 东乌珠穆沁旗| 呼和浩特市| 合川市| 台前县| 凤台县| 乃东县| 青岛市| 昆明市| 德兴市| 论坛|