/*
* 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.