1.在tomcat--->conf---->context 中写入:
<Resource name="jdbc/news"
auth="Container"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="-1"
username="scott"
password="tiger"
driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:@localhost:1521:orcl" />
2.在web项目下的WEB-INF下的l中写入:
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/news</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
3.建一个test.jsp文件进行测试:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*,javax.naming.*,javax.sql.*" %>
<%
String path = ContextPath();
String basePath = Scheme()+"://"+ServerName()+":"+ServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'test.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
out.print("使用连接池Oracle数据库成功!!!<br/>");
Context ctx = null;
DataSource ds = null;
Statement stmt = null;
ResultSet rs = null;
Connection con = null;
ResultSetMetaData md = null;
try{
ctx = new InitialContext();
ds = (DataSource)ctx.lookup("java:comp/env/jdbc/news");
con = ds.getConnection();
String sql = "select * from emp";
Statement st = ateStatement();
rs = st.executeQuery(sql);
()){
out.Int("empno")+"----");
out.String("ename")+"----");
out.Int("sal")+"<br/>");
}
}catch(Exception e){
out.print(e);
}finally{
jdbc连接oracle if(rs != null)
rs.close();
if(stmt != null)
stmt.close();
if(con != null)
con.close();
if(ctx != null)
ctx.close();
}
%>
</body>
</html>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论