首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

jndi示范演示

2012-08-03 
jndi示例演示1、找到Tomcat安装目录下的conf文件夹中的context.xmlContextWatchedResourceWEB-INF/web.

jndi示例演示

1、找到Tomcat安装目录下的conf文件夹中的context.xml

<Context>

 

      <WatchedResource>WEB-INF/web.xml</WatchedResource>

   <Resource name="jdbc/news"

                      auth="Container"

                      type="javax.sql.DataSource"  maxActive="100" 

                      maxIdle="30"

                      maxWait="10000"

                      username="tom"

                      password="tom"

                      driverClassName="oracle.jdbc.driver.OracleDriver" 

                   url="jdbc:oracle:thin:@localhost:1521:accp" />

</Context>

 

 

2、在项目的web.xml文件中加入以下代码:

<resource-ref>

              <res-ref-name>jdbc/news</res-ref-name>

              <res-type>javax.sql.DataSource</res-type>

              <res-auth>Container</res-auth>

       </resource-ref>

 

3、在Demo.java文件中加入以下代码:

 

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.sql.DataSource;

 

publicclass Demo {

   Connection conn = null;

   PreparedStatement ps = null;

   ResultSet rs = null;

   publicvoid getJndi() {

     try {

        Context ic = new InitialContext();

        DataSource source = (DataSource) ic

             .lookup("java:comp/env/jdbc/news");

        Connection connection = source.getConnection();

        System.out.println("OK");

        ps = connection.prepareStatement("SELECT * FROM BS_USERINFO ");

        ResultSet results = ps.executeQuery();

        while(results.next()){

           System.out.println(results.getString(1));

        }

     } catch (Exception e) {

        e.printStackTrace();

     }

   }

}

 

 

 

4、 在index.jsp中加入以下代码:

<%

     Demo demo = new Demo();

     demo.getJndi();

   %>

 

5、在工程下加入oracle驱动包、如果有问题,把jar包放到tomcat的bin目录





1楼dianzikuangjie前天 11:18
ssss

热点排行