1. Place the corresponding JDBC package in the common/lib directory of the tomcat installation directory.
Ii. tomcat configuration file content (Taking ORACLE and thin connection as an example)
<? XML version = '1. 0' encoding = 'utf-8'?>
<Context docbase = "E:/workspace/toshba" Path = "/toshba" reloadable = "true" workdir = "E: \ workspace \ toshba \ work">
<Resource Name = "JDBC/oracledb" auth = "Container" type = "javax. SQL. datasource"/>
<Resourceparams name = "JDBC/oracledb">
<Parameter>
<Name> factory </Name>
<Value> org. Apache. commons. DBCP. basicperformancefactory </value>
</Parameter>
<Parameter>
<Name> driverclassname </Name>
<Value> oracle. JDBC. Driver. oracledriver </value>
</Parameter>
<Parameter>
<Name> URL </Name>
<Value> JDBC: oracle: thin: @ UFJ: 1521: drch1001 </value>
</Parameter>
<Parameter>
<Name> username </Name>
<Value> Scott </value>
</Parameter>
<Parameter>
<Name> password </Name>
<Value> tiger </value>
</Parameter>
<Parameter>
<Name> maxactive </Name>
<Value> 20 </value>
</Parameter>
<Parameter>
<Name> maxidle </Name>
<Value> 10 </value>
</Parameter>
<Parameter>
<Name> maxwait </Name>
<Value>-1 </value>
</Parameter>
</Resourceparams>
</Context>
Iii. Web. XML file content
<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype web-app public "-// Sun Microsystems, Inc. // DTD web application 2.3 //" http://java.sun.com/dtd/web-app_2_3.dtd ">
<Web-app>
<Resource-ref>
<Description> Oracle datasource example </description>
<Res-ref-Name> JDBC/oracledb </RES-ref-Name>
<Res-type> javax. SQL. datasource </RES-type>
<Res-auth> container </RES-auth>
</Resource-ref>
</Web-app>
4. JavaCodeWrite
Package com. CSK. DM;
Import java. util. collections;
Import java. util. hashmap;
Import java. util. Map;
Import javax. Naming. context;
Import javax. Naming. initialcontext;
Import javax. Naming. namingexception;
Import javax. SQL. datasource;
Public class servicelocator {
Private initialcontext Ic = NULL;
Private map cache;
Private Static servicelocator me = NULL;
Static {
Try {
Me = new servicelocator ();
} Catch (servicelocatorexception SE ){
System. Err. println (SE );
Se. printstacktrace (system. Err );
}
}
/**
*
*/
Public servicelocator () throws servicelocatorexception {
Try {
Ic = new initialcontext ();
Cache = collections. synchronizedmap (New hashmap ());
} Catch (namingexception ne ){
Throw new servicelocatorexception (NE );
} Catch (exception e ){
Throw new servicelocatorexception (E );
}
}
/**
* @ Return
*/
Public static servicelocator getinstance (){
Return me;
}
/**
* @ Param datasourcename
* @ Return
* @ Throws servicelocatorexception
*/
Public datasource getdatasource (string datasourcename) throws servicelocatorexception {
Datasource = NULL;
Try {
If (Cache. containskey (CENAME )){
Datasource = (datasource) cache. Get (datasourcename );
} Else {
Context CTX = (context) IC. Lookup ("Java: COMP/ENV ");
Datasource = (datasource) CTX. Lookup (datasourcename );
Cache. Put (datasourcename, datasource );
}
} Catch (namingexception ne ){
Throw new servicelocatorexception (NE );
} Catch (exception e ){
Throw new servicelocatorexception (E );
}
Return datasource;
}
}