1.安装tomcat6.0
2. Tomcat 6.0的 conf\Catalina\localhost 目录下xx.xml文件内容。
xx名称与工程名一样。
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@111.11.11.111:1521:openet"
username="newdao"
password="123"
maxActive="20"
maxIdle="3"
removeAbandoned="true"
maxWait="3000" />
3.拷贝ojdbc14.jar 到 tomcat的lib下
oracle8i对应的为class12.jar,oracle9i为ojdbc14.jar
4.测试
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ page import="java.util.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="javax.sql.*" %>
<%@ page import="javax.naming.*" %>
My JSP 'index.jsp' starting page This is my JSP page.
<%
DataSource ds = null;
try
{
InitialContext ctx = new InitialContext();
ds = (DataSource)ctx.lookup("java:comp/env/jdbc/openetdb");
Connection conn = ds.getConnection();
String strSql = "SELECT field from TABLE " ;
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(strSql);
while(rs.next())
{
out.println("查出:"+rs.getObject("field"));
}
rs.close();
stmt.close();
conn.close();
}catch(Exception ex)
{
ex.printStackTrace();
out.println(ex);
}
%>
另外通过研究还有一种方法配置数据库 以oracle为例:
1.配置%TOMCAT_HOME%\conf\server.xml
global="jdbc/openetdb"
auth="Container"
type="javax.sql.DataSource"
password="111"
driverClassName="oracle.jdbc.driver.OracleDriver"
maxIdle="2"
maxWait="5000"
username="abc"
url="jdbc:oracle:thin:@111.11.11.111:1521:SID"
maxActive="4"/>
2.然后,在ROOT\WEB-INF\web.xml中和之间添加
DataSource
jdbc/openetdb
javax.sql.DataSource
Container
3.最后在%TOMCAT_HOME%\conf\content.xml里面配置当前应用程序的连接:
name="jdbc/openetdb"
type="javax.sql.DataSource"
global="jdbc/openetdb"/>
Tomcat 5 maintains a separate namespace of global resources for the
entire server. These are configured in the
element of
$CATALINA_HOME/conf/server.xml
. You may expose these resources to
web applications by using
elements.
阅读(3668) | 评论(0) | 转发(0) |