tomcat配置数据源

1、修改tomcat安装目录下的conf文件夹里context.xml文件。注意是在<Context></Context>中。

<Resource name="jdbc/TestDB"
auth="Container"
type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="1000"
username="scott" password="tiger" driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@127.0.0.1:1521:orcl" />

 

2. 在Tomcat安装目录下的webapps里创建工程SQLtest. (在eclipse中创建项目)

3. 将ROOT下的WEB-INF文件夹整个拷贝至SQLtest中.

4. 修改修改SQLtest\WEB-INF中web.xml,在<web-app></web-app>中添加如下

    <resource-ref>
        <description>popuserDataSource</description>
        <res-ref-name>jdbc/TestDB</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>

此处<res-ref-name>内容应与context.xml中Resource name一致。

 5、在WEB-INF中创建lib文件夹,并将sqlserver三个驱动msbase.jar/mssqlserver.jar/msutil.jar(oracle只需要:classes12.jar)拷贝 其中。(2005的只需将sqlserver.jar拷贝就行)

 

6、 SQLtest下编写jsp测试连接池是否成功,在SQLTest文件夹下创建index.jsp文件.输入内容如下:(注意修改数据库名字.)

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ 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+"/";
%>

<!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">
 -->
  </head>
 
  <body>
    <%out.println("<h1>Hello,test JNDI ! </h1>");%>
<%
Context ctx = new InitialContext();
Context envctx = (Context) ctx.lookup("java:comp/env");
DataSource ds = (DataSource) envctx.lookup("jdbc/TestDB");
Connection conn=ds.getConnection();
Statement st=conn.createStatement();
String sql="select * from emp";
ResultSet rs=st.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString("ename")%>
您的第二个字段内容为:<%=rs.getString("sal")%>
<br>
<%}%>
<%out.print("使用jdbc驱动操作数据库操作成功,恭喜你");%>
<%rs.close();
st.close();
conn.close();
%>

  </body>
</html>



 

 

posted @ 2011-11-16 16:09  合肥房屋托管  阅读(290)  评论(0编辑  收藏  举报