简单的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);
                }
        }
}

 

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wyfwwf.html