Data source connection Database configuration related XML file

Source: Internet
Author: User
Tags auth tomcat server

After learning the data source connection data, make a note, when our program access to the database frequently, in order to improve the efficiency of the program, we can

The data source connects to the database, obtains the database connection object from the database connection pool directly out of the idle state, and the following is the configuration of the associated XML file:

1, define the data source.

The data source is one of the Jndi (Java naming and Directory Interface) resources, starting with defining the data source

Create a context.xml file under the Meta-inf directory under the corresponding Javaweb project,

The following is a description of the contents of the Context.xml file and its properties.

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Context>3 4 <!--defining a data source -5 6 <!-- 7 Name: Specifies the Jndi name of the resource8 Auth: Specifies the manager that manages resource, has two optional values, container represents the container to create and manage resource9 application represents a web app to create and manage resourceTen Type: Specifies the Java class name to which resource belongs One      A Username: Database user name password: password to connect to database - driverclassname: Specifies the name of the driver implementation class in the JDBC drive that connects to the database - URL: Specify the URL of the database the  - Note: If Usessl=true/false is used in the URL, you need to add "amp;" after all ' & '. as follows -   - -     <Resourcename= "JDBC/BOOKDB"Auth= "Container"type= "Javax.sql.DataSource" + username= "root"  - Password= "Cz123" + Driverclassname= "Com.mysql.jdbc.Driver" A URL= "Jdbc:mysql://localhost:3306/books?characterencoding=utf8&amp;usessl=true"/> at  - </Context>

1. Declaring a reference to a Jndi resource

If the Web app accesses a Jndi resource managed by the servlet container, you must declare a reference to the Jndi resource in the registry of the. xml file. Create the Web. xml file under the Web-inf directory under the project

The contents and properties are described as follows:

1 <?XML version= "1.0" encoding= "UTF-8"?>2 3 <Web-appxmlns= "Http://xmlns.jcp.org/xml/ns/javaee"4 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"5 xsi:schemalocation= "Http://xmlns.jcp.org/xml/ns/javaee6 http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd "7 version= "3.1"8 Metadata-complete= "true">9 Ten   <Display-name>Tomcat Manager Application</Display-name> One   <Description> A A scriptable Management Web application for the Tomcat Web Server; - Manager lets you view, load/unload/etc particular Web applications. -   </Description> the    -    <!--declaring a reference data source - -   <!--  - <res-ref-name>: Specifies the jndi name of the referenced resource, corresponding to the name attribute in the <Resource> element of the defined data source + <res-type>: Specifies the class name of the referenced resource, corresponding to the name attribute in the <Resource> element of the defined data source - <res-auth>: Specifies the manager that manages the referencing resource, corresponding to the Auth attribute in the <Resource> element of the defined data source +     - A   <Resource-ref> at       <Description>DB Connection</Description> -       <Res-ref-name>Jdbc/bookdb</Res-ref-name> -       <Res-type>Javax.sql.DataSource</Res-type> -       <Res-auth>Container</Res-auth> -   </Resource-ref> -  in </Web-app>

Get database connection: Javax.naming.Context CTX = InitialContext (); Get Context Object

Call the lookup method of the context object to get the data source object by referencing the name of the resource

Because the author uses the Tomcat server, all to add java:comp/env (fixed format) in front of name

Javax.sql.DataSource ds = Ctx.lookup ("java:comp/env/jdbc/bookdb");

Get a database connection object from a data source

Connection con = ds.getconnection ();

Data source connection Database configuration related XML file

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.