Tomcat uses JNDI to connect to the MySQL database

Source: Internet
Author: User

Environment:

Jdk1.4 + Tomcat 5

I. environment variables:

Path: C:/j2sdk1.4.2/bin;

Java_home: C:/j2sdk1.4.2/

Note: Only the two.

II. JDBC driver

Copy the corresponding JDBC driver to tomcat5/common/lib
Three. Jar files in the SQL Server 2000 (or MySQL) for JDBC/lib/directory:
Msbase. Jar
MSSQLServer. Jar
Msutil. Jar
 

Iii. virtual directory and Connection Pool

Suppose you want to create your own virtual directory in D:/jsgtest

/Myjsp

Assume that the connection pool JNDI name is AAA.

Assume that the connection database is chengji (Table chengji in the test database)

In

Tomcat5/CONF/Catalina/localhost

Directory to create a file:

Myjsp. xml
 
The content is as follows:

<Context crosscontext = "true" displayname = "myjsp" docbase = "D:/jsgtest" Path = "/myjsp" reloadable = "true">
<Resource Name = "AAA" type = "javax. SQL. datasource"/>
<Resourceparams name = "AAA">

<Parameter>
<Name> URL </Name>
<Value> JDBC: mysql: // localhost: 3306/test </value>
</Parameter>

<Parameter>
<Name> maxactive </Name>
<Value> 4 </value>
</Parameter>

<Parameter>
<Name> maxwait </Name>
<Value> 5000 </value>
</Parameter>

<Parameter>
<Name> driverclassname </Name>
<! -- Old driver <value> org. gjt. Mm. MySQL. Driver </value> -->
<Value> com. MySQL. JDBC. Driver </value>
</Parameter>

<Parameter>
<Name> username </Name>
<Value> root </value>
</Parameter>

<Parameter>
<Name> password </Name>
<Value> </value>
</Parameter>

<Parameter>
<Name> maxidle </Name>
<Value> 2 </value>
</Parameter>

</Resourceparams>
</Context>

Test:

Create the file test. jsp in the Directory D:/jsgtest:

<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. SQL. *" %>
<% @ Page import = "javax. SQL. *" %>
<% @ Page import = "javax. Naming. *" %>
<%
Connection conn = NULL;
Context initctx = new initialcontext ();
If (initctx = NULL)
Throw new exception ("context cannot be obtained! ");
Context CTX = (context) initctx. Lookup ("Java: COMP/ENV"); // obtain the connection pool object
Object OBJ = (object) CTX. Lookup ("AAA"); // type conversion
Javax. SQL. datasource DS = (javax. SQL. datasource) OBJ;

Conn = Ds. getconnection ();

Statement stmt = conn. createstatement ();
String strsql = "select * From chengji ";
Resultset rs = stmt.exe cutequery (strsql );
While (Rs. Next ()){
Out. println (Rs. getstring (1 ));
}
Rs. Close ();
Stmt. Close ();
Conn. Close ();

Out. println ("connection pool test succeeded ");
%>

 

Test path:

HTTP: /localhost: 8080/myjsp/test. jsp

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.