Tomcat + mysql database connection pool configuration

xiaoxiao2021-03-06  20

1) Download mm.mysql-2.0.14-bin.jar package, place in the CommON / lib folder under the Tomcat directory

2) Join before in the CONF / Server.xml under the Tomcat directory (Note: All uppercase places should be replaced with your own things)

<-! For mysql database connection pool -> factory org.apache.commons.dbcp.basicDataSourceFactory driverclassname Org.gjt.mm.mysql.driver url <

Value> JDBC: mysql: // localhost: 3306 / database_name? Autoreconnect = true username db_username password db_password maxactive 100 maxidle 30 <

! - Maximum Time to Wait for A DB Connection To Become Available In MS. AN Exception Is Thrown if this timeout is excsseded. Set to -1 to wait indefinitely .--> MaxWait 10000 This will end up 3) Test:

Database_name: TestDB

Application_name: MySQLTEST

New Table UserInfo in TestDB Database

ID | Username1 aaa2 bbb

4) Web.xml

MySQL Datasource Example JDBC / TESTDB javax.sql.datasource Container

5) Index.jsp

<% @ Page ContentType = "Text / HTML; Charset = GB2312" Language = "Java" Import = "java.sql. *, javax.naming. *"%>

<% Context infectx = new initialcontext (); context ctx = (context) INITCTX.LOOKUP ("java: comp / env"); object obj = (object) CTX.lookup ("JDBC / TestDB"); Javax.sql. DataSource ds = (javax.sql.DataSource) obj; Connection conn = ds.getConnection (); Statement stmt = conn.createStatement (); String strSql = "select * from userinfo"; ResultSet rs = stmt.executeQuery (strSql); %>

<% while (rs.next ()) {%>

<% = rs.getstring ("Username")%> <%}%>

It can be seen that the output is:

Aaabbb

转载请注明原文地址:https://www.9cbs.com/read-43609.html

New Post(0)