<P> 4. Tomcat configures the data source to access the MySQL database through JNDI </P> <p> cannot create JDBC driver of class ''for connect URL 'null' error </P>
1.Add the MySQL JDBC driver to build path and copy it to the Tomcat directory/lib.
2.Add
<Context Path = "/BBS" docbase = "BBS" DEBUG = "5" reloadable = "true" crosscontext = "true">
<Resource Name = "JDBC/BBS" auth = "Container" type = "javax. SQL. datasource"
Maxactive = "100" maxidle = "30" maxwait = "10000"
Username = "root" Password = "0." driverclassname = "com. MySQL. JDBC. Driver"
Url = "JDBC: mysql: // localhost: 3306/BBS? Autoreconnect = true "/>
</Context>
3.Add <web-app> </Web-app> between the web-INF/Web. xml file of the project.
<Resource-ref>
<Description> dB connection </description>
<Res-ref-Name> JDBC/BBS </RES-ref-Name>
<Res-type> javax. SQL. datasource </RES-type>
<Res-auth> container </RES-auth>
</Resource-ref>
4.Create test JSP
<% @ Page contenttype = "text/html; charset = UTF-8" %>
<% @ Page Language = "Java" %>
<% @ Page import = "Java. util. *" %>
<% @ Page import = "Java. SQL. *" %>
<% @ Page import = "javax. SQL. *" %>
<% @ Page import = "javax. Naming. *" %>
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> test </title>
</Head>
<Body bgcolor = "# daf9fe">
<H1> MySQL JNDI test
<HR>
<%
Datasource DS = NULL;
Try {
Context initctx = new initialcontext ();
If (initctx = NULL)
Throw new exception ("Initial failed! ");
Context CTX = (context) initctx. Lookup ("Java: COMP/ENV ");
If (CTX! = NULL)
DS = (datasource) CTX. Lookup ("JDBC/BBS ");
If (DS = NULL)
Throw new exception ("Look up datasource failed! ");
} Catch (exception e ){
System. Out. println (E. getmessage ());
}
%>
<%
Connection conn = Ds. getconnection ();
Statement stmt = conn. createstatement ();
Resultset rs = stmt.exe cutequery ("select * from user ");
While (Rs. Next ()){
%>
<% = Rs. getint (1) %>
<% = Rs. getstring (2) %>
<%
}
Rs. Close ();
Stmt. Close ();
Conn. Close ();
%>
</Body>
</Html>
5. related error: <1> cannot create JDBC driver of class ''for connect URL 'null' Tomcat first finds the web. <resource-ref> in XML, and then find the server. <resource> in XML. If <Resource Name = "JDBC/testdb"> is not found or the name is incorrect, the error "cannot create JDBC driver of class ''' for connect URL 'null'" is returned. Note that the JDBC/BBS name in step 2 and 3 must be the same, and the URL Information in step 3 must be correct <2> cannot load JDBC Driver Class 'com. mySQL. JDBC. driver 'step 1