1.在WEB-INF下新建文件ibm-web-bnd.xmi(里面包括JNDI的引用就是在WEBSPHERE里配置的数据源名称里的JNDI名称)
<?xml version="1.0" encoding="UTF-8"?>
<com.ibm.ejs.models.base.bindings.webappbnd:WebAppBinding xmi:version="2.0" xmlns:xmi=" http://www.omg.org/XMI" xmlns:com.ibm.ejs.models.base.bindings.webappbnd="webappbnd.xmi" xmi:id="WebAppBinding_1216699055547" virtualHostName="default_host">
<webapp href="WEB-INF/web.xml#WebApp_1216699055562"/>
<resRefBindings xmi:id="ResourceRefBinding_1216699055547" jndiName="CIS_DS">
<bindingResourceRef href="WEB-INF/web.xml#ResourceRef_1216699055547"/>
</resRefBindings>
</com.ibm.ejs.models.base.bindings.webappbnd:WebAppBinding>
2.在WEB-INF下的web.xml文件中添加对jndi的引用(res-ref-name名称一般跟数据源名称相同名CIS_DS)
<resource-ref id="ResourceRef_1216699055547">
<description></description>
<res-ref-name>CIS_DS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
3.在代码中使用java:comp/env/CIS_DS(因为在web.xml文件里把JNDI已经映射为CIS_DS,所以就可以用java:comp/env/CIS_DS访问)就可以连接数据库
Context ctx = new InitialContext();
Object obj = ctx.lookup("java:comp/env/CIS_DS");
ds = (DataSource) obj;
conn = ds.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from c_jgb where rownum<10");
<?xml version="1.0" encoding="UTF-8"?>
<com.ibm.ejs.models.base.bindings.webappbnd:WebAppBinding xmi:version="2.0" xmlns:xmi=" http://www.omg.org/XMI" xmlns:com.ibm.ejs.models.base.bindings.webappbnd="webappbnd.xmi" xmi:id="WebAppBinding_1216699055547" virtualHostName="default_host">
<webapp href="WEB-INF/web.xml#WebApp_1216699055562"/>
<resRefBindings xmi:id="ResourceRefBinding_1216699055547" jndiName="CIS_DS">
<bindingResourceRef href="WEB-INF/web.xml#ResourceRef_1216699055547"/>
</resRefBindings>
</com.ibm.ejs.models.base.bindings.webappbnd:WebAppBinding>
2.在WEB-INF下的web.xml文件中添加对jndi的引用(res-ref-name名称一般跟数据源名称相同名CIS_DS)
<resource-ref id="ResourceRef_1216699055547">
<description></description>
<res-ref-name>CIS_DS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
3.在代码中使用java:comp/env/CIS_DS(因为在web.xml文件里把JNDI已经映射为CIS_DS,所以就可以用java:comp/env/CIS_DS访问)就可以连接数据库
Context ctx = new InitialContext();
Object obj = ctx.lookup("java:comp/env/CIS_DS");
ds = (DataSource) obj;
conn = ds.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from c_jgb where rownum<10");