通过Tomcat提供的界面 配置数据连接池比直接配置xml文件方便、安全的多
试验环境:Tomcat 5.0.28+Oracle10.1
1.将已经建立好的工程拷贝到/tomcat 5.0/webapps中启动tomcat
2.http://localhost:8080/admin登陆后
点击左边框架中Service菜单项左边的小圆,展开列表.
点击左边框架中Host (localhost)菜单项左边的小圆,展开列表
点击左边框架中相应的工程项左边的小圆,展开列表
点击左边框架中Data Sources蔡单项,右边框架出现设置页。
从右边框架中右上角的Data Source Actions下拉列表中选择create new data source
从右边框架中右上角的Data Source Actions下拉列表中选择create new data source
JNDI Name: | jdbc/MyOracle |
Data Source URL: | jdbc:oracle:thin:@219.242.112.37:1521:thunder |
JDBC Driver Class: | oracle.jdbc.driver.OracleDriver |
User Name: | bjtu |
Password: | bjtu |
点击右边框架中右上角save按钮 .
点击顶部框架Commit Changes按钮。
在这里不需要其它配置就可以了。
3.将D:/oracle/product/10.1.0/Db_1/jdbc/lib该目录下的classes12.jar拷贝到D:/Program Files/Tomcat 5.0/common/lib目录下
4.测试页面
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.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">
-->
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.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>
<%
DataSource ds = null;
ResultSet res = null;
Statement stmt = null;
Connection conn = null;
try{
InitialContext ctx=new InitialContext();
<body>
<%
DataSource ds = null;
ResultSet res = null;
Statement stmt = null;
Connection conn = null;
try{
InitialContext ctx=new InitialContext();
ds=(DataSource)ctx.lookup("java:comp/env/jdbc/thunder");
conn = ds.getConnection();
stmt = conn.createStatement();
String sql ="SELECT * FROM STUDENT";
res = stmt.executeQuery(sql);
System.out.println("11111111111111");
while(res.next()){
out.println(res.getString(1));
out.println(res.getString(2));
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(res != null){
res.close();
}
if(stmt != null){
stmt.close();
}
if(conn != null){
conn.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
%>
</body>
</html>
经测试OK
String sql ="SELECT * FROM STUDENT";
res = stmt.executeQuery(sql);
System.out.println("11111111111111");
while(res.next()){
out.println(res.getString(1));
out.println(res.getString(2));
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(res != null){
res.close();
}
if(stmt != null){
stmt.close();
}
if(conn != null){
conn.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
%>
</body>
</html>
经测试OK