tomcat怎么连接oracle数据库配置文件

tomcat怎么连接oracle数据库配置文件,第1张

在tomcat 6.0.中配置数据源连接Oracle数据库方法.

连接其他数据库也是一样.只是改下driverClassName 和Url就行了.只是需要不同的驱动程序的jar包,拷贝到apache-tomcat-6.0.29\lib中。

1.安装tomcat6.0

2. 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.


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/9418585.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-28
下一篇 2023-04-28

发表评论

登录后才能评论

评论列表(0条)

保存