Tomcat6.0 JNDI usage (connection pool)

Source: Internet
Author: User

Recently, a project used JNDI. To make it easy to change the database and environment, let's take a look at it. By the way, I will share the following methods: the methods on the Internet are messy, my eyes are dizzy. The methods I provided below are definitely usable by myself. If I try to send them to your website, it is estimated that they will be scolded by you. ......

Let's take a look at my project diagram:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1145395333-0.jpg "alt =" QQ20130310234141.jpg "/>

1. First, find the content in the context. xml change under the tomcat6.0conf directory:

 

 
  1. <?xml version='1.0' encoding='utf-8'?> 
  2. <Context> 
  3. <WatchedResource>WEB-INF/web.xml</WatchedResource> 
  4.     <Resource name="jdbc/mysqldb"     
  5.        auth="Container" 
  6.        type="javax.sql.DataSource" 
  7.        driverClassName="com.mysql.jdbc.Driver" 
  8.        url="jdbc:mysql://localhost:3306/test" 
  9.        username="root" 
  10.        password="123456" 
  11.        maxActive="4" 
  12.        maxIdle="2" 
  13.        maxWait="-1"  /> 
  14. <ResourceLink global="mysqldb" name="mysqldb" type="javax.sql.DataSource"/> 
  15. </Context> 

If you delete all the other items, replace them with the ones above.

Parameter description:

NameThe name of the current data source JNDI, which can be set at will;

AuthIs the verification method;

TypeResource type;

DriverClassNameIs referenced by the Oracle driver;

MaxActivIt is the maximum number of connections activated in the connection pool. If it is set to 0, there is no limit;

MaxIdleThe maximum number of idle connections in the connection pool and the maximum idle time of the database connection. When the idle time is exceeded, the database connection is marked as unavailable and then released. If it is set to 0, there is no limit;

MaxWaitThe maximum wait time for the connection, in milliseconds. If the wait time is exceeded, an exception is thrown. If it is set to-1, there is no limit;

UsernameIt is a username of the oracle database;

PasswordUsername Password;

UrlThe connection address for connecting to oracle;

 

2. Create a project named JNDIPro.

 

3. Create a ConnectionPool. java class

 

 
  1. package com.shyy.jndi; 
  2.  
  3. import java.sql.Connection; 
  4.  
  5. import javax.naming.Context; 
  6. import javax.naming.InitialContext; 
  7. import javax.sql.DataSource; 
  8.  
  9. public class ConnectionPool { 
  10.     private static Connection conn; 
  11.      
  12.     public static Connection getConn(){ 
  13.         try{ 
  14.             Context ctx = new InitialContext(); 
  15.             DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/mysqldb"); 
  16.             conn = ds.getConnection(); 
  17.         }catch(Exception e){ 
  18.             e.printStackTrace(); 
  19.         } 
  20.         return conn; 
  21.     } 
  22. } 

4. Some people on the Internet say they want to configure the web under the WebRoot/web-inf project. the xml file, as shown below, runs successfully without configuration. If it cannot be added, I cannot ensure that all environments are the same as mine)

Configure in the middle of <web-app> </web-app>,

 
  1.  <resource-ref>   
  2. <description>DB Connection test</description>   
  3. <res-ref-name>jdbc/mysqldb</res-ref-name>   
  4. <res-type>javax.sql.DataSource</res-type>   
  5. <res-auth>Container</res-auth>   
  6.  </resource-ref>  
 

5. Now let's write a JSP page to test the Code as follows:

 
 
  1. <% @ Page language = "java" contentType = "text/html; charset = UTF-8"
  2. PageEncoding = "UTF-8" %>
  3. <% @ Page import = "com. shyy. jndi. ConnectionPool, java. SQL. Connection, java. SQL. *" %>
  4. <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
  5. <Html>
  6. <Head>
  7. <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
  8. <Title> test the JNDI connection </title>
  9. </Head>
  10. <Body>
  11. <%
  12. ConnectionPool conn = new ConnectionPool ();
  13. Connection connconns = conn. getConn ();
  14. If (null! = Conns ){
  15. Out. println ("database connection status: <B> OK </B>
  16. PreparedStatement pst = conns. prepareStatement ("SELECT * FROM USER_INFO ");
  17. ResultSet rs = pst.exe cuteQuery ();
  18. While (rs. next ()){
  19. Int id = rs. getInt ("ID ");
  20. String name = rs. getString ("NAME ");
  21. String sex = rs. getString ("SEX ");
  22. String telPhone = rs. getString ("TELPHONE ");
  23. Out. println ("<B> id: </B>" + id + "<br/> <B> Name: </B> "+ name +" <br/> <B> Gender: </B> "+ sex +" <br/> <B> mobile phone number: </B> "+ telPhone +"
  24. }
  25. } Else {
  26. Out. println ("database connection status: NO ");
  27. }
  28. %>
  29. </Body>
  30. </Html>

6. start tomcat. The running result is as follows:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1145392458-1.jpg "alt =" QQ20130310233352.jpg "/>

 

OK. If you cannot, take a closer look at the above steps. I have succeeded.

The original Article is published in my personal blog thinker's diary, and is also published in BKJIA. Please keep this information for reprinting.

This article is from the "On My Way" blog, please be sure to keep this source http://shuyangyang.blog.51cto.com/1685768/1151537

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.