Configure the JNDI data source for Tomcat

Source: Internet
Author: User

Database version-oracle11g

Tomcat version -- tomc7.0

Ide version -- Eclipse Java ee ide for Web Developers.

Step 1:

Place the database Driver (jar package) in the project's WebContent/WEB-INF/lib folder

Step 2:

Create a context. xml file under the project's WebContent/META-INF directory with the following content:

              

Note:
The configuration items of the Tomcat standard data source resource factory are as follows:
* DriverClassName-the full name of the JDBC driver class used.
* MaxActive-Maximum number of active instances that can be allocated from the database connection pool at the same time.
* MaxIdle-Maximum number of inactive connections in the database connection pool at the same time.
* MaxWait-when no connection is available in the connection pool, the maximum wait time (in milliseconds) that the connection pool will wait before an exception is thrown.
* Password-the password sent to the JDBC driver.
* Url-the connection URL sent to the JDBC driver.
* User-the user Name of the database that is sent to the JDBC driver.
* ValidationQuery-an SQL query statement used for connection pool verification before the connection is returned to the application.
* If this attribute is specified, the SQL SELECT statement for at least one row of records must be returned.


Jdbc/test is the name of the data source (can be written at will ),
Other parameters are modified according to your actual situation, such as the database name, account, and password.

Step 3:

Write a jsp file and give it a try.

<% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %> <% @ page = "java. SQL. *, javax. SQL. *, javax. naming. * "%> <% Connection conn = Context ctx = DataSource ds = (DataSource) ctx. lookup ("java: comp/env/jdbc/wl" conn = System. out. println ("connection successful !! "}}%>

Result: The connection is successful !!

========================================= Common Errors ==================== ==========

Error message: cannot load JDBC driver class (the driver class cannot be loaded)

Error cause and solution: access the database through the database source, the database is created by Tomcat, The JDBC driver JAR file should be copied to the project's WebContent/WEB-INF/lib folder, or tomcat lib directory. (Refer to step 1)

 

Error message: Cannot create JDBC driver of class ''for connect URL 'null' (the driver Cannot be created because the connection path is null)

Cause of error and solution: these errors often occur when they occur with the database driver or when a connection error occurs. The solution is to check the context. make sure that the configuration information is correct. (Refer to step 2)

 

Error message: Name w is not bound in this Context (the jdbc Name is not bound to the Context)

Error cause and solution:

1. When you use the lookup () method to find the data source, if the data source name is inconsistent with the name in the configuration, this error occurs when the program is running. The solution is to ensure that the data source name specified in the context. xml file is consistent with the name of the data source to be accessed in the code.

2. When the lookup () method is used in the program to find the data source, the prefix (java: comp/env/) + data source name is not used. This error is reported when the program is running. The solution is to use the prefix (java: comp/env/) + Data Source Name When lookup () is used to find the data source.

 

 

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.