java web 项目连mysql tomcat作服务器~~拿不到DataSource

package cn.it.dishes.util;

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class Jdbcutil {
  private static DataSource ds;
  static {
  try {
   Context initContext = new InitialContext();
   Context envContext  = (Context)initContext.lookup("java:/comp/env");
   ds = (DataSource)envContext.lookup("jdbc/TestDB");
  } catch (NamingException e) {
   e.printStackTrace();
  }
  }
  public static Connection getConnection(){
   Connection conn=null;
   try {
    conn = ds.getConnection();
  } catch (SQLException e) {
   throw new RuntimeException(e);
  }
       return conn;
  }
 
  public static DataSource getDateSource()
  {
   return ds;
  }
 
}

-------------------------------------------xml  配置文档-----------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<Context>

    <!-- maxActive: Maximum number of database connections in pool. Make sure you
         configure your mysqld max_connections large enough to handle
         all of your db connections. Set to -1 for no limit.
         -->

    <!-- maxIdle: Maximum number of idle database connections to retain in pool.
         Set to -1 for no limit.  See also the DBCP documentation on this
         and the minEvictableIdleTimeMillis configuration parameter.
         -->

    <!-- maxWait: Maximum time to wait for a database connection to become available
         in ms, in this example 10 seconds. An Exception is thrown if
         this timeout is exceeded.  Set to -1 to wait indefinitely.
         -->

    <!-- username and password: MySQL username and password for database connections  -->

    <!-- driverClassName: Class name for the old mm.mysql JDBC driver is
         org.gjt.mm.mysql.Driver - we recommend using Connector/J though.
         Class name for the official MySQL Connector/J driver is com.mysql.jdbc.Driver.
         -->
   
    <!-- url: The JDBC connection url for connecting to your MySQL database.
         -->

  <Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
               maxActive="100" maxIdle="30" maxWait="10000"
               username="root" password="123456" driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://localhost:3306/room"/>

</Context>

你可能感兴趣的:(java,Web,无法获取DataSource)