JNDI创建java.sql.Connection

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

import org.apache.log4j.Logger;
import org.springframework.core.io.support.PropertiesLoaderUtils;


/**
 * 
 * @author lyh
 * @version
 * @see DbManager
 * @since
 */
public class DbManager
{
    /**
     * 日志
     */
    private static final Logger LOGGER = Logger.getLogger(DbManager.class);

    /**
     * 命名上下文
     */
    private static Context initctx = null;
    
    /**
     * 资源文件
     */
    private static Properties prop = null;

    /**
     * 数据源
     */
    @SuppressWarnings("unused")
    private static DataSource bds = null;
    
    /**
     * jndi名称
     */
    private static String jndiName = "";

    /**
     * 驱动
     */
    private static String driver = "";

    /**
     * url
     */
    private static String url = "";

    /**
     * db用户名
     */
    private static String username = "";

    /**
     * db密码
     */
    private static String password = "";
    
    static
    {
        try
        {
            prop = PropertiesLoaderUtils.loadAllProperties("jdbc.properties");
        }
        catch (IOException e1)
        {
            LOGGER.error(e1.getMessage());
        }
        
        // load jdbc.properties
        driver = (String)prop.get("driver");
        url =  (String)prop.get("url");
        username = (String)prop.get("username");
        password = (String)prop.get("password");
        
        // jndiName e.g. java:comp/env/jdbc/rhy
        jndiName = (String)prop.get("jndiName");
        
        // for jndi
        // init();
        
        try
        {
            Class.forName(driver);
        }
        catch (ClassNotFoundException e)
        {
            LOGGER.error(e.getMessage());
        }
    }

    /**
     * 
     * Description: 获得DB连接<br>
     * @return 
     * @see
     */
    public static Connection getDBConnection()
    {
        // for jndi
        /*Connection con = null;
        try
        {
            con = bds.getConnection();
        }
        catch (SQLException e)
        {
            LOGGER.error(e.getMessage());
        }
        return con;
        */
        
        Connection con = null;
        try
        {
            con = (Connection) DriverManager.getConnection(url, username, password);
        }
        catch (SQLException e)
        {
            LOGGER.error(e.getMessage());
        }
        return con;
    }

    /**
     * 
     * Description:关闭DB连接 <br>
     * @param con 
     * @see
     */
    public static void closeDBConnection(Connection con)
    {
        try
        {
            if (con != null && !con.isClosed())
            {
                con.close();
            }
        }
        catch (Exception e)
        {
            LOGGER.error(e.getMessage());
        }
    }

    /**
     * 
     * Description:jndi init method <br>
     * @see
     */
    @SuppressWarnings("unused")
    private static void init()
    {
        try
        {
            initctx = new InitialContext();
            bds = (DataSource) initctx.lookup(jndiName);
        }
        catch (Exception e)
        {
            LOGGER.error(e.getMessage());
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值