Tomcat database connection pool configuration, added to <content> in server. xml
<! ----------------------------------------------------------------------->
<Resource Name = "JDBC/smsgroup" Scope = "retriable" type = "javax. SQL. datasource"/>
<Resourceparams name = "JDBC/smsgroup">
<Parameter>
<Name> maxwait </Name>
<Value> 10000 </value>
</Parameter>
<Parameter>
<Name> maxactive </Name>
<Value> 100 </value>
</Parameter>
<Parameter>
<Name> password </Name>
<Value> SWT </value>
</Parameter>
<Parameter>
<Name> URL </Name>
<Value> JDBC: mysql: // localhost: 3306/smsgroup? Autoreconnect = true </value>
</Parameter>
<Parameter>
<Name> driverclassname </Name>
<Value> org. gjt. Mm. MySQL. Driver </value>
</Parameter>
<Parameter>
<Name> maxidle </Name>
<Value> 30 </value>
</Parameter>
<Parameter>
<Name> username </Name>
<Value> SWT </value>
</Parameter>
</Resourceparams>
<! ------------------------------------------------------->
Add in Web. xml
<Resource-ref>
<Description> dB connection </description>
<Res-ref-Name> JDBC/smsgroup </RES-ref-Name>
<Res-type> javax. SQL. datasource </RES-type>
<Res-auth> container </RES-auth>
</Resource-ref>
<! ---------------------------------------------------->
Connect to and close the database code
Public void initconnection (){
// Connect the connection pool to the database
Try {
Context CTX = new initialcontext ();
Datasource source = (datasource) CTX. Lookup ("Java: COMP/ENV/jdbc/smsgroup ");
Connection con = source. getconnection ();
Con = source. getconnection ();
Stmt = con. createstatement ();
System. Out. println ("database connection successful! ");
} Catch (exception e ){
E. printstacktrace ();
System. Out. println ("failed to connect to the database! ");
}
}
<! ---------------------------- Close --------------------------------->
Public void destroyconnection (){
// Close the database (connection pool)
Try {
If (RS! = NULL) Rs. Close ();
If (stmt! = NULL) stmt. Close ();
If (con! = NULL) con. Close ();
System. Out. println ("the database is closed successfully! ");
}
Catch (exception e ){
System. Out. println ("the database is busy. Please press F5 to refresh the page! ");
E. printstacktrace ();
}
}