自己顶!!大家有没有遇到类似情况,我在javasoft上也发现有人和我的问题一摸一样,可是也没有人回答。

解决方案 »

  1.   

    少安毋躁,给你一段代码看看m_ht=new Hashtable();
    m_ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
    m_ht.put(Context.PROVIDER_URL,t3://localhost:7001);
    m_ctx=new InitialContext(m_ht);
    m_ds=(DataSource)m_ctx.lookup(datasourceJNDIname);        
    m_conn=m_ds.getConnection();这是我写ejb时候连接数据源的代码,我的应用服务器是weblogic  
    看对你有没有帮助
      

  2.   

    package ejb;
        import java.util.*;
    import java.io.*;
    import java.sql.*;
    import javax.naming.*;
    import javax.sql.DataSource;
    public class DBConnection implements Serializable{
      protected Connection con=null;
      private  String  url  = "com.ibm.websphere.naming.WsnInitialContextFactory" ;
      private  String  user = "db2admin" ;
      private  String  pass = "db2admin" ;
      private  String  Dbs  = "jdbc/DataSource" ;
      public  DBConnection() { }
      DataSource ds = null;
      public void dbOpen() throws java.lang.Exception{
    try
    {
    if (con == null || con.isClosed()) {
     // create parameter list to access naming system
      Hashtable env = new Hashtable();
      env.put(Context.INITIAL_CONTEXT_FACTORY, url);
    // access naming system
      Context context = new InitialContext(env);
    // get DataSource factory object from naming system
      ds = (DataSource)context.lookup(Dbs);
      con = ds.getConnection(user, pass);
       System.out.println("DB OPEN");
    } else {
    System.out.println("OPENED DB");
    }
    } catch (Exception e)//数据源不存在,用jdbcjdbc连接
    {
                        System.out.println("数据源不存在");
                        Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
                        Properties p=new Properties();
                        p.put("user",user);
                        p.put("password",pass);
                        con=DriverManager.getConnection("jdbc:db2:db_sdbc",p);   System.out.println("连接数据库异常:"+e.toString());
    }   }
     /*public void beginTrans() throws SQLException{
    con.setAutoCommit(false);
      }
      public void commitTrans() throws SQLException{
    con.commit();
      }
      public void rollbackTrans() throws SQLException{
    con.rollback();
      }
      public boolean isTrans() throws SQLException{
    return !con.getAutoCommit();
      }*/
      public void dbClose() {
    try {
    if (con != null && !con.isClosed()) {
    con.rollback();
    con.close();
    System.out.println("DB CLOSE");
    } else {
    System.out.println("CLOSED DB");
    }
    }
    catch (Exception e) {
    System.out.println("关闭数据库异常:"+e.toString());
    }
    finally {
    con = null;
    }
    }
    }
      

  3.   

    谢谢上面两位,但是我没有weblogic或者websphere,我用的就是j2eesdk自带的那个j2ee Server,所以不能使用你们说得方法,我也试着寻找Context.INITIAL_CONTEXT_FACTORY相对应的InitialContextFactory,但是没有找到,我看一些j2ee教程是使用无参数的InitialContext(),但不知为什么不可以,另外,我不是在ejb中调用这些代码,而是不同的客户端,不知道这里有没有影响。最后,谁清楚j2eesdk中的DeployTool,写一下如何将Datasource和JNDI进行绑定。我试着自己在Tools|Server Configuration中的DataSource绑定了一个自己的DataSource,而且在启动j2ee时也可以看到正确绑定,不知这种方法是否可行。