通过JNDI连接数据库

http://download.csdn.net/download/tonmingjun/5975661 如果需要代码请去我所在的csdn下载,也能给我一些分。

jndi的连接方式要比jdbc的好是因为有地址池是的连接可以更大化,但是实际上因为现在服务器的硬件支撑其实直接用jdbc就行了,我公司就是这样的

//index.jsp

 

<%@ page language="java" import="java.util.*,java.sql.*,javax.naming.*,javax.sql.*" pageEncoding="ISO-8859-1"%>

<%

/*
* @author Evangelion
* @date
* @version V 1.0
*/

Connection conn = null;

PreparedStatement psmt=null;

ResultSet rs = null;

DataSource ds=null;

String url;

try{

Context ctx=new InitialContext();//上下文对象 初始化

ds=(DataSource)ctx.lookup("java:comp/env/jdbc/AiomniDB");//反回绑定的对象,looup后括号内容和 tomcat文件下的 context.xml里面设置的名字要一致

conn=ds.getConnection();

String sql="select * from flex_data where type='mx_data201101'";

psmt=conn.prepareStatement(sql);

rs=psmt.executeQuery();

out.println("<?xml version='1.0' encoding='utf-8'?><root>");

while (rs.next())

{

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

}

out.println("</root>");

rs.close();

psmt.close();

conn.close();

}

catch(Exception e)

{

e.printStackTrace();

}

finally

{

   rs.close();

   psmt.close();

   conn.close();

}

%>

//tomcat下的context.xml

<!-- The contents of this file will be loaded for each web application -->

<Context>

    <!-- Default set of monitored resources -->

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

    <!-- Uncomment this to disable session persistence across Tomcat restarts -->

    <!--

    <Manager pathname="" />

    -->

    <!-- Uncomment this to enable Comet connection tacking (provides events

         on session expiration as well as webapp lifecycle) -->

    <!--

    <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />

    -->

<Resource name="jdbc/AiomniDB" auth="Container" type="javax.sql.DataSource" username=" " password=" "  //请填写你数据库的相应数据   driverClassName="oracle.jdbc.driver.OracleDriver" url="jdbc:oracle:thin:@localhost:orcl" maxActive="100" maxIdle="2" maxWait="500" removeAbandoned="true" removeAbandonedTimeout="360000" logAbandoned="true" /> 

  <Resource name="jdbc/touch" auth="Container" type="javax.sql.DataSource" username=" " password=" " driverClassName="oracle.jdbc.driver.OracleDriver" url="jdbc:oracle:thin:@localhost:orcl" maxActive="100" maxIdle="2" maxWait="500" removeAbandoned="true" removeAbandonedTimeout="360000" logAbandoned="true" /> 

</Context>

//web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5" 

xmlns="http://java.sun.com/xml/ns/javaee" 

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

  <welcome-file-list>//默认打开的页面

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

  <resource-ref>

  <description>JNDI DataSource</description>

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

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

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

  </resource-ref>

</web-app>

posted @ 2013-10-19 21:25  Evangelion  阅读(223)  评论(0编辑  收藏  举报