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:
Jdk-1_5_0_01
tomcat5.5.12
mysql4.1.14
Mysql-connector-java-3.1.111-bin.jar

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

Java_home:\jdk1.5.0_01

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

CREATE TABLE tested
(
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"?>
<Server>
<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"/>
<GlobalNamingResources>
<environment
Name= "Simplevalue"
Type= "Java.lang.Integer"
Value= "/>"
<resource
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"/>
<resource
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"/>
</GlobalNamingResources>
<service
Name= "Catalina" >
<connector
port= "8080"
Redirectport= "8443"
Minsparethreads= "25"
connectiontimeout= "20000"
maxsparethreads= "75"
Maxthreads= "150"
Maxhttpheadersize= "8192" >
</Connector>
<connector
Port= "8009"
Redirectport= "8443"
connectiontimeout= "0"
Protocol= "ajp/1.3" >
</Connector>
<engine
defaulthost= "localhost"
Name= "Catalina" >
<realm classname= "Org.apache.catalina.realm.UserDatabaseRealm"/>
Appbase= "WebApps"
Name= "localhost" >
</Host>
</Engine>
</Service>
</Server>

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>)

<resource-ref>
<description>db connection</description>
<res-ref-name>Mypool</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

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.io.*,java.util.*,java.sql.*,javax.sql.*,javax.naming.*"%>
<%@ page contenttype= "text/html;charset=gb2312"%>
<body>
<%
try{
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 (Rs.next ()) {

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

}catch (Exception e) {
Out.print (E.getmessage ());
}
%>
</body>

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