首页>代码>Flash插件FusionCharts,可以做动态报表 包含各种图>/FusionCharts_Evaluation/FusionCharts_Evaluation/Code/JSP/SourceCode/com/fusioncharts/database/DBConnection.java
/*
 * Created on Oct 25, 2006
 *
 */
package com.fusioncharts.database;

import java.sql.Connection;
import java.sql.SQLException; 

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

/**
 * 
 * Contains methods to get a connection to the database.<br>
 * The database used here is MySQL<br>
 * This class contains code to connect to the database<br>
 * The DSN looked up for connection is "jdbc/FactoryDB". Please configure the same DSN in your server.<br>
 * Only one instance of this class should ideally be used throughout your application.<br>
 * For demo purpose, we have kept the default constructor available to all classes.<br>
 * Ideally you would override the default constructor and write a getInstance method which will<br>
 * return a single DBConnection instance always,thus making it a singleton class.<br>
 * 
 * @author InfoSoft Global (P) Ltd.
 */
public class DBConnection {
    
    
    /**
     * Returns a connection to a database as configured earlier.<br>
     * This method can be used by all the jsps to get a connection.<br>
     * The jsps do not have to worry about which db to connect to etc.<br>
     * This has been configured in this class by the InitServlet.<br>
     * @return Connection - a connection to the specific database based on the configuration
     */
    public Connection getConnection() {
	Connection oConn=null;

	    // Connection to the mySQL DB
	    oConn = getConnectionByDSN("jdbc/FactoryDB");

	return oConn;
    }
   
    
    /**
     * Opens the connection to the MySQL Database<br>
     * Using the DataSource Name specified in the config file of the server in which it is deployed.<br>
     * In real world applications,some sort of connection pooling mechanism <br>
     * would be used for getting the connection and proper care would be taken to<br>
     * close it when the work is done.<br>
     *  
     * @return Connection a connection to the MySQL database
     */
    private Connection getConnectionByDSN(String dataSourceName) {
	Connection oConn = null;

	try {
    	  	Context initContext = new InitialContext();
    	  	Context envContext  = (Context)initContext.lookup("java:/comp/env");
    	  	DataSource ds = (DataSource)envContext.lookup(dataSourceName);//"jdbc/FactoryDB"
    	  	oConn = ds.getConnection();
    
	} catch (SQLException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	} catch (NamingException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
	return oConn;
    }


}
最近下载更多
smallmicro  LV3 2021年1月24日
caozongan  LV19 2020年4月17日
jiqingxings  LV4 2020年3月10日
wanglong_wang  LV13 2019年8月1日
15239218175  LV7 2019年4月22日
lwf626888  LV16 2017年12月6日
dagf113225  LV68 2017年12月1日
chenzheng  LV1 2017年11月27日
wupujian  LV17 2017年11月10日
1324488732  LV27 2017年7月30日
最近浏览更多
fesfefe  LV13 2023年11月1日
yz2810790 2023年7月5日
暂无贡献等级
张顺利  LV6 2023年2月3日
无花空折枝  LV9 2021年6月28日
17558420274  LV16 2021年5月28日
长得丑活得久  LV3 2021年3月4日
smallmicro  LV3 2021年1月24日
daishu14 2020年12月10日
暂无贡献等级
清欢hello  LV3 2020年6月18日
hxx88781143  LV8 2020年5月19日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友