Tomcat itself does not provide the implementation of the data source. But with some other open source data source, Tomcat container to manage it, the program can be obtained via JNDI data source.
Tomcat data source configuration is divided into two types:
Global Data source: All web applications can access.
Local data sources: You can only access a web application.
The following start configuring
Step by Step:
1. The database driver is copied to the tomcat / common / lib directory.
Other open source data source dependent jar files should be copying the directory.
2. If you configure global data sources, modify the conf / server.xml file, as follows:
maxActive = “100” maxIdle = “30” maxWait = “10000”
username = “root” password = “123456”
driverClassName = “com.mysql.jdbc.Driver”
url = “jdbc: mysql :/ / 127.0.0.1:3306 / test”
characterEncoding = GBK & useUnicode = TRUE “/>
Note: If the & character, need to be transferred into & (XML file specification). It is not recommended to configure global data sources.
If you configure the local data source, you only need to modify the configuration file in a specific web application, in Context Configuration node with the node of resources, similar to the same configuration of the global data source, just modify the file.
3. Modify the Web application WEB-INF/web.xml file, together with the Resource-Def, as follows:
<Description> DB Connection </ description>
<Res-ref-name> jdbc / TestDB </ res-ref-name>
<Res-type> javax.sql.DataSource </ res-type>
<Res-auth> Container </ res-auth>
4. Restart the Web application, Web applications can use the following code to get the data source and database connection:
DataSource ds = ctx.lookup (“java: comp / env / jdbc / TestDB”);
Connection conn = ds.getConnection ();
Statement stmt = conn.createStatement ();
ResultSet rs = stmt.executeQuery (“select * from newsinf”);
while (rs.next ())
/ / …………