1) Start the Tomcat server, open the browser, and enter http: // localhost: 8080/admin (localhost is the name server or host) to go To the login page of the management interface, in this case, enter the user name and password required during the original installation and log on to the management interface.
2) Select Resources-Data sources to go to the Data Source configuration page. Select Data Source Actions-> Create New Data Source to go to the configuration details page.
The main content is as follows:
- JNDI Name: ->jdbc/mysql
- Data Source URL ->jdbc:mysql://localhost:3306/test
- JDBC Driver Class-> org.gjt.mm.mysql.Driver
3) modify the \ conf \ Catalina \ localhost directory to create an xml file named. xml for your published web application. (For example, testpool. xml) and add the following content:
- <? Xml version = "1.0" encoding = "UTF-8"?>
- <Context>
- <Resource
- Name = "jdbc/mysql"
- Type = "javax. SQL. DataSource"
- Password = "123456"
- DriverClassName = "org. gjt. mm. mysql. Driver"
- MaxIdle = "2"
- MaxWait = "50"
- Username = "root"
- Url = "jdbc: mysql: // localhost: 3306/test"
- MaxActive = "4"/>
-
- </Context>
- The content is the same as that in conf/server. xml <GlobalNamingResources>
- <Resource
- Name = "jdbc/mysql"
- Type = "javax. SQL. DataSource"
- Password = "123456"
- DriverClassName = "org. gjt. mm. mysql. Driver"
- MaxIdle = "2"
- MaxWait = "50"
- Username = "root"
- Url = "jdbc: mysql: // localhost: 3306/test"
- MaxActive = "4"/>
- </GlobalNamingResources>
If this step is missing, the following error occurs: Cannot create JDBC driver of class ''for connect URL 'null'
4) modify web. xml
Open % atat_home % \ conf \ web. xml or yourwebapp/web-inf/web. xml and add the following content:
- <resource-ref>
- <description>DB Connection</description>
- <res-ref-name>jdbc/mysql</res-ref-name>
- <res-type>javax.sql.DataSource</res-type>
- <res-auth>Container</res-auth>
- </resource-ref>
Note that the res-ref-name must be the same as the JNDI Name mentioned above.
Now, the configuration is complete!
5) use "java: comp/env/jdbc/mysql" to reference JNDI ";
Create a file to test. jsp:
- <% @ Page contentType = "text/html; charset = UTF-8" %>
- <% @ Page import = "java. SQL. *" %>
- <% @ Page import = "javax. SQL. *" %>
- <% @ Page import = "javax. naming. *" %>
- <Html>
- <Head>
- <Title> Tomcat connection pool test </title>
- </Head>
- <Body>
- <%
- Context ctx = new InitialContext ();
- Connection conn = null;
- DataSource ds = (DataSource) ctx. lookup ("java: comp/env/jdbc/mysql ");
- Conn = ds. getConnection ();
- Statement stmt = conn. createStatement (ResultSet. CONCUR_READ_ONLY, ResultSet. CONCUR_UPDATABLE );
- ResultSet rs1_stmt.exe cuteQuery ("select * from testexample ");
- While (rs. next ()){
- Out. println (rs. getInt (1 ));
- Out. println (rs. getString (2 ));
- Out. println (rs. getString (3 ));
- }
- Out. println ("database operation successful! ");
- Rs. close ();
- Stmt. close ();
- Conn. close ();
- %>
- </Body>
- </Html>