码迷,mamicode.com
首页 > 数据库 > 详细

java mysql数据库链接与资源关闭

时间:2018-08-13 15:55:51      阅读:269      评论:0      收藏:0      [点我收藏+]

标签:ace   base   mys   throws   stat   color   throw   ssl   ons   

/**
 * Created by Clear on 2018/8/11.
 * here provide the kind of connections from mysql database,and close the resources of the mysql
 * there are
 *      load driver
 *      use properties file
 *      use xml file
 *      use tomcat
 *      and... so on
 *
 *
 *
 */
public class MysqlUtil {
    /**
     * 链接数据库
     */
    /**
     * 方法一:
     * 加载驱动
      */
    public static Connection getConnectionOne(String database,String username,String password){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/"+database,username,
                    password);
            return connection;
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 方法二:
     * 利用properties文件
     * ::::: 在Web 编程时 文件难以定位
      */
    public static Connection getConnectionTwo() {
        /**
         * 建立文件
         */
        Properties pro = new Properties();

        InputStream in = MysqlUtil.class.getClassLoader().getResourceAsStream("mysqllog.properties");
        try {
            pro.load(in);
            Class.forName(pro.getProperty("driver"));
            String username = pro.getProperty("user");
            String password = pro.getProperty("password");
            String database = pro.getProperty("database");
            String url = pro.getProperty("url");

            Connection connection = DriverManager.getConnection(url+database,username,password);
            return connection;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
    // Connection ,Statement, ResultSet 这几个资源的关闭是有顺序的
    public static void close (Object...objects) throws MysqlCloseException {
        Map<String,Object> map = new HashMap();
        for(Object o : objects){
            if(o instanceof ResultSet){
                map.put("ResultSet",o);
            }else if(o instanceof Connection){
                map.put("Connection",o);
            }else if(o instanceof Statement){
                map.put("Statement",o);
            }else if(o instanceof PreparedStatement){
                map.put("PreparedStatement",o);
            }else{
                throw new MysqlCloseException("关闭异常,不能处理");
            }
        }
        Object obj = map.get("ResultSet");
        if(obj!=null){
            ResultSet r = (ResultSet)obj;
            try {
                r.close();
                map.remove("ResultSet");
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        obj = map.get("PreparedStatement");
        if(obj!=null){
            PreparedStatement p = (PreparedStatement)obj;
            try {
                p.close();
                map.remove("PreparedStatement");
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        obj = map.get("Statement");
        if(obj!=null){
            Statement s = (Statement)obj;
            try {
                s.close();
                map.remove("Statement");
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        obj = map.get("Connection");
        if(obj!=null){
            Connection c = (Connection)obj;
            try{
                c.close();
                map.remove("Connection");
            }catch(SQLException e){
                e.printStackTrace();
            }
        }
    }
}

 

java mysql数据库链接与资源关闭

标签:ace   base   mys   throws   stat   color   throw   ssl   ons   

原文地址:https://www.cnblogs.com/lightandtruth/p/9461743.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!