The TOMCAT6 server configuration file is placed under the ${tomcat6}/conf directory. We can find Server.xml and context.xml here. Of course, there are other resource files. But in this article we can only use these two, the others are not introduced.
1, first, you need to configure a Jndi resource for the data source. Our data source Jndi resources should be defined in the context element. In the TOMCAT6 version, the context element has been isolated from the Server.xml file and placed in a context.xml file. Since Server.xml is a resource that cannot be reloaded dynamically, once the server has been started, to modify the file, you will have to restart the server to reload. The context.xml file does not, and the Tomcat server scans the file periodically. Once the file is found to be modified (the timestamp changes), the file is automatically reloaded without restarting the server. We certainly recommend that you configure the Jndi resources that your application requires in the Context.xml file instead of the Server.xml file.
2, first, the database driver copy to Tomcat6.0\lib, this is the key, if there is no copy when running the program after the report-no driver-found exception.
Place the following code in the middle of Tomcat 6.0\conf\context.xml, such as:
<context reloadable= "true" >
<resource
Name= "jdbc/database name"
Type= "Javax.sql.DataSource"
Maxactive= "100"
Maxidle= "30"
maxwait= "5000"
Username= "User Name"
password= "Password"
Driverclassname= "Database Driver"
Url= "Connection address for database"/>
</Context>
Explanation: (100 30 5000 for the above data)
Maxactive= "Maximum 100 users can connect to data source"
Maxidle= "If there is no user connection, empty 30 connections waiting for users to connect"
Maxwait= "If the connected user does not connect to the data source again within 5,000 seconds, discard this connection"
By completing these two, the data source is ready for use.
Alternatively, you can configure the new context.xml under Web-inf under Project root
<?xml version= "1.0" encoding= "UTF-8"?>
<context>/////Direct Connection
<resource
Name= "Jdbc/book"
Type= "Javax.sql.DataSource"
Password= ""
Driverclassname= "Com.microsoft.jdbc.sqlserver.SQLServerDriver"
Maxidle= "5"
maxwait= "5000"
Username= "SA"
Url= "Jdbc:microsoft:sqlserver://localhost:1433;databasename=book"////Direct Connect data source
Maxactive= "Ten"/>
</Context>
<!-->
<context>////Bridge Company
<resource
Name= "Jdbc/book"
Type= "Javax.sql.DataSource"
Password= ""
Driverclassname= "Sun.jdbc.odbc.JdbcOdbcDriver"
Maxidle= "2"
maxwait= "5000"
Username= "SA"
Url= "JDBC:ODBC:BB"///Bridge data source
Maxactive= "4"/>
<watchedresource>c:\program Files\apache Software Foundation\tomcat 5.5\conf\context.xml</watchedresource >
</Context>
<!-->
Add into the context file directly:
<resource
Name= "Jdb/dbsource"
Type= "Javax.sql.DataSource"
Driverclassname= "Com.microsoft.jdbc.sqlserver.SQLServerDriver"
Maxidle= "2"
maxwait= "5000"
Url= "Jdbc:microsoft:sqlserver://localhost:1433;databasename=jspdev"
Maxactive= "4"/>
Username= "he"
Password= "he"
Through the Java Jndi, you can do it.
InitialContext initctx = new InitialContext ();
DataSource ds = (DataSource) initctx.lookup ("java:comp/env/jdbc/database name");
Connection conn = Ds.getconnection ();
The following are the configuration of various databases
1.sql2000
<resource
Name= "jdbc/database name"
Type= "Javax.sql.DataSource"
Maxactive= "100"
Maxidle= "30"
maxwait= "5000"
Username= "User Name"
password= "Password"
Driverclassname= "Com.microsoft.jdbc.sqlserver.SQLServerDriver"
Url= "Jdbc:microsoft:sqlserver://127.0.0.1:1433;databasename= database name"
/>
2.oracle
<resource
Name= "jdbc/database name"
Type= "Javax.sql.DataSource"
Maxactive= "100"
Maxidle= "30"
maxwait= "5000"
Username= "User Name"
password= "Password"
Driverclassname= "Oracle.jdbc.driver.OracleDriver"
Url= "Jdbc:oracle:thin:@127.0.0.1:1521:ora9"
/>
3.mysql
<resource name= "Jdbc/mysql"
Auth= "Container"
Type= "Javax.sql.DataSource"//resource type
Driverclassname= "Org.gjt.mm.mysql.Driver"
Url= "jdbc:mysql://localhost/database name"
Username= "User Name"
password= "Password"
Maxactive= "100"//maximum number of links
maxidle= "30"//MAX idle time, 0 for unlimited
maxwait= "10000"/>//maximum wait time to establish a connection
Configure various database connection pools in Tomcat under Context.xml