首先确定你要修改的连接是否能正常访问,然后,jsp项目,除了修改html文件,不用重启tomcat,修改其他文件,都需要重启tomcat的。
tomcat仅是一个服务,跟你连的上连不上数据库没有关系. 看下你项目中数据库的路径\参数是否都正确。
1.安装tomcat6.02. Tomcat 6.0的 conf\Catalina\localhost 目录下xx.xml文件内容。
xx名称与工程名一样。
<Context debug="1" docBase="C:\Tomcat 6.0\webapps\myweb" path="/myweb" reloadable="true" workDir="work\Catalina\localhost\myweb">
<Resource name="jdbc/openetdb" auth="Container"
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" />
</Context>
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.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<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>
This is my JSP page. <br>
<%
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)
}
%>
</body>
</html>
另外通过研究还有一种方法配置数据库 以oracle为例:
1.配置%TOMCAT_HOME%\conf\server.xml
<GlobalNamingResources>
<Resource name="jdbc/openetdb"
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"/>
</GlobalNamingResources>
2.然后,在ROOT\WEB-INF\web.xml中<web-app>和</web-app>之间添加
<resource-ref>
<description>DataSource</description>
<res-ref-name>jdbc/openetdb</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
3.最后在%TOMCAT_HOME%\conf\content.xml里面配置当前应用程序的连接:
<ResourceLink
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 <GlobalNameingResources>element of $CATALINA_HOME/conf/server.xml. You may expose these resources to web applications by using <ResourceLink>elements.
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)