类似这样1>\Tomcat 5.5\conf\server.xml2>\Tomcat 5.5\conf\context.xml3>项目中\WebRoot\WEB-INF\web.xml配置的代码1> 在tomcat\conf\server.xml的GlobalNamingResources中增加:
<Resource auth="Container"
     description="DB Connection"
     driverClass="com.mysql.jdbc.Driver"
     maxPoolSize="10"
     minPoolSize="2"
     acquireIncrement="2"
     name="jdbc/mysqlDB"
     user="root"
     password="123123"
     factory="org.apache.naming.factory.BeanFactory"
     type="com.mchange.v2.c3p0.ComboPooledDataSource"
     jdbcUrl="jdbc:mysql://localhost:3306/mldn?autoReconnect=true" />2> 在tomcat\conf\context.xml的Context中增加:  <ResourceLink name="jdbc/mysqlDB" global="jdbc/mysqlDB" type="javax.sql.DataSource"/>3> 在web.xml中增加:<resource-ref>
     <description>DB Connection</description>
     <res-ref-name>jdbc/mysqlDB</res-ref-name>
     <res-type>javax.sql.DataSource</res-type>
     <res-auth>Container</res-auth>
</resource-ref>(3)jsp测试页面<%@page language="java" import="java.util.*,java.sql.*,javax.naming.*,javax.sql.*" pageEncoding="GB2312"%>
<%@page import="com.mchange.v2.c3p0.*"%>
<%@page import="java.sql.Connection"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>    
    <title>My JSP 'mysqlTest.jsp' starting page</title>
</head><body>
    Mysql数据库测试<br><br><br>
    <%
    Connection conn=null;
      try
{
      InitialContext ctx = new InitialContext();
      DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/mysqlDB");
      conn=ds.getConnection();
}
catch(NamingException ex)
{
   ex.printStackTrace();
}    String sql="select * from user";
PreparedStatement ps=conn.prepareStatement(sql);
ResultSet rs=ps.executeQuery();
    while(rs.next())
{
   %>
   字段1:<%=rs.getString(1)%> 字段2:<%=rs.getString(2)%><br>
   <%
}
    if(rs!=null)
    {
        rs.close();
        rs=null;
    }
    if(ps!=null)
    {
        ps.close();
        ps=null;
    }
    if(conn!=null)
    {
        conn.close();
        conn=null;
    }
    %>
</body>
</html>

解决方案 »

  1.   

    在spring配置文件中这样<bean id="datasource"
            class="org.springframework.jndi.JndiObjectFactoryBean">
            <property name="jndiName">
                <value>java:comp/env/jdbc/cms</value>
            </property>
        </bean>
      

  2.   

    然后在程序里面getBean("datasource")就得到了你参考一下,思路不会有错误,但是可能需要稍加改动,如果什么都那么准确详细
    自己不研究也就没啥意思了