Get a database connection when you start a service

Source: Internet
Author: User

Take TOMCAT6 configuration as an example

The first step is to add the following resource definition tags under Tomcate/conf/context, including Jndi naming, authorization, type, database driver class, database connection path, account number, password, maximum number of activities, minimum idle number, maximum number of waits, etc.

<resource name= "Jdbc/mydb"       auth= "Container"       type= "Javax.sql.DataSource"       driverclassname= " Oracle.jdbc.driver.OracleDriver "       url=" Jdbc:oracle:thin: @localhost: 1521:mydb "       username=" Bmgis "       Password= "Bmgis"       maxactive= "       maxidle=", "maxwait=" 10000 "/>"       


Second, the Jndi name must be consistent with the previously defined Jndi name in the location specified by the configuration resource definition under Web. xml!

<resource-ref>    <description>db connection</description>    <res-ref-name>jdbc/mydb </res-ref-name>    <res-type>javax.sql.DataSource</res-type>    <res-auth>container </res-auth></resource-ref>


The third step, call get

Create Context Initcontext = new InitialContext (); Get data source DataSource ds = (DataSource) envcontext.lookup ("java:/comp/env/ Jdbc/mydb "); finally get the database connection Connection conn = Ds.getconnection (); Note: tomcat5.x, the configuration data source way and TOMCAT6 some different!



Get a database connection when you start a service

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.