Configure JNDI in eclipse + MySQL + Tomcat

Source: Internet
Author: User

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

  1. Package app;
  2. Import java. SQL .*;
  3. Import javax. Naming .*;
  4. Import javax. SQL. datasource;
  5. /*
  6. Public class dbmanager
  7. {
  8. Public static synchronized connection getconnection () throws exception
  9. {
  10. Try
  11. {
  12. Context initctx = new javax. Naming. initialcontext ();
  13. Context envctx = (context) initctx. Lookup ("Java: COMP/ENV ");
  14. Datasource DS = (datasource) envctx. Lookup ("JDBC/test ");
  15. Return Ds. getconnection ();
  16. }
  17. Catch (exception ex)
  18. {
  19. Throw ex;
  20. }
  21. }
  22. }
  23. */
  24. Import javax. Naming. context;
  25. Import javax. Naming. initialcontext;
  26. Import javax. SQL. datasource;
  27. Public class dbmanager
  28. {
  29. Final Static private Boolean verbose = true; // print console Control
  30. // Static logger = logger. getlogger (dbmanager. Class. getname ());
  31. Private context initctx = NULL;
  32. Private context CTX = NULL;
  33. Private datasource DS = NULL;
  34. Private long timeout = 5000;
  35. Public dbmanager ()
  36. {
  37. Try
  38. {
  39. Initctx = new initialcontext ();
  40. // Init context, read config web. xml
  41. If (initctx = NULL)
  42. {
  43. Throw new exception ("Initial failed! ");
  44. }
  45. CTX = (context) initctx. Lookup ("Java: COMP/ENV ");
  46. // Find "JDBC/sqlserverdb" object this configruation in the server. xml of Tomcat
  47. If (CTX! = NULL)
  48. {
  49. DS = (datasource) CTX. Lookup ("JDBC/test ");
  50. }
  51. If (DS = NULL)
  52. {
  53. Throw new exception ("Look up datasource failed! ");
  54. }
  55. }
  56. Catch (exception E)
  57. {
  58. Log (E, "can't get the context! ");
  59. }
  60. }
  61. Public synchronized connection getconnection (){
  62. // Get connection and set to delay time
  63. Long starttime = new java. util. Date (). gettime ();
  64. Connection con = NULL;
  65. While (con = NULL ){
  66. Con = newconnection ();
  67. If (con! = NULL ){
  68. // Log ("create new connection! ");
  69. Break;
  70. }
  71. Try {
  72. Log ("connection timeout, reconnect, wait" + timeout + "Ms ");
  73. Wait (timeout );
  74. }
  75. Catch (interruptedexception e ){
  76. Log (E, "connection timeout! ");
  77. }
  78. If (New java. util. Date (). gettime ()-starttime)> = timeout ){
  79. Log ("connection timeout! ");
  80. Break;
  81. }
  82. }
  83. Return con;
  84. }
  85. Private connection newconnection (){
  86. Connection con = NULL;
  87. Try {
  88. Con = Ds. getconnection ();
  89. If (con = NULL ){
  90. Throw new exception ("create connection failed! ");
  91. }
  92. }
  93. Catch (exception e ){
  94. Log ("create connection failed! ");
  95. System. Out. println (E. getmessage ());
  96. }
  97. Return con;
  98. }
  99. Public synchronized void freeconnection (connection Conn,
  100. Statement stmt,
  101. Preparedstatement pstmt ){
  102. Try {
  103. // Close statement
  104. If (stmt! = NULL ){
  105. Stmt. Close ();
  106. Stmt = NULL;
  107. // Log ("Close statement ......");
  108. }
  109. // Close preparedstatement
  110. If (pstmt! = NULL ){
  111. Pstmt. Close ();
  112. Pstmt = NULL;
  113. // Log ("Close preparedstatement ......");
  114. }
  115. }
  116. Catch (exception e ){
  117. System. Out. println (E. getmessage ());
  118. }
  119. Try {
  120. // Close connection
  121. If (Conn! = NULL ){
  122. Conn. Close ();
  123. Conn = NULL;
  124. // Log ("close connection ......");
  125. }
  126. }
  127. Catch (sqlexception e ){
  128. Log (E, "An error occurred while releasing the resource! ");
  129. }
  130. }
  131. /************************************
  132. * Write log file.
  133. * @ Param s string
  134. ************************************/
  135. Private void log (string S)
  136. {
  137. If (verbose)
  138. {
  139. System. Out. println (New java. util. Date () + ":" + S );
  140. // Logger.info (New java. util. Date () + S );
  141. }
  142. }
  143. Private void log (throwable E, string MSG)
  144. {
  145. System. Out. println (New java. util. Date () + ":" + MSG );
  146. }
  147. }

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

Related Article

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.