The following articles mainly describe the correct configuration of the java jsp tomcat6 MySQL connection pool. Many people use Tomcat 5 to configure the java jsp tomcat6 MySQL connection pool, however, I personally think it is too time-consuming to use Tomcat 5. Now I will share the following:
1. required file: Installation (Installation File)
2. Configure the context. xml file under conf under tomcat, and add the java jsp tomcat6 MySQL connection pool between <context> </context> as follows:
- <Resource name="jdbc/MySQL"
- auth="Container"
- type="javax.sql.DataSource"
- driverClassName="com.MySQL.jdbc.Driver"
- url="jdbc:MySQL://localhost/test"
- username="root"
- password="root"
- maxActive="100"
- maxIdle="30"
- maxWait="10000" />
-
I don't need to talk about the above parameters. I know what it means.
3. Configure <web-app> </web-app> in the web. xml file of your application to add:
- Xml Code <resource-ref>
- <Description> DB Connection </description>
- <Res-ref-name> jdbc/MySQLx </res-ref-name>
- <Res-type> javax. SQL. DataSource </res-type>
- <Res-auth> Container </res-auth>
- </Resource-ref>
4. the success is that you do not need to use the original server. after the configuration is in xml, you can write the test program below. There will be a lot on the internet, mainly on the top. Of course, you need to put the connection drivers under the lib under Tomcat 6. the test code is as follows:
- Java code <! Doctype html public "-// w3c // dtd html 4.0 transitional // en"
- Http://www.w3.org/TR/REC-html40/strict.dtd>
- <% @ Page import = "java. SQL. *" %>
- <% @ Page import = "javax. SQL. *" %>
- <% @ Page import = "javax. naming. *" %>
- <% @ Page session = "false" %>
- <Html>
- <Head>
- <Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
- <Title> </title>
- <%
- Out. print ("My test started ");
- DataSource ds = null;
- Try {
- InitialContext ctx = new InitialContext ();
- Ds = (DataSource) ctx. lookup ("java: comp/env/jdbc/MySQL ");
- Connection conn = ds. getConnection ();
- Statement stmt = conn. createStatement ();
Tip: users must be an existing database table,
The database contained in the Data Source URL configuration mentioned above.
- String strSql = "select * from users ";
- ResultSet rs = stmt.exe cuteQuery (strSql );
- While (rs. next ()){
- Out. print (rs. getString (1 ));
- }
- Out. print ("My test ended ");
- }
- Catch (Exception ex ){
- Out. print ("exception, message:" + ex. getMessage ());
- Ex. printStackTrace ();
- }
- %>
- </Head>
- <Body>
- </Body>
- </Html>
I have tested the above. If you have any questions, please leave a message. The above content is an introduction to the configuration of the java jsp tomcat6 MySQL connection pool. I hope you will gain some benefits.