The methods for configuring data sources in Tomcat are slightly different in different versions. If you do not pay attention to these differences, web applications may be deployed in different environments.
The data source configuration of Tomcat 5 differs from that of Tomcat 5.5. The data source configuration of Tomcat 6 is basically the same as that of Tomcat 5.5.
Here, atat6 + MySQL is used to configure the Data source:
Under the webroot directory, create a new META-INF directory (if not exist) and create a context under that directory. XML file (or global context in the conf directory. in the context. when the following configuration information is added to the XML file (you must modify it based on your situation ):
Here we choose to create a new context. xml file under the META-INF, the content of this file is:
[XHTML]
View plaincopyprint?
- <Context>
- <Resource Name = "JDBC/dstest" auth = "Container"
- Type = "javax. SQL. datasource"
- Maxactive = "5" maxidle = "2" maxwait= "10000"
- Username = "root" Password = "root"
- Driverclassname = "com. MySQL. JDBC. Driver"
- Url = "JDBC: mysql: // localhost: 3306/dstest"/>
- </Context>
<Context> <br/> <Resource Name = "JDBC/dstest" auth = "Container" <br/> type = "javax. SQL. datasource "<br/> maxactive =" 5 "maxidle =" 2 "maxwait =" 10000 "<br/> username =" root "Password =" root "<br/> driverclassname = "com. mySQL. JDBC. driver "<br/> url =" JDBC: mysql: // localhost: 3306/dstest "/> <br/> </context>
// Configure Resource Based on your own situation
Name: Specifies the JNDI name of the data source in the container.
Maxactive: specifies the maximum number of active connections of the data source.
Maxidle: specifies the maximum number of idle connections in the data source.
Maxwait: specifies the maximum client waiting for connection in the data source.
Username: Specifies the username used to connect to the database.
Password: password used to connect to the database
Driverclassname: Specifies the driver used to connect to the database.
URL: Specifies the URL of the database service.
In the past, Tomcat also needed to specify the corresponding resource in Web. XML, which can be left empty in Versions later than Tomcat 5.5. However, we recommend that you configure the resource as follows:
- <Resource-ref>
- <Description> Tomcat datasource </description>
- <Res-ref-Name> JDBC/dstest </RES-ref-Name>
- <Res-type> javax. SQL. datasource </RES-type>
- <Res-auth> container </RES-auth>
- </Resource-ref>
<Resource-ref> <br/> <description> Tomcat datasource </description> <br/> <res-ref-Name> JDBC/dstest </RES-ref-Name> <br/> <res-type> javax. SQL. datasource </RES-type> <br/> <res-auth> container </RES-auth> <br/> </resource-ref> <br/>
// This is easy to understand, so I will explain it...
Finally, remember to store the MySQL driver in % atat_home %/lib. This is required. Otherwise, Tomcat cannot find the driver, so it cannot help you with new.
OK. The configuration is complete. Compile a small program for testing.
---------------------------------------------------------------------------
Assume that the above steps have been configured,
Use myeclipse to create a web project named datasource
Compile a test Servlet and place it in the Wen. Hui. DS package. The Code is as follows:
[Java]
View plaincopyprint?
- Package Wen. Hui. Ds;
- Import java. Io. ioexception;
- Import java. SQL. connection;
- Import java. SQL. resultset;
- Import java. SQL. statement;
- Import javax. Naming. context;
- Import javax. Naming. initialcontext;
- Import javax. servlet. servletexception;
- Import javax. servlet. http. httpservlet;
- Import javax. servlet. http. httpservletrequest;
- Import javax. servlet. http. httpservletresponse;
- Import javax. SQL. datasource;
- Public class testdatasource extends httpservlet {
- Protected void Service (httpservletrequest req, httpservletresponse resp)
- Throws servletexception, ioexception {
- Try {
- Context CTX = new initialcontext ();
- Datasource DS = (datasource) CTX. Lookup ("Java: COMP/ENV/dstest ");
- Connection conn = Ds. getconnection ();
- Statement stmt = conn. createstatement ();
- Resultset rs = stmt.exe cutequery ("select * from user ");
- While (Rs. Next ()){
- System. Out. println (Rs. getstring (1) + "," + Rs. getstring (2 ));
- }
- Rs. Close ();
- Stmt. Close ();
- } Catch (exception e ){
- E. printstacktrace ();
- }
- }
- }
Package Wen. hui. DS; </P> <p> Import Java. io. ioexception; <br/> Import Java. SQL. connection; <br/> Import Java. SQL. resultset; <br/> Import Java. SQL. statement; </P> <p> Import javax. naming. context; <br/> Import javax. naming. initialcontext; <br/> Import javax. servlet. servletexception; <br/> Import javax. servlet. HTTP. httpservlet; <br/> Import javax. servlet. HTTP. httpservletrequest; <br/> Import javax. servlet. HTTP. httpservletresponse; <br/> Import javax. SQL. datasource; </P> <p> public class testdatasource extends httpservlet {<br/> protected void Service (httpservletrequest req, httpservletresponse resp) <br/> throws servletexception, ioexception {<br/> try {<br/> context CTX = new initialcontext (); <br/> datasource DS = (datasource) CTX. lookup ("Java: COMP/ENV/dstest"); <br/> connection conn = Ds. getconnection (); <br/> statement stmt = Conn. createstatement (); <br/> resultset rs = stmt.exe cutequery ("select * from user"); <br/> while (RS. next () {<br/> system. out. println (RS. getstring (1) + "," + Rs. getstring (2); <br/>}< br/> Rs. close (); <br/> stmt. close (); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}< br/>
Configure servlet in Web. xml
[XHTML]
View plaincopyprint?
- <Servlet>
- <Servlet-Name> test </servlet-Name>
- <Servlet-class> Wen. Hui. Ds. testdatasource </servlet-class>
- </Servlet>
- <Servlet-mapping>
- <Servlet-Name> test </servlet-Name>
- <URL-pattern>/test </url-pattern>
- </Servlet-mapping>
<Servlet> <br/> <servlet-Name> test </servlet-Name> <br/> <servlet-class> Wen. hui. DS. testdatasource </servlet-class> <br/> </servlet> <br/> <servlet-mapping> <br/> <servlet-Name> test </servlet-Name> <br/> <URL-pattern>/test </url-pattern> <br/> </servlet-mapping>
Create a database:
Drop database if exists dstest;
Create Database dstest;
Use dstest;
Create Table user (
Username varchar (20) primary key,
Password varchar (20) not null
);
Insert into user values ('wwh', '20140901 ');
Insert into user values ('wwf', '20140901 ');
Insert into user values ('wwe', '20140901 ');
Deploy the project and access http: // localhost: 8080/datasource/test
Background output:
WWE, 123.
WWF, 123.
WWH, 123