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