Configuration environment: tomcat5.5, mysql4.1, mysql-connector-java-5.0.0-beta, commons-dbcp-1.2.1, jdk1.5 above the "Dongdong" can go to the Internet.
Then in D: \ tomcat5.5 \ webapps \ new directory dbtest folder, dbtest to create such directories: Web-INF, META-INF, create two folders and two files under the WEB-INF: classes, Lib, Web. XML, test. JSP, create context under the META-INF. XML (I am not sure why I want to create this file. If anyone knows this, please advise me)
Next, we will take the JDBC driverProgramMysql-connector-java-5.0.0-beta and commons-dbcp-1.2.1 decompress, respectively decompress the. Jar files are copied to D: \ tomcat5.5 \ common \ Lib, the above work can be done to configure the connection pool.
1. Configure D: \ Tomcat 5.5 \ conf \ Server. XML to add the following configuration information to server. xml:
Type = "javax. SQL. datasource"
Driverclassname = "com. MySQL. JDBC. Driver"
Password = "yang"
Maxidle = "2"
Maxwait = "5000"
Username = "root"
Url = "JDBC: mysql: // localhost: 3306/math"
Maxactive = "4"/>
Note: "name" indicates the name of the connection pool. "type" indicates the type of the data source. "driverclassname" indicates the class name of the driver. "url" indicates the database path. "math" indicates the database name.
2. Configure D: \ Tomcat 5.5 \ webapps \ dbtest \ WEB-INF \ WEB. XML to add information such
DB connection
Test
Javax. SQL. datasource
Container
Note: The res-ref-name must be the same as the connection pool name.
3. Configure D: \ Tomcat 5.5 \ webapps \ dbtest \ META-INF \ context. XML as follows
DEBUG = "5"
Reloadable = "true"
Crosscontext = "true">
Note: path is the path of the workspace. resourcelink name and global seem to have to be the same as the name of the previous connection pool. I am not quite sure about the global here. Sorry, hey !!
JavaCode
-
- Package app;
-
-
-
-
-
-
-
- Import java. SQL .*;
-
-
-
- Import javax. Naming .*;
-
-
-
- Import javax. SQL. datasource;
-
-
-
- /*
-
-
-
- Public class dbmanager
-
-
-
- {
-
-
- Public static synchronized connection getconnection () throws exception
-
-
-
- {
-
-
-
- Try
-
-
-
- {
-
-
-
- Context initctx = new javax. Naming. initialcontext ();
-
-
- Context envctx = (context) initctx. Lookup ("Java: COMP/ENV ");
-
-
-
- Datasource DS = (datasource) envctx. Lookup ("JDBC/test ");
-
-
-
- Return Ds. getconnection ();
-
-
-
- }
-
-
-
- Catch (exception ex)
-
-
- {
-
-
-
- Throw ex;
-
-
-
- }
-
-
-
- }
-
-
-
- }
-
-
-
- */
-
-
-
- Import javax. Naming. context;
-
-
-
- Import javax. Naming. initialcontext;
-
-
-
- Import javax. SQL. datasource;
-
-
- Public class dbmanager
-
-
-
- {
-
-
-
- Final Static private Boolean verbose = true; // print console Control
-
-
-
- // Static logger = logger. getlogger (dbmanager. Class. getname ());
-
-
-
- Private context initctx = NULL;
-
-
-
- Private context CTX = NULL;
-
-
-
- Private datasource DS = NULL;
-
-
-
- Private long timeout = 5000;
-
-
-
- Public dbmanager ()
-
-
-
- {
-
-
- Try
-
-
-
- {
-
-
-
- Initctx = new initialcontext ();
-
-
-
- // Init context, read config web. xml
-
-
-
- If (initctx = NULL)
-
-
-
- {
-
-
-
- Throw new exception ("Initial failed! ");
-
-
- }
-
-
-
- CTX = (context) initctx. Lookup ("Java: COMP/ENV ");
-
-
-
- // Find "JDBC/sqlserverdb" object this configruation in the server. xml of Tomcat
-
-
-
- If (CTX! = NULL)
-
-
-
- {
-
-
- DS = (datasource) CTX. Lookup ("JDBC/test ");
-
-
-
- }
-
-
-
- If (DS = NULL)
-
-
-
- {
-
-
-
- Throw new exception ("Look up datasource failed! ");
-
-
- }
-
-
-
- }
-
-
-
- Catch (exception E)
-
-
-
- {
-
-
-
-
-
-
-
- Log (E, "can't get the context! ");
-
-
-
-
-
-
-
- }
-
-
- }
-
-
-
- Public synchronized connection getconnection (){
-
-
-
- // Get connection and set to delay time
-
-
-
- Long starttime = new java. util. Date (). gettime ();
-
-
-
- Connection con = NULL;
-
-
-
- While (con = NULL ){
-
-
-
- Con = newconnection ();
-
-
-
- If (con! = NULL ){
-
-
- // Log ("create new connection! ");
-
-
-
- Break;
-
-
-
- }
-
-
-
- Try {
-
-
-
- Log ("connection timeout, reconnect, wait" + timeout + "Ms ");
-
-
-
- Wait (timeout );
-
-
-
- }
-
-
-
- Catch (interruptedexception e ){
-
-
- Log (E, "connection timeout! ");
-
-
-
- }
-
-
-
- If (New java. util. Date (). gettime ()-starttime)> = timeout ){
-
-
-
- Log ("connection timeout! ");
-
-
-
- Break;
-
-
-
- }
-
-
-
- }
-
-
-
- Return con;
-
-
- }
-
-
-
- Private connection newconnection (){
-
-
-
- Connection con = NULL;
-
-
-
- Try {
-
-
-
- Con = Ds. getconnection ();
-
-
-
- If (con = NULL ){
-
-
-
- Throw new exception ("create connection failed! ");
-
-
-
- }
-
-
-
- }
-
-
- Catch (exception e ){
-
-
-
- Log ("create connection failed! ");
-
-
-
- System. Out. println (E. getmessage ());
-
-
-
- }
-
-
-
- Return con;
-
-
-
- }
-
-
-
- Public synchronized void freeconnection (connection Conn,
-
-
- Statement stmt,
-
-
-
- Preparedstatement pstmt ){
-
-
-
- Try {
-
-
-
- // Close statement
-
-
-
- If (stmt! = NULL ){
-
-
-
- Stmt. Close ();
-
-
-
- Stmt = NULL;
-
-
- // Log ("Close statement ......");
-
-
-
- }
-
-
-
- // Close preparedstatement
-
-
-
- If (pstmt! = NULL ){
-
-
-
- Pstmt. Close ();
-
-
-
- Pstmt = NULL;
-
-
-
- // Log ("Close preparedstatement ......");
-
-
-
- }
-
-
-
- }
-
-
- Catch (exception e ){
-
-
-
- System. Out. println (E. getmessage ());
-
-
-
- }
-
-
-
- Try {
-
-
-
- // Close connection
-
-
-
- If (Conn! = NULL ){
-
-
-
- Conn. Close ();
-
-
-
- Conn = NULL;
-
-
-
- // Log ("close connection ......");
-
-
- }
-
-
-
- }
-
-
-
- Catch (sqlexception e ){
-
-
-
- Log (E, "An error occurred while releasing the resource! ");
-
-
-
- }
-
-
-
- }
-
-
-
- /************************************
-
-
-
- * Write log file.
-
-
-
- * @ Param s string
-
-
-
- ************************************/
-
-
- Private void log (string S)
-
-
-
- {
-
-
-
- If (verbose)
-
-
-
- {
-
-
-
- System. Out. println (New java. util. Date () + ":" + S );
-
-
-
- // Logger.info (New java. util. Date () + S );
-
-
-
- }
-
-
- }
-
-
-
- Private void log (throwable E, string MSG)
-
-
-
- {
-
-
-
- System. Out. println (New java. util. Date () + ":" + MSG );
-
-
-
- }
-
-
-
- }
-
-
-
-
-
Triman.rar
Description:
Eclipse + MySQL + Tomcat configuration JNDI implementation example
Download
File Name:
Triman.rar
File Size:
1007 KB
Http://uuplace.javaeye.com/blog/88878