簡單的Java數據庫鏈接和關閉工具類

簡單的Java數據庫鏈接和關閉工具類
 
寫JDBC應用的人經常爲關閉資源而頭痛不已,這些代碼枯燥無味,如何才能用簡單的代碼進行關閉呢,下面我寫了一個方法,能夠解除你的痛苦:
 
         /**
         * 關閉全部可關閉資源
         *
         * @param objs 可關閉的資源對象有Connection、Statement、ResultSet,別的類型資源自動忽略
         */

         public static void closeAll(Object... objs) {
                 for (Object obj : objs) {
                         if (obj instanceof Connection) close((Connection) obj);
                         if (obj instanceof Statement) close((Statement) obj);
                         if (obj instanceof ResultSet) close((ResultSet) obj);
                }
        }
 
這個方法,帶了「...」參數,這個其實是Java5中的可變參數方法。能夠不論順序,不論個數,調用時候直接關閉想要關閉的資源對象就ok了。例如:
 
catch (SQLException e) {
                        e.printStackTrace();
                } finally {
                        DBTools.closeAll(stmt, pstmt1, pstmt2, conn);
                }
 
下面給出這個類完整的寫法:
package com.lavasoft.ibatistools.common;

import com.lavasoft.ibatistools.bean.Table;
import com.lavasoft.ibatistools.metadata.DataSourceMetaData;
import com.lavasoft.ibatistools.metadata.MySQLDataSourceMetaData;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.List;
import java.util.Properties;

/**
* 簡單的Java數據庫鏈接和關閉工具類
*
* @author leizhimin 11-12-20 下午4:32
*/

public class DBTools {
         private static String driverClassName, url, user, password;

         static {
                init();
        }

         private static void init() {
                InputStream in = DBTools. class.getResourceAsStream( "/com/lavasoft/ibatistools/jdbc.properties");
                Properties preps = new Properties();
                 try {
                        preps.load(in);
                        driverClassName = preps.getProperty( "jdbc.driver");
                        url = preps.getProperty( "jdbc.url");
                        user = preps.getProperty( "jdbc.username");
                        password = preps.getProperty( "jdbc.password");
                } catch (IOException e) {
                        e.printStackTrace();
                }
        }

         /**
         * 建立一個JDBC鏈接
         *
         * @return 一個JDBC鏈接
         */

         public static Connection makeConnection() {
                Connection conn = null;
                 try {
                        Class.forName(driverClassName);
                        conn = DriverManager.getConnection(url, user, password);
                } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                } catch (SQLException e) {
                        e.printStackTrace();
                }
                 return conn;
        }

         public static void close(Connection conn) {
                 if (conn != null)
                         try {
                                conn.close();
                        } catch (SQLException e) {
                                e.printStackTrace();
                        }
        }

         public static void close(ResultSet rs) {
                 if (rs != null)
                         try {
                                rs.close();
                        } catch (SQLException e) {
                                e.printStackTrace();
                        }
        }

         public static void close(Statement stmt) {
                 if (stmt != null)
                         try {
                                stmt.close();
                        } catch (SQLException e) {
                                e.printStackTrace();
                        }
        }

         /**
         * 關閉全部可關閉資源
         *
         * @param objs 可關閉的資源對象有Connection、Statement、ResultSet,別的類型資源自動忽略
         */

         public static void closeAll(Object... objs) {
                 for (Object obj : objs) {
                         if (obj instanceof Connection) close((Connection) obj);
                         if (obj instanceof Statement) close((Statement) obj);
                         if (obj instanceof ResultSet) close((ResultSet) obj);
                }
        }

         public static void main(String[] args) {
                DataSourceMetaData dbmd = MySQLDataSourceMetaData.instatnce();
                List<Table> tableList = dbmd.getAllTableMetaData(DBTools.makeConnection());
                 for (Table table : tableList) {
                        System.out.println(table);
                }
        }
}
 
由於是在寫工具,鏈接用到的次數不多,因此這裏採用jdbc模式建立,而沒有用到鏈接池。關閉方法用起來很爽,減小了代碼量,也提升了程序的可靠性和質量。
相關文章
相關標籤/搜索