歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

簡單的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模式創建,而沒有用到連接池。關閉方法用起來很爽,減少了代碼量,也提高了程序的可靠性和質量。
Copyright © Linux教程網 All Rights Reserved