根据properties配置文件加载DB-Util帮助类

Technorati 标记: util,properties
package cn.itcast.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

public class DbUtil {
    
    private static String className = null;
    private static String connUrl = null;
    private static String userName = null;
    private static String password = null;
    
    //类加载时初始化静态数据
    static{
        ResourceBundle resbe = ResourceBundle.getBundle("jdbc");
        className = resbe.getString("className");
        connUrl = resbe.getString("connUrl");
        userName = resbe.getString("userName");
        password = resbe.getString("password");
        initDriver();
    }
    
    /**
     * 获取数据库连接接口
     * @return
     */
    public static Connection getConnection(){
        
        try {
            return DriverManager.getConnection(connUrl, userName, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
        
    }
    
    /**
     * 初始化数据库驱动
     */
    public static void initDriver(){
        try {
            Class.forName(className);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 释放对象内存
     * @param rs ResultSet
     * @param st Statement
     * @param conn Connection
     */
    public static void release(ResultSet rs,Statement st,Connection conn){
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            rs = null;
        }
        if(st!=null){
            try {
                st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            st = null;
        }
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}

根据properties配置文件加载DB-Util帮助类

上一篇:oracle 12c 创建用户


下一篇:Microsoft-PetSop4.0(宠物商店)-数据库设计-Oracle