天天看點

自己實作的JDBC工具類

最近做了個背景應用程式,剛開始用Spring+iBatis來做的,後來因為種種原因,不讓用Spring、iBatis以及一些開源的工具包。

于是用JDBC重寫了原來的Service實作,項目做完了。

這個JDBC是以前業餘時候寫的,主要針對沒有事物控制的應用,比如MySQL的一些應用。現在放出來大家評論評論不足,也好改進改進。

jdbc.properties

jdbc.url=jdbc:mysql://192.168.1.101:3306/testdb?autoReconnect=true&zeroDateTimeBehavior=convertToNull 

jdbc.username=root 

jdbc.password=leizhimin

DBToolkit.java

package lavasoft.common; 

import org.apache.commons.logging.Log; 

import org.apache.commons.logging.LogFactory; 

import java.io.IOException; 

import java.sql.*; 

import java.util.List; 

import java.util.Properties; 

/** 

* JDBC工具類 

* @author leizhimin 2009-11-24 9:28:03 

*/ 

public class DBToolkit { 

        private static final Log log = LogFactory.getLog(DBToolkit.class); 

        private static String url = null; 

        private static String username = null; 

        private static String password = null; 

        private static Properties props = new Properties(); 

        static { 

                try { 

                        props.load(DBToolkit.class.getResourceAsStream("/jdbc.properties")); 

                } catch (IOException e) { 

                        log.error("#ERROR# :系統加載sysconfig.properties配置檔案異常,請檢查!", e); 

                } 

                url = (props.getProperty("jdbc.url")); 

                username = (props.getProperty("jdbc.username")); 

                password = (props.getProperty("jdbc.password")); 

                //注冊驅動類 

                        Class.forName("com.mysql.jdbc.Driver"); 

                } catch (ClassNotFoundException e) { 

                        log.error("#ERROR# :加載資料庫驅動異常,請檢查!", e); 

        } 

        /** 

         * 建立一個資料庫連接配接 

         * 

         * @return 一個資料庫連接配接 

         */ 

        public static Connection getConnection() { 

                Connection conn = null; 

                //建立資料庫連接配接 

                        conn = DriverManager.getConnection(url, username, password); 

                } catch (SQLException e) { 

                        log.error("#ERROR# :建立資料庫連接配接發生異常,請檢查!", e); 

                return conn; 

         * 在一個資料庫連接配接上執行一個靜态SQL語句查詢 

         * @param conn            資料庫連接配接 

         * @param staticSql 靜态SQL語句字元串 

         * @return 傳回查詢結果集ResultSet對象 

        public static ResultSet executeQuery(Connection conn, String staticSql) { 

                ResultSet rs = null; 

                        //建立執行SQL的對象 

                        Statement stmt = conn.createStatement(); 

                        //執行SQL,并擷取傳回結果 

                        rs = stmt.executeQuery(staticSql); 

                        log.error("#ERROR# :執行SQL語句出錯,請檢查!\n" + staticSql, e); 

                return rs; 

         * 在一個資料庫連接配接上執行一個靜态SQL語句 

        public static void executeSQL(Connection conn, String staticSql) { 

                        stmt.execute(staticSql); 

         * 在一個資料庫連接配接上執行一批靜态SQL語句 

         * @param conn        資料庫連接配接 

         * @param sqlList 靜态SQL語句字元串集合 

        public static void executeBatchSQL(Connection conn, List<String> sqlList) { 

                        for (String sql : sqlList) { 

                                stmt.addBatch(sql); 

                        } 

                        stmt.executeBatch(); 

                        log.error("#ERROR# :執行批量SQL語句出錯,請檢查!", e); 

        public static void closeConnection(Connection conn) { 

                if (conn == null) return; 

                        if (!conn.isClosed()) { 

                                //關閉資料庫連接配接 

              &n