Tomcat configures the data source to access the MySQL database through JNDI

Source: Internet
Author: User

 

<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

 

 

 

 

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.