zoukankan      html  css  js  c++  java
  • 抽取JDBC工具类

    package com.wbytts.util;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import java.util.Properties;
    
    public class JDBCUtils {
        
        static String driverClass = null;
        static String url = null;
        static String name = null;
        static String password = null;
        
        static {        
            try {
                //建立一个属性配置对象
                Properties properties = new Properties();
    
                //使用类加载器,去获取src底下的文件,要自己创建jdbc.properties配置文件
                InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
                    //Thread.currentThread().getContextClassLoader().getResourceAsStream("jdbc.properties");
    
                //导入输入流
                properties.load(is);
                //读取属性
                driverClass = properties.getProperty("driverClass");
                url = properties.getProperty("url");
                name = properties.getProperty("name");
                password = properties.getProperty("password");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
        public static Connection getConnection() {    
            Connection conn = null;
            try {
                Class.forName(driverClass);
                conn = DriverManager.getConnection(url, name, password);
            } catch (Exception e) {
                e.printStackTrace();
            }
            
            return conn;
        }
        
        /**
         *  释放资源
         * @param conn
         * @param st
         * @param rs
    
         */
        public static void release(Connection conn, Statement st, ResultSet rs) {
            closeRs(rs);
            closeSt(st);
            closeConn(conn);
        }
        
        public static void release(Connection  conn, Statement st) {
            closeSt(st);
            closeConn(conn);
        }
        
        private static void closeRs(ResultSet rs) {
            try {
                if(rs != null) {
                    rs.close();
                }
                rs = null;
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                rs = null;
            }
        }
        
        private static void closeSt(Statement st) {
            try {
                if(st != null) {
                    st.close();
                }
                st = null;
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                st = null;
            }
        }
        
        private static void closeConn(Connection conn) {
            try {
                if(conn != null) {
                    conn.close();
                }
                conn = null;
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                conn = null;
            }
        }
    }
    
    
  • 相关阅读:
    HTTPs与HTTP的区别&HTTPs如何建立连接
    HTTP协议常见状态码和字段
    服务器负载均衡
    ARP协议工作原理
    C++智能指针
    C++类型转换
    Rust 只出现一次的数字 两种解法
    Rust 存在重复元素 两种解法
    Rust 旋转数组
    Rust 动态规划 买卖股票的最佳时机 II
  • 原文地址:https://www.cnblogs.com/wbyixx/p/12129064.html
Copyright © 2011-2022 走看看