public test(){
    try {
      // get the DataSource from JNDI
      InitialContext initialContext = new InitialContext();
      Object object = initialContext.lookup( "testDB");
      dataSource = (DataSource) PortableRemoteObject.narrow( object, DataSource.class );
    } catch( Throwable t ) {
      t.printStackTrace();
      throw new RuntimeException( "couldn't find DataSource '" + dataSourceName + "' in JNDI-registry : " + t.getMessage() );
    }
}

解决方案 »

  1.   

    关于icf和url,知道weblogic和jboss,websphere的,tomcat的naming service没有研究过
      

  2.   

    public String GetAField(char userID)
    {
        try {
          // get the connection
            connection = dataSource.getConnection();      // get the first free id
          PreparedStatement stmt = connection.prepareStatement( selectStatement );
          ResultSet resultSet = stmt.executeQuery();
          resultSet.next();
          firstIdOfBlock = resultSet.getLong( 1 );
          stmt.close();      // add blockSize to the first free id
          stmt = connection.prepareStatement( updateStatement );
          stmt.setLong( 1, ( firstIdOfBlock + blockSize ) );
          int nbrOfUpdatedRecords = stmt.executeUpdate();
          stmt.close();      // there may only be one record in the table
          if ( nbrOfUpdatedRecords != 1 ) {
            throw new RuntimeException( "more then one record was updated by statement '" + updateStatement + "'" );
          }    } catch (SQLException e) {
          context.setRollbackOnly();
          if ( e.getMessage().indexOf( "Table not found" ) != -1 ) {
            throw new SQLException( "Did you forget to create the database ? : SequenceSession-EJB couldn't create new block of id's : " + e.getMessage() );
          } else {
            throw new SQLException( "SequenceSession-EJB couldn't create new block of id's : " + e.getMessage() );
          }
        } catch (Throwable t) {
          context.setRollbackOnly();
          t.printStackTrace();
          throw new RuntimeException( "SequenceSession-EJB couldn't create new block of id's : " + t.getMessage() );
        } finally {
          if ( connection != null ) {
            try {
              connection.close();
            } catch (Throwable t ) {
              t.printStackTrace();
              System.out.println( "SequenceSession-EJB couldn't close connection while fetching a next block of database id's : " + t.getMessage() );
            }
          }
        }}
      

  3.   

    我没有用过JNDI,能不能发个例子给我呀?[email protected]
    Tomcat + Oracle + Linux 用JNDI的,谢谢!