Tomcat database connection pool

Source: Internet
Author: User

1. mysql-jdbc package
2. install Tomcat-admin in % Tomcat home %, localhost: 8080/admin, 12345 (you can set your own password)
Tomcat-users.xml
<? XML version = '1. 0' encoding = 'utf-8'?>
<Tomcat-users>
<Role rolename = "Tomcat"/>
<Role rolename = "role1"/>
<Role rolename = "manager"/>
<Role rolename = "admin"/>
<User Username = "Tomcat" Password = "Tomcat" roles = "Tomcat"/>
<User Username = "role1" Password = "Tomcat" roles = "role1"/>
<User Username = "both" Password = "Tomcat" roles = "tomcat, role1"/>
<User Username = "admin" Password = "admin" roles = "Admin, Manager"/>
</Tomcat-users>
)
3. After Entering, click the tree node data sources on the left, and then select the "Create new data source" drop-down box in the upper right corner"
Enter the JNDI name connection pool JNDI name JDBC/MySQL
Datasource url jdbc: mysql: // localhost/DB
JDBC Driver Class com. MySQL. JDBC. Driver
Username Root
911
Max. Active. Connection Max connections
Max. idle. Connection maximum number of idle connections
Save in the lower right corner and Commit Changes in the upper right corner

This information is stored in the conf/server. xml file of Tomcat.
4. Modify CONF/context. xml
Add a sentence
<Context>
<Resourcelink name = "JDBC/MySQL" Global = "JDBC/MySQL"/>
</Context>

5 testdb. jsp
<% @ Page Language = "Java" contenttype = "text/html; charset = gb2312" Import = "Java. SQL. *, javax. SQL. *, javax. naming. * "%>

<%
String Path = request. getcontextpath ();
String basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/";
%>

<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
<HTML>
<Head>
<Base href = "<% = basepath %>">

<Title> my JSP 'index. jsp 'starting page </title>
<Meta http-equiv = "Pragma" content = "no-Cache">
<Meta http-equiv = "cache-control" content = "no-Cache">
<Meta http-equiv = "expires" content = "0">
<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "Description" content = "this is my page">
<! --
<LINK rel = "stylesheet" type = "text/CSS" href = "styles.css">
-->
</Head>

<Body>
<%
Connection con = NULL;
Statement Sm = NULL;
Resultset rs = NULL;
Try {
Initialcontext CTX = new initialcontext ();
Datasource DS = (datasource) CTX. Lookup ("Java: COMP/ENV/jdbc/MySQL ");
Con = Ds. getconnection ();
Sm = con. createstatement ();
Rsw.sm.exe cutequery ("select * From MySQL ");
While (Rs. Next ())
{
Out. println (Rs. getstring (2 ));

}

}
Catch (exception E)
{
E. printstacktrace ();

}

Finally {
If (RS! = NULL ){
Try {
Rs. Close ();
}
Catch (sqlexception E)
{
E. printstacktrace ();
}
Rs = NULL;

}

If (con! = NULL ){
Try {
Con. Close ();
}
Catch (sqlexception E)
{
E. printstacktrace ();
}
Con = NULL;

}
If (SM! = NULL ){
Try {
SM. Close ();
}
Catch (sqlexception E)
{
E. printstacktrace ();
}
Sm = NULL;

}

}
 
%>
</Body>
</Html>

6. Internal testing? How to Add Server

 

 

Ii. sqlserver2000 Configuration

To solve the problem that plagued the connection pool for many days, write this article to share with you. I configured it in tomcat5.5.9. tomcat5.5.x is different from the previous version, so the configuration is also different. Let me talk about the basic steps for configuring tomcat5.5.9: (Are you sure you want to install tomcat5.5.9 and SQL2000)

1. Copy the JDBC driver of the database to % atat_home %/common/lib and % tomcat_home %/webapps/yourweb/WEB-INF/lib (my web folder name is Quickstart)
Everyone knows this step. Otherwise, Tomcat cannot connect to the database.

2. Modify the % tomcat_home %/CONF/server. xml file between <globalnamingresources> </globalnamingresources>
Add the following content:

<Resource Name = "JDBC/Quickstart" type = "javax. SQL. datasource "Password =" 123456 "driverclassname =" com. microsoft. JDBC. sqlserver. sqlserverdriver "maxidle =" 2 "maxwait =" 5000 "username =" sa "url =" JDBC: Microsoft: sqlserver: // localhost; databasename = Quickstart "maxactive =" 4 "/>

The above content is modified based on your actual situation. For example, change Quickstart in name = "JDBC/Quickstart" to the same name as your web folder, change "123456" in Password = "123456" to your database password.

3. Create an XML file with the same name as your web folder under % tomcat_home %/CONF/Catalina/localhost (my name is Quickstart. XML)
This step is very important. If this step is not used, errors will occur.
Org. Apache. tomcat. DBCP. DBCP. sqlnestedexception: cannot create JDBC driver of class ''for connect URL 'null'

Add the following content to the file:

<? XML version = "1.0" encoding = "UTF-8"?> <Context> <Resource Name = "JDBC/Quickstart" type = "javax. SQL. datasource "Password =" 123456 "driverclassname =" com. microsoft. JDBC. sqlserver. sqlserverdriver "maxidle =" 2 "maxwait =" 5000 "username =" sa "url =" JDBC: Microsoft: sqlserver: // localhost; databasename = Quickstart "maxactive =" 4 "/> </context>

Modify the settings based on your actual situation.

4. Modify the Web. xml file under % tomcat_home %/webapps/yourweb/WEB-INF and add the following content between <web-app> </Web-app>

<Resource-ref> <description> sqlserverdb connection </description> <res-ref-Name> JDBC/Quickstart </RES-ref-Name> <res-type> javax. SQL. datasource </RES-type> <res-auth> container </RES-auth> </resource-ref>

Change Quickstart in <res-ref-Name> JDBC/Quickstart </RES-ref-Name> to your web folder name.

5. Test. Create an index. jsp file under Quickstart.

<% @ Page contenttype = "text/html; charset = gb2312" %> <% @ page import = "javax. naming. * "%> <% @ page import =" javax. SQL. * "%> <% @ page import =" Java. SQL. * "%> <HTML>

Run the Tomcat server and enter http: // localhost: 8080/Quickstart/index. jsp in the browser.

 

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.