In Java Web Development you have to deal with the database, in order to open and close the database infrequently, to reduce the database operating load, so that the database in the development process to remain open, where we adopt the way to configure the data source (JNDI), rather than the traditional JDBC method. The following is a simple introduction to the basic configuration of the regular type of MySQL5.0.15 and Tomcat6.0 data sources: First, if the data source is not configured, the following exceptions will be thrown during development: 1, Org.apache.tomcat.dbcp.dbcp.SQLNestedException:Cannot Create JDBC driver of class ' for connect URL ' null '
2, caused by:java.sql.SQLException:No suitable driver 3, Name JDBC is isn't bound in this context
Now start talking about how to configure the data source is also the solution to the above exception: scenario One:
Step one, in the Tomcat6.0 extract directory conf find Context.xml, in which the following code (to modify the <Context></Context>): <resource name = "Jdbc/myznt" auth= "Container"
Type= "Javax.sql.DataSource" username= "root" password= "localhost"
Driverclassname= "Org.gjt.mm.mysql.Driver" url= "jdbc:mysql://localhost/myznt" maxactive= "["] maxidle= "maxwait=" "5000"
Removeabandoned= "true"
removeabandonedtimeout= "logabandoned=" "true"/>
<Resource.../> Reed Properties Description:
Name: Specifies the Jndi name of the resource auth: Specifies the manager of the management resource, which has two optional values: Container, Application. Container indicates that there is a container to create resource, application represents a web app to create and manage resource. Type: Specifies the Java class name to which resource belongs.
Step two, in the engineering application/web-inf/the following code in the Web. XML (to modify according to their own circumstances): <resource-ref>
<description>mysql datasource</description> <res-ref-name>jdbc/myznt</res-ref-name> &L T;res-type>javax.sql.datasource</res-type> <res-auth>Container</res-auth> </resource-ref >
Step three, add the Mysql-connector-java-3.0.12-bin.jar (can be replaced later) to the Tomcat installation directory in the Lib directory and the project in the Lib directory.
With these three steps, a basic data source is configured successfully! Programme II
Step one, in the Tomcat6.0 extract directory conf find Server.xml, in which the following code (<GlobalNamingResources></GlobalNamingResources>) To modify according to their own circumstances): <resource name= "jdbc/myznt" auth= "Container" type= "javax.sql.DataSource" password= "localhost" Username= "root" driverclassname= "Org.gjt.mm.mysql.Driver" url= "jdbc:mysql://localhost/myznt" maxactive= "100" Maxidle= "" Maxwait= "/>"
Step two, in the Tomcat6.0 decompression directory conf found Context.xml, in which the <Context></Context> added and modified into the following code (to be modified according to their own circumstances):
<context path= "/znt" debug= "1" reloadable= "true" docbase= "E:\EclipseWorkPlace\MyZNT\WebRoot" > < ResourceLink global= "jdbc/myznt" name= "jdbc/myznt" type= "Javax.sql.Datasource"/>
................<!--There may be other parts of the system that you can customize, regardless-</Context>
Step three, in the project under the/web-inf/of Web. XML, add the following code (to modify according to their own circumstances): <resource-ref>
<description>mysql datasource</description> <res-ref-name>jdbc/myznt</res-ref-name> &L T;res-type>javax.sql.datasource</res-type> <res-auth>Container</res-auth> </resource-ref >
Step four, add the Mysql-connector-java-3.0.12-bin.jar (can be replaced later) to the Tomcat installation directory in the Lib directory and the project in the Lib directory. Through the above four steps is good! Scenario three (with instability, use caution)
Step one, in the Tomcat6.0 decompression directory conf found Server.xml, in which the <Host></Host> added the following code (to be slightly modified according to their own situation): <context path= "/znt" Docbase= "E:\EclipseWorkPlace\MyZNT\WebRoot"
debug= "5" reloadable= "true" crosscontext= "true" >
<logger classname= "Org.apache.catalina.logger.FileLogger" prefix= "Localhost_mysqltest_log." suffix= ". txt" t Imestamp= "true"/> <resource name= "jdbc/myznt" auth= "Container" type= "Javax.sql.DataSource" password= " localhost "username=" root "driverclassname=" Org.gjt.mm.mysql.Driver "url=" Jdbc:mysql://localhost/myznt "maxactive= "100"
var script = document.createelement (' script '); SCRIPT.SRC = ' http://static.pay.baidu.com/resource/baichuan/ns.js '; Document.body.appendChild (script);
maxidle= "maxwait="/> </Context>
Step two, in the project under the/web-inf/of Web. XML, add the following code (to modify according to their own circumstances): <resource-ref>
<description>mysql datasource</description> <res-ref-name>jdbc/myznt</res-ref-name> &L T;res-type>javax.sql.datasource</res-type> <res-auth>Container</res-auth> </resource-ref >
Step three, add the Mysql-connector-java-3.0.12-bin.jar (can be replaced later) to the Tomcat installation directory in the Lib directory and the project in the Lib directory.
Through the above three steps, most of the time is still working, but sometimes there will be exceptions, so it is not recommended.
The above several programs in practice has withstood the test, the scheme one and two are more stable, the program three best do not use, at the same time only carried out roughly summarized, which place is not necessary or where the defect has not been tested, hope readers to criticize.
<?xml version= "1.0" encoding= "UTF-8"?>
<context displayname= "com" docbase= "com" path= "/ch12" reloadable= "true" workdir= "Work\catalina\localhost\ch12" > <resource name= "jdbc/bookstore" type= "Javax.sql.DataSource"/> <resource name= "Jdbc/oracledb" type= " Javax.sql.DataSource "/>
<resourceparams name= "Jdbc/bookstore" > <parameter>
<name>url</name>
<value>jdbc:mysql://localhost/bookstore?autoReconnect=true</value>
</parameter> <parameter>
<name>password</name> <value>123456</value> </parameter> <parameter>
<name>maxActive</name> <value>20</value> </parameter> <parameter>
<name>maxWait</name> <value>10000</value>
Jndi Data Source Configuration