1、编写context.xml 文件复制到 WebRoot/META_INF/context.xml
<?xml version=‘1.0‘ encoding=‘utf-8‘?> <Context> <WatchedResource>WEB-INF/web.xml</WatchedResource> <Resource name="jdbc/oracle" auth="Container" type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver" url="jdbc:oracle:thin:@localhost:1521:orcl" username="system" password="FengZe2012" maxActive="1000" maxIdle="30" maxWait="10000" /> </Context>
2、复制相关的数据库jar文件到 WebRoot/WEB-INF/lib/目录下
3、 使用连接代码
<%@page import="java.sql.PreparedStatement"%> <%@ page language="java" pageEncoding="utf-8"%> <%@ page import="javax.naming.InitialContext"%> <%@ page import="javax.naming.Context"%> <%@ page import="javax.sql.DataSource"%> <%@ page import="java.sql.Connection"%> <% Context ctx = new InitialContext(); DataSource ds = (DataSource)ctx.lookup("java:/comp/env/jdbc/oracle"); Connection conn = ds.getConnection(); PreparedStatement pst = conn.prepareStatement("create table t(t number)"); pst.execute(); pst.close(); conn.close(); out.print(conn); %>
时间: 2024-10-18 09:42:59