tomcat和WebSphere下JNDI是不同的

public static Connection getConnection1() throws ServletException {
  Connection con = null;

  try {
   Context initCtx = new InitialContext();
            //tomcat中查找如下:  
   //DataSource ds = (DataSource)initCtx.lookup("java:/comp/env/jdbc/b2c");
   //websphere中查找如下:
   /////////////////////////////////////////////////////////////////////////
   DataSource ds = (DataSource)initCtx.lookup("jdbc/shopping");
   ////////////////////////////////////////////////////////////////////////
   con = ds.getConnection();
   System.out.println("数据源连接成功");   
  } catch (NamingException e) {
   e.printStackTrace();   
   throw new ServletException(e.getMessage());
  } catch (SQLException e) {
   e.printStackTrace();   
   throw new ServletException(e.getMessage());
  }
  return con;
 }

原文地址:https://www.cnblogs.com/luleiitlife/p/8545114.html