科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道开源技术之Tomcat数据源配置总结

开源技术之Tomcat数据源配置总结

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

成功配置环境Tomcat5.0.28 MSSQLServer2000 MS JDBC Driver 一、 安装Tomcat5.0.28 二、 安装MS JDBC Driver 假设安装路径是F:\\green\\Microsoft SQL S

作者:中国IT实验室 来源:中国IT实验室 2007年9月24日

关键字:

  • 评论
  • 分享微博
  • 分享邮件

在本页阅读全文(共2页)

四、    如果你在Eclipse或JBuilder中开发的话,你需要在你的Web应用程序的WEB-INF\Web.xml文件中注册数据源,文件添加如下内容:
    <resource-ref>
        <res-ref-name>jdbc/northwind</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>
一定注意:同时检查一下你部署到Tomcat中对应的
彩色的加粗文字是添加上的,用来注册数据源的JNDI,在这我注册了两个数据源,一个是oracle的,一个是MSSQL  Server 2000的。

在做任何配置时最好不要修改Tomcat服务器的任何文件,如servel.xml或web.xml文件,而所有的操作和配置都可以在你自己的应用配置文件中来完成,这样即使培植错误也不至于服务器的崩溃。

按以上步骤就可以完成数据源的配置,你可以写一些程序来测试。
用JSP来测试,Index.jsp文件程序如下:

<%@ page language="java" import="java.util.*" %>
<%@ page import="javax.sql.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="javax.naming.*" %>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
out.println(basePath);
%>

<!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>
    This is my JSP page. <br> 

    <%         
 Context ctx=null;
   Connection cnn=null;
   java.sql.Statement stmt=null;
   ResultSet rs=null;
   try
   {
    ctx=new InitialContext();
    if(ctx==null)
     throw new Exception("initialize  the Context  failed");
    DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/northwind");
    out.println(ds);
    if(ds==null)
     throw new Exception("datasource  is  null");
     
     try{
        cnn=ds.getConnection();   
        out.println("<br> connection:"+cnn);
    }catch(Exception e){
        e.printStackTrace();
    }

   }
   finally
   {
    if(rs!=null)
     rs.close();
    if(stmt!=null)
     stmt.close();
    if(cnn!=null)
     cnn.close();
    if(ctx!=null)
     ctx.close();
   }

   
    %>
  </body>
</html>
在你的浏览器中运行http://10.0.0.168:8888/WebDemo/web/即可以看到结果:如下:
 
 
你看到连接成功的标志,就意味这你的数据源配置成功!!!

记住:要想配置成功,就要认真检查需要配置的每一个细节。

查看本文来源

    • 评论
    • 分享微博
    • 分享邮件