A very useful Java class for database connection pool operations

Source: Internet
Author: User

/*
* Created on 2007-
*/
Package wangyq. datasource;

/**
* @ Author wangyaqiang
* @ Version 1.0
* @ Link http://niceboy.cnblogs.com
*/
Import java. SQL .*;
Import javax. SQL .*;
Import javax. Naming .*;

Public class mysqldatasource {

 
Public connection conn;
Public resultset RS;
Public Statement stmt;

Private Static datasource Ds;
/**
*
* 2007-1-12
* Wangyq
*
*/
Public mysqldatasource (){
Try
{
Context CTX = new initialcontext ();
If (CTX = NULL)
Throw new exception ("no context ");
 
DS = (datasource) CTX. Lookup ("Java: COMP/ENV/jdbc/MySQL ");
 
If (Ds! = NULL ){
Conn = Ds. getconnection ();

If (Conn! = NULL ){

}
}
} Catch (exception e ){
E. printstacktrace ();
}

}
 
Public datasource getdatasource ()
{
Try
{
Context initctx = new initialcontext ();
If (initctx = NULL)
Throw new exception ("no context ");
Context envctx = (context) initctx. Lookup ("Java: COMP/ENV ");
DS = (datasource) envctx. Lookup ("JDBC/MySQL ");

If (Ds! = NULL ){
Return Ds;
}
Return NULL;
} Catch (exception e ){
E. printstacktrace ();
Return NULL;
}
}
/**
*
* @ Return
*
*/
Public connection getconnection ()
{
Try
{
Return Ds. getconnection ();
}
Catch (exception E)
{
E. printstacktrace ();
Return NULL;
}
}
/**
*
* @ Param SQL
* @ Return
*/
Public resultset executequery (string SQL)
{
Rs = NULL;
Try
{
Stmt = conn. createstatement ();
Rs1_stmt.exe cutequery (SQL );
}
Catch (sqlexception ex)
{
System. Err. println ("mysqlperformance.exe cutequery ():" + ex. getmessage ());
}
Return Rs;
}
/**
*
* @ Param SQL
*
*/
Public void executeupdate (string SQL)
{
Stmt = NULL;
Rs = NULL;
Try
{
Stmt = conn. createstatement ();
Stmt.exe cuteupdate (SQL );
Stmt. Close ();
Conn. Close ();
}
Catch (sqlexception ex)
{
System. Err. println ("mysqlperformance.exe cutequery ():" + ex. getmessage ());
}
}
/**
*
* @ Return
*
*/
Public databasemetadata getdbmd ()
{
Conn = NULL;
Databasemetadata dbmd = NULL;
Try
{
Dbmd = conn. getmetadata ();
}
Catch (sqlexception ex)
{
System. Err. println ("mysqlperformance.exe cutequery ():" + ex. getmessage ());
}
Return dbmd;
}
/**
*
* @ Return
*
*/
Public Boolean closeconnection ()
{
Try
{
Conn. Close ();
Return true;
}
Catch (sqlexception E)
{
E. printstacktrace ();
Return false;
}
}
/**
*
* @ Return
*
*/
Public Boolean closestmt ()
{
Try
{
Stmt. Close ();
Return true;
}
Catch (sqlexception E)
{
E. printstacktrace ();
Return false;
}

}
}

You need to modify it according to your project.
Context envctx = (context) initctx. Lookup ("Java: COMP/ENV ");
DS = (datasource) envctx. Lookup ("JDBC/MySQL ");
You can change it to your own configuration path. Of course, you can also use the parameter to give the path parameter to this class, but I am very lazy and can use it.

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.