1:context.xml
<Resource name="jdbcoracle" auth="Container" type="javax.sql.DataSource" factory="org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory" maxActive="20" maxIdle="1" maxWait="100" username="yshy" password="yshy" driverClassName="oracle.jdbc.OracleDriver" url="jdbc:oracle:thin:@localhost:1521:XE"/>
2:web.xml
<resource-ref> <description>Oracle 数据库连接池</description> <res-ref-name>jdbcoracle</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>
3:TestServlet.java
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { InitialContext ctx = new InitialContext(); DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbcoracle"); Connection conn = ds.getConnection(); out.print("数据源测试联接成功:"+conn); } catch (NamingException | SQLException ex) { Logger.getLogger(TestServlet.class.getName()).log(Level.SEVERE, null, ex); } }
时间: 2024-10-12 13:56:15