Excerpt: Tomcat Mysql Data Connection Pool

Source: Internet
Author: User
Body: Go to the Management page and create datasource: JNDI name: JDBC/MySQL
Data source URL: JDBC: mysql: // localhost: 3306/CS
JDBC Driver Class: COM. MySQL. JDBC. Driver
User name: Root
Password :********
Max. Active connections: 4
Max. idle connections: 2
Max. Wait for connection: 5000
Validation query:

% Tomcat_home % \ conf \Web. XML,In</Web-app>Before:
<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>

Add

<Resourcelink name = "JDBC/MySQL" Global = "JDBC/MySQL" type = "javax. SQL. Cer CER"/>

 

TestCode

<! 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:UsersMust be an existing table in the database,

//The database mentioned aboveData source URLThe database contained in the configuration.

String strsql = "select * from users ";

Resultset rs = stmt.exe cutequery (strsql );

While (Rs. Next ()){

Out. Print (Rs. getstring (1 ));

 }

Out. Print ("My test is complete");

}

Catch (exception ex ){

Out. Print ("Exception: the message is: "+ Ex. getmessage ());

Ex. printstacktrace ();

}

%>

</Head>

<Body>

</Body>

</Html>

Category:JavaRelease date:

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.