本帖最后由 givemeluck 于 2011-02-16 14:32:19 编辑

解决方案 »

  1.   

    我的读取datasource 的代码如下:
              InitialContext ctx = new InitialContext();
    // String lookStr = "java:/WWWHKG";
    String lookStr = "java:/WWWHKG";
    logger.info("doPost(): lookStr is: " + lookStr);
    DataSource ds = (DataSource) ctx.lookup(lookStr);
    // java:comp/env/
    // Context envCtx = (Context) ctx.lookup("java:comp/env");
    // DataSource ds = (DataSource) envCtx.lookup("jdbc/TheDS");
    logger.info("doPost(): get ds. ");
    conn = ds.getConnection();
    Statement stmt = null;
    stmt = (Statement) conn.createStatement();
      

  2.   

    你是不是还没搞清楚 jndi 是干啥用的~~
      

  3.   

    3楼:
    我想用jndi得到设置好的数据源的属性值:如下面:
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws IOException { try {
    logger.info("doPost(): Begin the servlet handling.");
    Connection conn = null;
    // InitialContext ctx1 = new InitialContext();
    InitialDirContext initialContext = new InitialDirContext();
    // DirContext ctx = initialContext;
    // InitialContext ctx = new InitialContext();
    logger.info("doPost(): 1");
    // Get all the attributes of named object
    Attributes answer = initialContext.getAttributes("WWWHKG");
    logger.info("doPost(): 2");
    String driver = answer.get("driver-class").toString();
    String username = answer.get("user-name").toString();
    String password = answer.get("password").toString();
    String Message = "driver is: " + driver + "; username is : "
    + username + "; password is : " + password;
    logger.info("doPost(): Message is : " + Message);
    PrintWriter out = response.getWriter(); out.println("<script language=\"JavaScript\">");
    out.println("alert('" + Message + "')");
    out.println("history.back();");
    out.println("</script>");
    out.flush(); } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } }但是在这句: Attributes answer = initialContext.getAttributes("WWWHKG"); 的时候,总是报错javax.naming.NotContextException: Not an instance of DirContext, 我看网上的例子一般在初始化 InitialDirContext的时候都是给设定了environment的,但是我这个需求是要从sybase-ds.xml里得到信息,不知道怎么做。
      

  4.   

    有另外一种解决思路, 从jndi里获得 datasource,能不能通过datasource或者  connection得到这个连接的account,password, driver ?
      

  5.   

    可以得到连接的那个串jdbc:sybase:Tds:192.168.3.252:4001.用户名和密码不知道能不能得到. String conStr = conn.getMetaData().getURL();
      

  6.   

    context.getEnvironment() 呢?这个里面是不是空的?getAttributes() 是给 LDAP 用的。
      

  7.   

    6楼: String conStr = conn.getMetaData().getURL();只可以得到url,其他不能得到。
    7楼:
    context.getEnvironment() ,我在xml文件里定义了多个datasource,用这个方法应该是得到所有的datasource 的environment吧。
      

  8.   

    经过实验,发现上面的方法做不到。
    现在想这么来做: 在jboss里通过 web application 下的META-INF里的context.xml来设置datasource以及parameter:
    context.xml如下:
    <?xml version="1.0" encoding="UTF-8"?>
    <Context>
    <Resource name="WWWHKG2" auth="Container"
    type="javax.sql.DataSource" username="reader1" password="checking1"
    driverClassName="com.sybase.jdbc3.jdbc.SybDataSource"
    url="jdbc:sybase:Tds:192.168.13.252:4888" maxActive="8" maxIdle="4" />
    <Parameter name="username2" value="reader" override="false" />
    <Parameter name="password2" value="checking" override="false" />
    <Parameter name="driverClassName2"
    value="com.sybase.jdbc3.jdbc.SybDataSource" override="false" />
    </Context>
    在servlet里有如下代码:
    Connection conn = null;
    // InitialContext ctx = new InitialContext();
    String lookStr1 = "java:comp/env";
    String lookStr = "WWWHKG2";
    logger.info("doPost(): lookStr is: " + lookStr);
    // DataSource ds = (DataSource) ctx.lookup(lookStr);
    Context initCtx = new InitialContext();
    Context ctx = (Context) initCtx.lookup(lookStr1); // below lookup the pre-defined parameters
    //String companyName = (String) ctx.lookup("companyName");
    String userName = (String) ctx.lookup("username2");
    String Password = (String) ctx.lookup("password2");
    String Driver = (String) ctx.lookup("driverClassName2");
    logger.info("companyName userName2 Password2 Driver2 are: "
    + companyName + userName + Password + Driver); DataSource ds = (DataSource) ctx.lookup(lookStr);
    logger.info("doPost(): get ds. " + ds.getClass().getName());可是读不到 DataSource ds以及设置好的username2等啊?