我的Tomcat是5.0.28的jdk是5.0的今天试了n次都不成功啊!急死我了

解决方案 »

  1.   

    1。
    web.xml和%TOMCAT_HOME%\conf\Catalina\localhost下对应你的引用的配置文件修改通过文件夹导航到%TOMCAT_HOME%\conf,打开web.xml,在</web-app>的前面添加以下内容:      <resource-ref>    <description>DB Connection</description>    <res-ref-name>jdbc/mysql</res-ref-name>    <res-type>javax.sql.DataSource</res-type>    <res-auth>Container</res-auth>      </resource-ref>    注意res-ref-name填写的内容要与在上文提到的JNDI Name名称一致。 通过文件夹导航到%TOMCAT_HOME%\conf\Catalina\localhost下,找到你的web应用对应的.xml文件,如    ROOT.xml,并在此文件的下添入代码:<ResourceLink name="jdbc/mysql" global="jdbc/mysql" type="javax.sql.DataSourcer"/>
    2。
    编写测试代码在应用的目录下建立一个Test.jsp文件,代码如下:<!doctype html public "-//w3c//dtd html 4.0 transitional//en" "http://www.w3.org/TR/REC-html40/strict.dtd"><%@ page import="java.sql.*"%><%@ page import="javax.sql.*"%><%@ page import="javax.naming.*"%><%@ page session="false" %><html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><title></title><%    out.print("我的测试开始");   DataSource ds = null;   try{   InitialContext ctx=new InitialContext();   ds=(DataSource)ctx.lookup("java:comp/env/jdbc/mysql");   Connection conn = ds.getConnection();   Statement stmt = conn.createStatement();     //提示:users必须是数据库已有的表,//这里的数据库前文提及的Data Source URL配置里包含的数据库。   String strSql = " select * from users";   ResultSet rs = stmt.executeQuery(strSql);   while(rs.next()){      out.print(rs.getString(1));                      }out.print("我的测试结束");   }   catch(Exception ex){       out.print(“出现例外,信息是:”+ex.getMessage());    ex.printStackTrace();   }%></head><body></body></html>
      

  2.   

    数据源定义不要写在server.xml里面,否则又要写<resource-ref>,又要写<ResourceLink,麻烦死了。直接把数据源写在你的web应用对应的.xml文件,(通过文件夹导航到%TOMCAT_HOME%\conf\Catalina\localhost下,找到你的web应用对应的.xml文件,如    ROOT.xml)这样又简单,又不容易出错。我也是搞了很久,最后用这种方法搞定的