Database Connection dbsource. Java class

Source: Internet
Author: User

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 ();
}

}

}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.