现在的位置: 首页 > 综合 > 正文

JNDI创建java.sql.Connection

2014年02月15日 ⁄ 综合 ⁄ 共 2045字 ⁄ 字号 评论关闭
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());
        }
    }
}

抱歉!评论已关闭.