java經過數據庫鏈接池連接oracle

  開發工具:Eclipse J2EE 3.6java

  運行環境:jdk1.6web

  部署環境:Tomcat7sql

  數據庫鏈接池用的是dbcp,網上download下來的三個jar包。數據庫

  把數據庫鏈接池包和jdbc的包放到tomcat的lib下。apache

  新建web project,在META-INF下新建context.xml文件,放入代碼(一系列配置)。tomcat

<Context  path="Prj02" docBase="Prj02"
    debug="5" crossContext="true" reloadable="true"
    cachingAllowed="true" cacheMaxSize="20480"
    cacheTTL="10000">
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <Resource 
        name="jdbc/Prj02" 
        auth="Container" 
        type= "javax.sql.DataSource" 
        factory="org.apache.commons.dbcp.BasicDataSourceFactory"
        driverClassName="oracle.jdbc.driver.OracleDriver" 
        url="jdbc:oracle:thin:@localhost:1521:orcl"
        username="yff" 
        password="yff"
        maxActive="100" 
        maxIdle="30"
        maxWait="10000">
    </Resource>    
</Context>

  WebContent下新建index.jsp,放入代碼(jdbc鏈接,sql語句查詢)。oracle

  

<%@ page import="java.sql.*,javax.sql.*,javax.naming.*,org.apache.commons.dbcp.BasicDataSource,javax.rmi.PortableRemoteObject;" %>
<%
    Connection conn = null;
    Connection m_conn;     
    Statement m_stat;
    try{
        Context ctx = new InitialContext(); 
            Object obj = ctx.lookup("java:comp/env/jdbc/Prj02");
            DataSource ds = (DataSource)PortableRemoteObject.narrow(obj, DataSource.class);
            conn = ds.getConnection();
            if(conn!=null){
                m_conn = ds.getConnection();
                m_stat = m_conn.createStatement();
                ResultSet rs = m_stat.executeQuery("select * from TEST");
                
                while(rs.next()){
 
                    
                       out.print("id =" + rs.getInt("id") + ", name=" + rs.getString("name")+"\n"  );
            }
            }
       }catch (Exception e) {
        out.print(e.getMessage());
           e.printStackTrace();
      }finally {
        if(conn!=null)  conn.close();
      }
%>

  部署至Tomcat上。jsp

  (oracle內數據)工具

  部署以後的效果。開發工具

相關文章
相關標籤/搜索