The first step: Configure Web. xml
<resource-ref> <description>db connection</description><res-ref-name>test_datasource< /res-ref-name><res-type>javax.sql.datasource</res-type><res-auth>container</res-auth ></resource-ref>
Step Two: Configure the Server.xml for Tomcat (the following Oracle, for example, other DB equivalent)
<context docbase= "E:\dev\apps\apache-tomcat-7.0.42\webapps\Test" path= "/test" reloadable= "true" source= " Org.eclipse.jst.jee.server:Test "><resource name=" Test_datasource "auth=" Container "type=" Javax.sql.Da Tasource "Driverclassname=" Oracle.jdbc.driver.OracleDriver "url=" Jdbc:oracle:thin: @IP: Port: Database name "Username=" with User name "password=" password "maxactive=" maxidle= "2" maxwait= "1"/></context>
Step three: Paste the sample code for my Project Jndi section
static datasource getdatasource (String dsName) { DataSource ds = null; try { if (Serverdetector.istomcat ()) { dsName = "java:comp/env/" + dsName; } ds = (DataSource) Jndiutil.getinitialcontext (). Lookup (DSName); } catch (namingexception e) { throw New dataaccessexception (e); } if (null == ds ) { throw new dataaccessexception ("data source[" + dsName + "] is null"); } return ds; }
Precautions:
1, the above data source name Test_datasource must be consistent!
2, Server.xml in the configuration of the content, will be myeclipse to perform clean operation of the time to enforce the operation of restoring server.xml content, be sure to remember to reconfigure!
This article is from the "You can choose Extraordinary" blog, please be sure to keep this source http://ylcodes01.blog.51cto.com/5607366/1916643
Configuration of Jndi in Java Web Engineering