Tomcat6 data source configuration.

Source: Internet
Author: User

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?
  1. <Context>
  2. <Resource Name = "JDBC/dstest" auth = "Container"
  3. Type = "javax. SQL. datasource"
  4. Maxactive = "5" maxidle = "2" maxwait= "10000"
  5. Username = "root" Password = "root"
  6. Driverclassname = "com. MySQL. JDBC. Driver"
  7. Url = "JDBC: mysql: // localhost: 3306/dstest"/>
  8. </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:

  1. <Resource-ref>
  2. <Description> Tomcat datasource </description>
  3. <Res-ref-Name> JDBC/dstest </RES-ref-Name>
  4. <Res-type> javax. SQL. datasource </RES-type>
  5. <Res-auth> container </RES-auth>
  6. </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?
  1. Package Wen. Hui. Ds;
  2. Import java. Io. ioexception;
  3. Import java. SQL. connection;
  4. Import java. SQL. resultset;
  5. Import java. SQL. statement;
  6. Import javax. Naming. context;
  7. Import javax. Naming. initialcontext;
  8. Import javax. servlet. servletexception;
  9. Import javax. servlet. http. httpservlet;
  10. Import javax. servlet. http. httpservletrequest;
  11. Import javax. servlet. http. httpservletresponse;
  12. Import javax. SQL. datasource;
  13. Public class testdatasource extends httpservlet {
  14. Protected void Service (httpservletrequest req, httpservletresponse resp)
  15. Throws servletexception, ioexception {
  16. Try {
  17. Context CTX = new initialcontext ();
  18. Datasource DS = (datasource) CTX. Lookup ("Java: COMP/ENV/dstest ");
  19. Connection conn = Ds. getconnection ();
  20. Statement stmt = conn. createstatement ();
  21. Resultset rs = stmt.exe cutequery ("select * from user ");
  22. While (Rs. Next ()){
  23. System. Out. println (Rs. getstring (1) + "," + Rs. getstring (2 ));
  24. }
  25. Rs. Close ();
  26. Stmt. Close ();
  27. } Catch (exception e ){
  28. E. printstacktrace ();
  29. }
  30. }
  31. }

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?
  1. <Servlet>
  2. <Servlet-Name> test </servlet-Name>
  3. <Servlet-class> Wen. Hui. Ds. testdatasource </servlet-class>
  4. </Servlet>
  5. <Servlet-mapping>
  6. <Servlet-Name> test </servlet-Name>
  7. <URL-pattern>/test </url-pattern>
  8. </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

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.