Context env = (Context)(new InitialContext().lookup("java:comp/env"));DataSource ds=(DataSource)env.lookup("Wuliu");试试

解决方案 »

  1.   

    还是不行,提示
    javax.naming.NameNotFoundException: Unable to resolve 'app/webapp/DefaultWebApp/2913366/comp/env/WuLiu' Resolved: 'app/webapp/DefaultWebApp/2913366/comp/env' Unresolved:'WuLiu' ; remaining name 'WuLiu'
      

  2.   

    你的datasource的target是否设了server
      

  3.   

    你的 jndi名不对要用weblogic的t3://localhost:7001你试试用下面的方法取得Context对象  private Context getInitialContext() throws Exception {
        String url = "t3://localhost:7001";
        String user = null;
        String password = null;
        Properties properties = null;
        try {
          properties = new Properties();
          properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
          properties.put(Context.PROVIDER_URL, url);
          if (user != null) {
            properties.put(Context.SECURITY_PRINCIPAL, user);
            properties.put(Context.SECURITY_CREDENTIALS, password == null ? "" : password);
          }      return new InitialContext(properties);
        }
        catch(Exception e) {
          System.out.println("Unable to connect to WebLogic server at " + url);
          System.out.println("Please make sure that the server is running.");
          throw e;
        }
      }
      

  4.   

    you try:Class.forName("weblogic.jdbc.mssqlserver4.Driver");
    conn=DriverManager.getConnection("jdbc:weblogic:mssqlserver4:你的数据库名称@localhost","sa","");
    Statement stmt = conn.createStatement();
    rs = stmt.executeQuery(sql);
      

  5.   

    你先去查一下你的connection pool和datasource是不是配好,有可能是那儿就不对了:)
      

  6.   

    在WEB.XML中建立
     <resource-ref>
        <description>Allow the Fast Lane Reader Pattern to Read Catalog Data Directly via JDBC in the Web Tier</description>
        <res-ref-name>jdbc/Wuliu</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
      </resource-ref>
    然后在sun-j2ee-ri.xml中
        <resource-ref>
          <res-ref-name>jdbc/Wuliu</res-ref-name>
          <jndi-name>Wuliu</jndi-name>
          <default-resource-principal>
            <name>用户名</name>
            <password>密码</password>
          </default-resource-principal>
        </resource-ref>
    在程序中
    Context env = (Context)(new InitialContext().lookup("java:comp/env/jdbc/Wuliu"));
      

  7.   

    程序中应该为
    InitialContext ic=new InitialContext();
    DataSource ds=(DataSource)ic.lookup("java:comp/env/jdbc/Wuliu");
      

  8.   

    public Connection getConnection() throws Exception {
                   try {
                           Context ctx = new InitialContext();
                           javax.sql.DataSource ds = 
                               (javax.sql.DataSource)ctx.lookup("Wuliu");
                           return ds.getConnection();
                   }
                   catch (Exception e) {
                           System.err.println("Could not locate datasource!Reason:");
                           e.printStackTrace();
                           throw e;
            
                   }
           }ejb-jar.xml中:  
               <resource-ref>
                    <description />
                    <res-ref-name>Wuliu</res-ref-name>
                    <res-type>javax.sql.DataSource</res-type>
                    <res-auth>Container</res-auth>
                </resource-ref>我是这样做的:)
      

  9.   

    weblogic-ejb-jar.xml:<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE weblogic-ejb-jar PUBLIC '-//BEA Systems, Inc.//DTD WebLogic 7.0.0 EJB//EN' 'http://www.bea.com/servers/wls700/dtd/weblogic-ejb-jar.dtd'>
    <weblogic-ejb-jar>
        <weblogic-enterprise-bean>
            <ejb-name>TestBean</ejb-name>
            <reference-descriptor>
                <resource-description>
                    <res-ref-name>Wuliu</res-ref-name>
                    <jndi-name>Wuliu</jndi-name>
                </resource-description>
            </reference-descriptor>
            <jndi-name>TestBean</jndi-name>
        </weblogic-enterprise-bean>
    </weblogic-ejb-jar>