呵,你的InitialContext没东东啊你如果是用weblogic服务器,这儿有一个例子:  private static Context getInitialContext() throws Exception {
    String url = "t3://localhost:7001";
    String user = "bibi";
    String password = "12345678";
    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 ex) {
      throw ex;
    }
  }
然后去调用它:
     Context ctx = getInitialContext();
     DataSource ds = (javax.sql.DataSource) ctx.lookup("myDataSource");

解决方案 »

  1.   

    嗯,谢谢!只是你用的是weblogic,我用的tomcat,这个参数设置上应该是不一样的吧?
      

  2.   

    改一下这个:
          properties.put(Context.INITIAL_CONTEXT_FACTORY,
                         "org.apache.naming.java.javaURLContextFactory");
    然后其它照着套试试
      

  3.   

    不知你的目的是不是:tomcat配置连接池然后操作数据库(MSSQL)给你一个例子:
    在server.xml中加:
            <Context path="" docBase="www"
            debug="5" reloadable="true" crossContext="true">

      <Logger className="org.apache.catalina.logger.FileLogger"
         prefix="localhost_www_log." suffix=".txt"
         timestamp="true"/>

      <Resource name="jdbc/MSSQL7.0DB"
           auth="Container" 
           type="javax.sql.DataSource"/>

      <ResourceParams name="jdbc/MSSQL7.0DB">
        <parameter>
          <name>factory</name>
          <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
        </parameter>
        <parameter>
          <name>maxActive</name>
          <value>20</value>
        </parameter>
        <parameter>
          <name>maxIdle</name>
          <value>300000</value>
        </parameter>
        <parameter>
          <name>maxWait</name>
          <value>1000</value>
        </parameter>
        <parameter>
         <name>username</name>
         <value>sa</value>
        </parameter>
        <parameter>
         <name>password</name>
         <value>981050</value>
        </parameter>

        <parameter>
           <name>driverClassName</name>
           <value>com.jnetdirect.jsql.JSQLDriver</value>
        </parameter>

        <parameter>
          <name>url</name>
          <value>jdbc:JSQLConnect://172.16.10.99:1433/database=takam</value>
        </parameter>
        <parameter>
          <name>removeAbandoned</name>
          <value>true</value>
        </parameter>
        <parameter>
          <name>removeAbandonedTimeout</name>
          <value>60</value>
       </parameter>
       <parameter>
         <name>logAbandoned</name>
         <value>true</value>
               </parameter>
      </ResourceParams>
    </Context>
    然后可以用如下方法调用 :
        Connection con =null;
        Statement  stmt=null;
        ResultSet  rs  =null;
    javax.sql.DataSource ds = (javax.sql.DataSource)new javax.naming.InitialContext().lookup("java:comp/env/jdbc/MSSQL7.0DB");
            if (ds != null)    con = ds.getConnection();
            if (con != null)   stmt=con.createStatement(                ResultSet.TYPE_SCROLL_SENSITIVE,     ResultSet.CONCUR_READ_ONLY
                   );
      

  4.   

    呵呵,老兄,你猜的是没有错的!只是多少有些变化,你看看这个帖子:http://www.jdon.com/jive/thread.jsp?forum=16&thread=13176
      

  5.   

    我想实现的是读取自己写的配置文件,然后设置数据源,建立数据库连接!
    BasicDataSource source=null;
    try{
    source = new BasicDataSource();          //以下设置的参数都可以从配置文件中获得.而可以不局限在server.xml
             String connectURL="jdbc:mysql://localhost:3306/vter_info";
             source.setDriverClassName("org.gjt.mm.mysql.Driver");
             source.setUrl(connectURL);
             source.setUsername("root");
             source.setPassword(""); 
             source.setMaxActive(1000); 
             source.setMaxIdle(30);
             source.setMaxWait(10000);我的意思你明白了吗?
      

  6.   

    我正好看过楼主的另外一个帖子。何必呢,你在server.xml中定义多个数据源,不就行了。
      

  7.   

    改动原来的:
    Context initCtx = new InitialContext();为:
    java.util.Hashtable properties = new java.util.Hashtable();
    properties.put(Context.INITIAL_CONTEXT_FACTORY,
                       "org.apache.commons.dbcp.BasicDataSourceFactory");
    Context initCtx = new InitialContext(properties);
    试试看
      

  8.   

    你的程序还是有问题 我觉得, 没有配置连接池,而直接使用Datasource了应该是先建立连接池, 再设置Datasource
    Datasource 是访问实际连接池的
      

  9.   

    package com.unicom.race.util;
    import javax.sql.DataSource;
    import java.sql.Connection;
    import java.sql.Statement;
    import java.sql.ResultSet;
    import java.sql.SQLException;//
    // Here are the dbcp-specific classes.
    // Note that they are only used in the setupDriver
    // method. In normal use, your classes interact
    // only with the standard JDBC API
    //
    import java.sql.DriverManager;
    import org.apache.commons.pool.ObjectPool;
    import org.apache.commons.pool.impl.GenericObjectPool;
    import org.apache.commons.dbcp.ConnectionFactory;
    import org.apache.commons.dbcp.PoolingDataSource;
    import org.apache.commons.dbcp.PoolableConnectionFactory;
    import org.apache.commons.dbcp.DriverManagerConnectionFactory;
    import org.apache.commons.dbcp.AbandonedConfig;public class DBCPPOOL {private static PropertyReader pr = PropertyReader.getInstance("conf");
    private static String driver = pr.getProperty("db.driver").trim();
    private static String uri = pr.getProperty("db.uri").trim();
    private static String user = pr.getProperty("db.user").trim();
    private static String password = pr.getProperty("db.password").trim();
    private static     DataSource dataSource =null;public DBCPPOOL() {
    }public static Connection getConnection()
    throws Exception {
    Connection conn = null;         if (dataSource == null){
                dataSource = setupDataSource(driver,uri,user,password);
              }          conn = dataSource.getConnection();
    return conn; }private static DataSource setupDataSource(String driver,String connectURI,String user,String password)
    throws Exception{ //加载驱动程序
    Class.forName(driver);
            //
            // First, we'll need a ObjectPool that serves as the
            // actual pool of connections.
            //
            // We'll use a GenericObjectPool instance, although
            // any ObjectPool implementation will suffice.
            //
    ObjectPool connectionPool = new GenericObjectPool(null);        //
            // Next, we'll create a ConnectionFactory that the
    // pool will use to create Connections.
    // We'll use the DriverManagerConnectionFactory,
            // using the connect string passed in the command line
            // arguments.
            //
           ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI,user,password);        //
            // Now we'll create the PoolableConnectionFactory, which wraps
            // the "real" Connections created by the ConnectionFactory with
            // the classes that implement the pooling functionality.
            //
    AbandonedConfig abandonedConfig = new AbandonedConfig();
    abandonedConfig.setRemoveAbandoned(true);
    //abandonedConfig.setRemoveAbandonedTimeout(60);
    abandonedConfig.setLogAbandoned(true); PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,connectionPool,null,null,false,true,abandonedConfig);        //
            // Finally, we create the PoolingDriver itself,
            // passing in the object pool we created.
            //
            PoolingDataSource dataSource = new PoolingDataSource(connectionPool);
    //Logger.log( "DBCPPOOL Active Connections:" + connectionPool.toString() );
    //Logger.log( "DBCPPOOL Idle Connections:" + connectionPool.getNumIdle() );        return dataSource;
        }}
      

  10.   

    我认为是你的CMP所调用的表没有主键,这个问题我遇到国,将表中加一个主键就可以了
      

  11.   

    楼主干脆自己写连接池程序,用自己的配置文件. <<什么都用自己写的>>
      

  12.   

    对了,问一下啊,在J2EE1。4中如何设置JNDI啊!
    我弄了半天也没有搞出来,
    程序总是出错,可能就是没有找到数据源了!
    如何设置JNDI和数据库之间的联系啊!