• 数据库连接工具类


    package com.pk.xjgs.util;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.Properties;
    
    public class DBConn {
        
        private static String jdbcDriver = "";
        
        private static String jdbcUrl = "";
        
        private static String jdbcUser = "";
        
        private static String jdbcPasswd = "";
        
        static{
            InputStream is = null;
            try {
                is = DBConn.class.getClassLoader().getResourceAsStream("database.properties");
                Properties p = new Properties();//对应properties文件的类
                p.load(is);//加载流 目的就是将流中的数据进行解析
    //            System.out.println(p.getProperty("jdbcDriver"));
    //            System.out.println(p.getProperty("jdbcUrl"));
    //            System.out.println(p.getProperty("jdbcUser"));
    //            System.out.println(p.getProperty("jdbcPasswd"));
                jdbcDriver = p.getProperty("jdbcDriver");
                jdbcUrl = p.getProperty("jdbcUrl");
                jdbcUser = p.getProperty("jdbcUser");
                jdbcPasswd = p.getProperty("jdbcPasswd");
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if(is != null){
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        
        /*
         * 获取数据库指定的连接
         */
        public static Connection getConn(){
            Connection conn = null;
            try {
                Class.forName(jdbcDriver);
                conn = DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPasswd);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return conn;
        }
        
        /*
         * 关闭数据库打开的对象  包括connection  statement  resultSet
         */
        public static void closeAll(Connection conn,Statement st,ResultSet rs){
            if(rs != null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(st != null){
                try {
                    st.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        public static void closeAll(Connection conn,PreparedStatement ps,ResultSet rs){
            if(rs != null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(ps != null){
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        
        public static void closeConn(Connection conn){
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        
        public static void main(String[] args) {
            System.out.println(DBConn.getConn());
        }
    }
    View Code
  • 相关阅读:
    【杭电】[2071]无限的路
    【杭电】[1161]Eddy's mistakes
    【杭电】[1859]最小长方形
    【杭电】[2083]简易版之最短距离
    【CodeForces】[614A]Link/Cut Tree
    【杭电】[2076]夹角有多大
    【杭电】[2055]An easy problem
    【杭电】[2036]改革春风吹满地
    【杭电 】[2040]亲和数
    【杭电】[2044]一只小蜜蜂...
  • 原文地址:https://www.cnblogs.com/xxy94077776/p/3395554.html
Copyright © 2020-2023  润新知