JNDI访问连接池

1.利用tomcat的amdin管理界面配置连接池

2.将conf/server.xml文件中生存的连接池部分
<Resource
      name="jdbc/drp"
      type="javax.sql.DataSource"
      maxActive="4"
      maxIdle="2"
      username="drp1"
      maxWait="5000"
      driverClassName="oracle.jdbc.driver.OracleDriver"
      password="drp1"
      url="jdbc:oracle:thin:@localhost:1521:HUGUI"/>

剪切到conf/context.xml文件中



3。JNDI访问连接池
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
    <%@page import="javax.naming.*" %>
    <%@page import="javax.sql.*" %>
    <%@page import="java.sql.*" %>
<%
 Connection conn = null;
 PreparedStatement pstmt = null;
 ResultSet rs = null;
 try{
  Context ctx = new InitialContext();
 //通过JNDI查找DataSource
 DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/drp");
 conn = ds.getConnection();
 pstmt = conn.prepareStatement("select * from t_user");
 rs = pstmt.executeQuery();
System.out.println("ffff");
 while(rs.next()){
  out.println(rs.getString("user_id") + ", " + rs.getString("user_name"));
 }
 }catch(Exception e){
  e.printStackTrace();
 }finally{
  if(rs != null){
   rs.close();
  }
  if(pstmt != null){
   pstmt.close();
  }
  if(conn != null){
  //将Connecting放到连接池
   conn.close();
  }
 }
%>

你可能感兴趣的:(java,sql,tomcat,xml,SQL Server)