Context.lookup Analysis of JSP connection database exception

Source: Internet
Author: User
Tags auth

1. How database connections are accessed

JSP inside the link database operation as follows:

Context CTX = new InitialContext ();
DataSource ds = (DataSource) ctx.lookup ("Java:comp/env/jdbc/bookstore");
Connection conn = Ds.getconnection ();


Statement stmt = conn.createstatement (resultset.type_scroll_insensitive, resultset.concur_read_only);
ResultSet rs = stmt.executequery ("SELECT * from Guestbook ORDER BY gst_time Desc");

2. Exception Details

When you log on to a JSP page, an exception is thrown as follows:

type Exception Report

message javax.servlet.ServletException:org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC Driver of class ' for connect URL ' jdbc:mysql://localhost:3306/bookstore?autoreconnect=true '

Description The Server encountered an internal error, prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException:javax.servlet.ServletException: Org.apache.tomcat.dbcp.dbcp.SQLNestedException:Cannot Create JDBC driver of class ' for connect URL ' jdbc:mysql://local Host:3306/bookstore?autoreconnect=true ' Org.apache.jasper.servlet.JspServletWrapper.handleJspException ( jspservletwrapper.java:502) Org.apache.jasper.servlet.JspServletWrapper.service (jspservletwrapper.java:412) Org.apache.jasper.servlet.JspServlet.serviceJspFile (jspservlet.java:313) Org.apache.jasper.servlet.JspServlet.service (jspservlet.java:260) Javax.servlet.http.HttpServlet.service ( httpservlet.java:723)

3. Locate the problem by using the exception information
By viewing the exception information, you find that the driver class is empty, stating that the driver class is not configured
To view the Server.xml file, configure the following:
<context path= "/ch12" docbase= "I:\Develop\Code\Chapter12" reloadable= "true" >
<resource name= "Jdbc/bookstore" auth= "Container" type= "Javax.sql.DataSource"
Maxactive= "maxidle=" maxwait= "10000"
Username= "root" password= "root"
dirverclassname= "Com.mysql.jdbc.Driver"
Url= "Jdbc:mysql://localhost:3306/bookstore?autoreconnect=true"/>
</Context>
By observation, it should be "Driverclassname" but mistakenly written "dirverClassName"
After correcting driverclassname, restart tomcat,jsp webpage access is OK

4.context.lookup parameter Description
DataSource ds = (DataSource) ctx.lookup ("Java:comp/env/jdbc/bookstore");
Description: Java:comp/env is a fixed parameter
Jdbc/bookstore corresponds to two places:
One is: Resource name in Server.xml
Another: Resource-ref res-ref-name in Web. xml

Detailed configuration: Server.xml
(See the 3rd step above for abnormal positioning)
<context path= "/ch12" docbase= "I:\Develop\Code\Chapter12" reloadable= "true" >
<resource name= "Jdbc/bookstore" auth= "Container" type= "Javax.sql.DataSource"
Maxactive= "maxidle=" maxwait= "10000"
Username= "root" password= "root"
dirverclassname= "Com.mysql.jdbc.Driver"
Url= "Jdbc:mysql://localhost:3306/bookstore?autoreconnect=true"/>
</Context>

Detailed configuration: Web. xml

<resource-ref>

<description>mysql JDBC datasource</description>

<res-ref-name>jdbc/bookstore</res-ref-name>

<res-type>javax.sql.DataSource</res-type>

<res-auth>Container</res-auth>

</resource-ref>

The program can be replaced by:

InitialContext context=new InitialContext ();

Context Evncontext = (context) initcontext.lookup ("java:/comp/env");

DataSource DataSource = (DataSource) evncontext.lookup ("Jdbc/bookstore");

Connection con = datasource.getconnection ();

5. Summary

1) Master the exception information thrown through the page, quickly locate the problem, solve the problem

2) How to configure and use the database connection mode of JSP

  

Related Article

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.