DBSource. java
Package com. aspboy. base. database;
/*
* Creation date:
*
* To change the template of the generated file in TODO, go
* Window-preferences-Java-code style-Code Template
*/
Import java. SQL. Connection;
Import java. SQL. DriverManager;
Import java. SQL. SQLException;
/**
Database contact class, which provides a method for retrieving database connections
*/
Public class DBSource
{
/**
Static Method, database connection
*/
Public static Connection getConnection () throws Exception
{
Connection conn = null;
Try
{
/*
// Start connection pool
Context = new InitialContext ();
InitialContext context = new InitialContext ();
Ds = (DataSource) context. lookup ("jdbc/OracleDS ");
Conn = ds. getConnection ();
// End connection pool
*/
/*
// This is the odbc connection method.
String driver = "sun. jdbc. odbc. JdbcOdbcDriver ";
String url = "jdbc: odbc: newjchtdb ";
String username = "sa ";
String password = "";
*/
// Set database connection parameters through the configuration file
String url = (string) configutil. getconfiginfo (). Get ("url ");
// System. Out. println ("url100 =" + URL );
String driver = (string) configutil. getconfiginfo (). Get ("driver ");
// System. Out. println ("driver =" + driver );
String username = (string) configutil. getconfiginfo (). Get ("user ");
// System. Out. println ("user =" + username );
String Password = (string) configutil. getconfiginfo (). Get ("password ");
// System. Out. println ("Password =" + password );
Class. forname (driver );
Conn = drivermanager. getconnection (URL, username, password );
}
Catch (sqlexception SE)
{
// Throw new servletexception (SE );
}
Catch (exception E)
{
E. printstacktrace ();
Conn = null;
}
Return conn;
}
Public static Connection getConnectionFromSession () throws Exception
{
Connection conn = null;
Try
{
/*
// Start connection pool
Context = new InitialContext ();
InitialContext context = new InitialContext ();
Ds = (DataSource) context. lookup ("jdbc/OracleDS ");
Conn = ds. getConnection ();
// End connection pool
*/
/*
// This is the odbc connection method.
String driver = "sun. jdbc. odbc. JdbcOdbcDriver ";
String url = "JDBC: ODBC: newjchtdb ";
String username = "sa ";
String Password = "";
*/
// Set database connection parameters through the configuration file
String url = (string) configutil. getconfiginfo (). Get ("url ");
// System. Out. println ("url100 =" + URL );
String driver = (string) configutil. getconfiginfo (). Get ("driver ");
// System. Out. println ("driver =" + driver );
String username = (String) ConfigUtil. getConfigInfo (). get ("user ");
// System. out. println ("user =" + username );
String password = (String) ConfigUtil. getConfigInfo (). get ("password ");
// System. out. println ("password =" + password );
Class. forName (driver );
Conn = DriverManager. getConnection (url, username, password );
}
Catch (SQLException se)
{
// Throw new ServletException (se );
}
Catch (Exception e)
{
E. printStackTrace ();
Conn = null;
}
Return conn;
}
Public static Connection getConnectionFromPool () throws Exception
{
Connection conn = null;
Try
{
Conn = DriverManager. getConnection ("proxool. myweb2008 ");
}
Catch (SQLException se)
{
// Throw new ServletException (se );
}
Catch (Exception e)
{
E. printStackTrace ();
Conn = null;
}
Return conn;
}
Public static void main (String [] args)
{
Try {
System. out. println ("conn =" + DBSource. getConnectionFromPool ());
} Catch (Exception e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
}