TOMCAT5 Configuration MYSQL4 Data source

Source: Internet
Author: User
Tags auth insert sql mysql mysql in stmt version tomcat
mysql| Data | data source

I. Configure environment variables:

Windows XP + SP2
Softe version:

Path: TOMCAT5 in C:\Tomcat 5.5;
MySQL in C:\Program files\mysql\mysql Server 4.1
JDK in C:\jdk1.5.0_01

Path (added on the original basis): C:\Tomcat 5.5\bin; C:\jdk1.5.0_01; C:\jdk1.5.0_01\bin; C:\Tomcat 5.5\common\lib\servlet-api.jar; C:\Program Files\mysql\mysql Server 4.1\bin

Classpath:c:\tomcat 5.5\common\lib\servlet-api.jar; C:\Tomcat 5.5\common\lib\jsp-api.jar


Catalina_home:c:\tomcat 5.5

Second, the establishment of test database

Create a MYWEBDB database in MySQL while creating a table tested as follows

Create DATABASE Mywebdb

ID int (2),
Name varchar (6)

Then insert two test data as follows

Insert INTO member values (1, "Holmes");
Insert into member values (2, "Conan");

At this point, the database is ready to complete

Third, TOMCAT5 data source Configuration

1 Login to admin account to find tomcatserver/service/host/your Web program name/datasource//--> click here.

2 Select "Create New Data Source".

3 Fill in the information
JNDI Name: mypool //--> Connection pool name
Data Source Url:jdbc:mysql://localhost:3306/mywebdb?autoreconnect=true
JDBC Driver Class:com.mysql.jdbc.Driver
Drivers need to be downloaded.
User name://Database username.
Password://Database password.
Max. Active connections://maximum number of active links, 0 is not limited.
Max. Idle connections://maximum number of waiting links, 0 is not limited.
Max. Wait for Connection://Establish connection Timeout Ms,-1 Unlimited.

4 Save certain points after the commit changes.

5 Do not reboot, just refresh the page.

My server.xml content is:

<?xml version= "1.0" encoding= "UTF-8"?>
<listener classname= "Org.apache.catalina.core.AprLifecycleListener"/>
<listener classname= "Org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
<listener classname= "Org.apache.catalina.storeconfig.StoreConfigLifecycleListener"/>
<listener classname= "Org.apache.catalina.mbeans.ServerLifecycleListener"/>
Name= "Simplevalue"
Type= "Java.lang.Integer"
Value= "/>"
Auth= "Container"
description= "User database" can be updated and saved "
Name= "Userdatabase"
Type= "Org.apache.catalina.UserDatabase"
Pathname= "Conf/tomcat-users.xml"
factory= "Org.apache.catalina.users.MemoryUserDatabaseFactory"/>
Name= "Mypool"
Type= "Javax.sql.DataSource"
Driverclassname= "Com.mysql.jdbc.Driver"
password= "Admin"
Maxidle= "5"
maxwait= "5000"
Username= "Root"
Url= "Jdbc:mysql://localhost:3306/mywebdb?autoreconnect=true"
Maxactive= "5"/>
Name= "Catalina" >
port= "8080"
Redirectport= "8443"
Minsparethreads= "25"
connectiontimeout= "20000"
maxsparethreads= "75"
Maxthreads= "150"
Maxhttpheadersize= "8192" >
Port= "8009"
Redirectport= "8443"
connectiontimeout= "0"
Protocol= "ajp/1.3" >
defaulthost= "localhost"
Name= "Catalina" >
<realm classname= "Org.apache.catalina.realm.UserDatabaseRealm"/>
Appbase= "WebApps"
Name= "localhost" >

Iv. Configuration of Web.xml

Web.xml is located under C:\Tomcat 5.5\webapps\root\web-inf, which is also the web.xml of your WEB. (I was modified directly using Tomcat's project)

Also open web.xml with a text editor and add the following statement (between <web-app> and </web-app>)

<description>db connection</description>

V. Write Test JSP page

Write a test.jsp under C:\Tomcat 5.5\webapps\root, and the code is as follows:
<%@ page import= "*,java.util.*,java.sql.*,javax.sql.*,javax.naming.*"%>
<%@ page contenttype= "text/html;charset=gb2312"%>
Statement stmt;
ResultSet rs;

Javax.naming.Context ctx=new Javax.naming.InitialContext ();
Javax.sql.DataSource ds= (Javax.sql.DataSource) ctx.lookup ("java:comp/env/mypool"); The red Word is the connection pool name, other fixed.
Java.sql.Connection con=ds.getconnection ();

Stmt=con.createstatement ();
Rs=stmt.executequery ("SELECT * from tested");
while ( ()) {

Out.print (Rs.getint (1));
Out.print (rs.getstring (2));
Rs.close ();
Stmt.close ();
Con.close ();

}catch (Exception e) {
Out.print (E.getmessage ());

Six, start testing

Run tomcat, open ie in the Address bar, enter:http://localhost:8080/ROOT/test.jsp.

But the results are:

Cannot create JDBC driver of class ' for connect URL ' null '

Please advise the master! Hurry

Related Article

Alibaba Cloud 10 Year Anniversary

With You, We are Shaping a Digital World, 2009-2019

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.