Configuration and application demonstration of Tomcat Data Connection Pool Based on MySQL and SQL Server

Source: Internet
Author: User

In fact, the two Database Connection Methods of MySQL and SQL Server are very similar.

The encoding format uses uriencoding = "UTF-8" you can set as GBK or gb2312 as needed, but I personally strongly recommend that all
UTF-8

 

For example, Mysql Data Connection Pool settings:

 

1. Copy the MySQL driver file mysql. jar to Tomcat/lib.

2. Edit the directory META-INF/context. xml file in the Web application with the following content:

<Context Path = "/webapp" uriencoding = "UTF-8">
<Resource Name ="JDBC/myjdbc"
Auth = "Container"
Description = "DB connection"
Driverclassname = "com. MySQL. JDBC. Driver"
Maxactive = "500"
Maxidle = "30"
Maxwait = "10000"
Type = "javax. SQL. datasource"
Username = "sqluser1"
Password = "123456"
Url = "JDBC: mysql: // 192.168.1.2: 3306/mydb? Autoreconnect = true"
/>
</Context>

 

Webapp: Your app name

Mydb: name of the database in MySQL

Com. MySQL. JDBC. DRIVER: The driver used, from the jar file of MySQL.

 

3. Edit the contents of directory WEB-INF/Web. XML in the Web application as follows

 

<Resource-ref>
<Description> dB connection </description>
<Res-ref-Name>JDBC/myjdbc</RES-ref-Name>
<Res-type> javax. SQL. datasource </RES-type>
<Res-auth> container </RES-auth>
</Resource-ref>

 

So far, the data connection pool configuration for MySQL is complete.

 

SQL Server Data Connection Pool settings:

 

1. Copy the sqljdbc. Jar driver file of sqlserver
Tomcat/lib

2. Edit the directory META-INF/context. xml file in the Web application with the following content:

<? XML version = "1.0" encoding = "UTF-8"?>
<Context Path = "/webapp1" uriencoding = "UTF-8">
<Resource
Description = "DB connection"
Driverclassname = "com. Microsoft. sqlserver. JDBC. sqlserverdriver"
Maxactive = "500"
Maxidle = "30"
Maxwait = "10000"
Name ="JDBC/msjdbc"
Auth = "Container"
Type = "javax. SQL. datasource"
Username = "sqluser2"
Password = "123456"
Url = "JDBC: sqlserver: // localhost: 1433; databasename = mydb"
/>
</Context>
3. Edit the contents of directory WEB-INF/Web. XML in the Web application as follows

<Resource-ref>
<Description> dB connection </description>
<Res-ref-Name>JDBC/msjdbc</RES-ref-Name>
<Res-type> javax. SQL. datasource </RES-type>
<Res-auth> container </RES-auth>
</Resource-ref>

So far, the data connection pool configuration for SQL Server has been completed. Microsoft jdbc1.2 is compatible with both MySQL 2000 and MySQL 2005. I have actually passed the test.

 

 

The connection pool call code is as follows:

 

Import java. SQL .*;

Import javax. SQL .*;

Import javax. Naming .*;

 

...

...

 

Connection con;
Statement stmt;
Resultset RS;

Try
{
// Connect to the database
Context CTX = new initialcontext ();

// The following two connection modes are determined based on the connection to MySQL or sqlserver. Select only one of them.

Datasource DS = (datasource) CTX. Lookup ("Java: COMP/ENV/jdbc/myjdbc"); // Use MySQL

Datasource DS = (datasource) CTX. Lookup ("JDBC/msjdbc"); // Use SQL Server

Con = Ds. getconnection ();
// Create a JDBC Statement
Stmt = con. createstatement ();

/* Stmt = con. createstatement (resultset. type_scroll_sensitive, resultset. concur_updatable); // use the last or first method in SQL Server */

Try
{

// Construct SQL commands

String sql1_text = "select * From message_text ";

// Execute the SQL command

Rs = stmt.exe cutequery (sqlcmdselect );

// Traverse all records

While (Rs. Next ()

{

String outtext = Rs. getstring ("fieldname ");

......

......
}

// Resource release
If (RS! = NULL)
{
Rs. Close ();
}

}
Catch (exception E)
{
}

// Resource release

If (stmt! = NULL)
{
Stmt. Close ();
}
If (con! = NULL)
{
Con. Close ();
}
}
Catch (exception E)
{
}

// Complete

 

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.