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

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

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletContext;
import javax.sql.DataSource;
import com.infosoftglobal.fusioncharts.Constants;

/**
 * 
 * Contains methods to get a connection to the database.<br>
 * The database could be MS Access or MySQL<br>
 * This class contains database specific code to connect to the database Access or MySQL<br>
 * Only one instance of this class should ideally be used throughtout 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>
 * In the current application,<code>InitServlet</code> instantiates this class and sets all the <br>
 * variables like dbName,accessDBPath & mySQLDSN and puts that instance in the application context<br>
 * so that it is available to all the jsps.<br>
 * 
 * @deprecated Please use classes from com.fusioncharts.database package
 * 
 * @author InfoSoft Global (P) Ltd.
 * 
 */
public class DBConnection {
    
    /**
     * Name of the database to be used
     */
    private String dbName="";
    /**
     * Path to the access DB
     */
    private String accessDBPath="";
    /**
     * DSN for the MySql DB
     */
    private String mySQLDSN="";
    
    /**
     * 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;
	if(this.dbName.equals(Constants.ACCESSDB)){
	    //Connection to the Access DB needs a path 
	   oConn = getConnection(this.accessDBPath);
	}
	else if(this.dbName.equals(Constants.MYSQLDB)){
	    // Connection to the mySQL DB
	    oConn = getConnectionByDSN(this.mySQLDSN);
	}
	return oConn;
    }
   
    /**
     * Gets a connection to the specific database as given in configuration file.<br>
     * First it checks connection to which database is required<br>
     * then it calls the appropriate getConnection method<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.
     * 
     * @param context ServletContext of the server requesting connection
     * @return Connection - a connection to the database given in configuration file of the server.
     * 
     */
    public Connection getConnection(ServletContext context) {
	Connection oConn=null;
	String dbName=context.getInitParameter("dbName");
	this.dbName=dbName;
	String pathToDB=context.getInitParameter("AccessDBPath");
	this.accessDBPath=pathToDB;
	String dsName=context.getInitParameter("dataSourceName");
	this.mySQLDSN=dsName;
	if(dbName.equals(Constants.ACCESSDB)){
	    //Connection to the Access DB needs a path 
	   oConn = getConnection(pathToDB);
	}
	else if(dbName.equals(Constants.MYSQLDB)){
	    // Connection to the mySQL DB
	    oConn = getConnectionByDSN(dsName);
	}
	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/FusionChartsDB"
    	  	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;
    }
    /**
     * Opens the connection to the Access Database<br>
     * The complete path to the mdb comes from configuration file web.xml.<br>
     * Here we are just printing the stack trace in case of any exception.<br>
     * In real world applications,some kind of exception-handling needs to be done.<br>
     * Further,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. 
     * @param pathToDB - the path to the mdb file
     * @return Connection - a connection to the Access database
     */
    private Connection getConnection(String pathToDB) {
	Connection oConn = null;

	try {
	    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

	    String absolutePath = pathToDB;
	   /* add connection mode here if required */
	    String connString = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ="
		    + absolutePath;

	    oConn = DriverManager.getConnection(connString, "", "");
	} catch (ClassNotFoundException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	} catch (SQLException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
	return oConn;
    }
    
    /**
     * Gets the path to the Access DB
     * @return String - path to the Access DB
     */
    public String getAccessDBPath() {
        return accessDBPath;
    }
    /**
     * Sets the path to the AccessDB
     * @param accessDBPath - path to the Access DB
     */
    public void setAccessDBPath(String accessDBPath) {
        this.accessDBPath = accessDBPath;
    }
    /**
     * Gets the Database Name for the current instance
     * @return String - the Database name
     */
    public String getDbName() {
        return dbName;
    }
    /**
     * Sets the Database Name for the current instance
     * @param dbName
     */
    public void setDbName(String dbName) {
        this.dbName = dbName;
    }
    /**
     * Gets the DSN for MySQLDB
     * @return String - the DSN for MySQLDB
     */
    public String getMySQLDSN() {
        return mySQLDSN;
    }
    /**
     * Sets the DSN for MySQLDB
     * @param mySQLDSN
     */
    public void setMySQLDSN(String mySQLDSN) {
        this.mySQLDSN = mySQLDSN;
    }

}
最近下载更多
smallmicro LV31月24日
星星星星星星
caozongan LV162020年4月17日
太阳
jiqingxings LV42020年3月10日
月亮
wanglong_wang LV122019年8月1日
月亮月亮月亮
15239218175 LV72019年4月22日
月亮星星星星星星
lwf626888 LV162017年12月6日
太阳
dagf113225 LV682017年12月1日
皇冠月亮
chenzheng LV12017年11月27日
星星
wupujian LV172017年11月10日
太阳星星
1324488732 LV272017年7月30日
太阳月亮月亮星星星星星星
最近浏览更多
无花空折枝 LV96月28日
月亮月亮星星
17558420274 LV155月28日
月亮月亮月亮星星星星星星
长得丑活得久 LV33月4日
星星星星星星
smallmicro LV31月24日
星星星星星星
daishu142020年12月10日
暂无贡献等级
清欢hello LV32020年6月18日
星星星星星星
hxx88781143 LV82020年5月19日
月亮月亮
cmb126 LV12020年5月9日
星星
caozongan LV162020年4月17日
太阳
wit1232020年4月4日
暂无贡献等级
顶部客服微信二维码底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友