zoukankan      html  css  js  c++  java
  • eclipse+mysql+tomcat配置JNDI

    配置环境:Tomcat5.5、MySQL4.1 、mysql-connector-java-5.0.0-beta 、commons-dbcp-1.2.1、 JDK1.5以上的“东东”在网上都能够下到的。

    然后在D:\Tomcat5.5\webapps\新建目录DBtest文件夹,DBtest下建这样几个目录:WEB-INF、META-INF,在WEB-INF下创建两个文件夹和两个文件分别为classes、lib、web.xml、Test.jsp,在META-INF下创建context.xml(为什么要创建这个文件我也不太清楚 ,哪位高手知道还请多指教)

    接下来就要把JDBC驱动程序mysql-connector-java-5.0.0-beta和commons-dbcp-1.2.1解压,分别将解压后的.jar文件分别复制到D:\Tomcat5.5\common\lib下,以上工作做完就可以进行连接池的配置了。

    1、配置D:\Tomcat 5.5\conf\server.xml在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"/>

    注:name是连接池的名字可任取,type,是数据源的类型,driverClassName是驱动程序的类名好像得这么写,url是数据库的路径math为数据库名。

    2、配置D:\Tomcat 5.5\webapps\DBtest\WEB-INF\web.xml在中加入如入信息

    DB Connection
    Test
    javax.sql.DataSource
    Container

    注:其中的res-ref-name必须和前面的连接池名相同。

    3、配置D:\Tomcat 5.5\webapps\DBtest\META-INF\context.xml全部内容如下

             debug="5"
             reloadable="true"
             crossContext="true">

    注:path是工作区的路径,ResourceLink name和global好像都得和前面的连接池的名字相同,这里的global有什么用我也不太清楚啦,不好意思,嘻嘻!!

    java 代码

    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; //打印控制台控制
    30. //static Logger 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("连接超时,重新连接,等待" + timeout + "ms");   
    73.            wait(timeout);   
    74.          }   
    75. catch (InterruptedException e) {   
    76.            log(e, "连接超时!");   
    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, "释放资源出错!");   
    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

     描述:
     eclipse+mysql+tomcat配置JNDI实现例子

    下载

     文件名:
     triman.rar

     文件大小:
     1007 KB

    http://uuplace.javaeye.com/blog/88878

  • 相关阅读:
    The Lobo Project: Home of Lobo (Java Web Browser) and Cobra (HTML Rendering Engine)
    基于DOM树的网页相似度研究与应用《大连理工大学》2011年硕士论文
    学习用 c/c++写crawler
    终于用上gcc4.1编译的系统了
    Android 查找SDCard 下面的文件 函数
    对HTML5 Device API相关规范的解惑
    Windows Phone开发(14):数据模板
    Windows Phone开发(13):如何规范用户的输入行为
    InputScope的62个值
    转:Windows Phone 7 设计简介
  • 原文地址:https://www.cnblogs.com/pony/p/814382.html
Copyright © 2011-2022 走看看