tomcat和WebSphere下JNDI是不同的

来源:互联网 发布:白金汉网络订课系统 编辑:程序博客网 时间:2024/04/30 17:36

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;
 }

原创粉丝点击