⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 userhelper.java

📁 Oracle的J2EE Sample
💻 JAVA
字号:
/*
 * @author : Elangovan Balusamy
 * @version 1.0
 *
 * Development Environment : Oracle9i JDeveloper
 * Name of the File        : UserHelper.java
 * 
 * Creation / Modification History
 *    Elangovan           19-Aug-2003        Created
 *
 */
package oracle.otnsamples.ibfbs.usermanagement.ejb;

import oracle.toplink.tools.sessionmanagement.SessionManager;
import oracle.toplink.tools.sessionconfiguration.XMLLoader;
import oracle.toplink.threetier.ServerSession;
import oracle.toplink.threetier.ClientSession;
import oracle.toplink.expressions.ExpressionBuilder;
import oracle.toplink.queryframework.ValueReadQuery;
import oracle.toplink.queryframework.ReadObjectQuery;
import oracle.toplink.exceptions.DatabaseException;

import oracle.otnsamples.ibfbs.toplink.Symbol;


/**
 * This singleton class encapsulates all datasource access needed by 
 * UserManagementSessionFacadeBean.
 * 
 * @see UserManagementSessionFacadeBean.java
 */
public class UserHelper {

  // Handle to Server session
  private ServerSession server = null;
  
  // Static instance of this class
  private static UserHelper helper = null;

  /**
   * Creates a new instance of UserHelper.
   * 
   * @exception DatabaseException if intiailizing Toplink server session fails
   */
  private UserHelper() 
    throws DatabaseException {
      // Initialize ServerSession from session.xml 
      server = (ServerSession) SessionManager.getManager().getSession(new XMLLoader(),
                                "FBSServerSession",
                                Thread.currentThread().getContextClassLoader());    
  }
  
  /**
   * Returns a static instance of UserHelper.
   * 
   * @return  static instance of UserHelper
   * @exception DatabaseException if initializing server session fails
   */
  public static UserHelper getInstance() 
    throws DatabaseException {
    
    if(helper == null) {
      helper = new UserHelper();
    }  
    return helper;   
  }

   /**
   * Checks whether the Symbol exists in the datasource or not.
   *
   * @param s Name of the Symbol to be checked
   * @return Flag indicating whether the symbol exists or not
   *         returns true if symbol exists
   *         returns false if symbol doesnot exist
   */
  public boolean checkSymbolExists(String s) {   

    ClientSession client = null;
    Symbol        symbol = null;    
    boolean       exists = false; 
    
    try {
      // Initialize client session
      client = server.acquireClientSession();

      // Initialize read query to read Symbol ( to read a single Symbol )
      ReadObjectQuery readSymbol = new ReadObjectQuery(Symbol.class);
      
      // Construct the where condition
      // WHERE symbol = ?
      ExpressionBuilder whereClause = new ExpressionBuilder();
      readSymbol.setSelectionCriteria(whereClause.get("symbol").equalsIgnoreCase(s));
      
      symbol = (Symbol)client.executeQuery(readSymbol);
            
    } catch (Exception ex) {     // Generic errors
      System.out.println("UserHelper.checkSymbolExists: Error getting symbol status :"+
                           ex.toString());
    } 
    if(symbol != null) {    
      exists = true;                                   
    }    
    return exists;
  }

  /**
   * Retrieves the next value of the specified sequence.
   * 
   * @param seqName Name of sequence whose next value has to be fetched
   * @return next val of the specified sequence
   * @exception DatabaseException if fetching next value fails
   */
  public Integer getNextID(String seqName) 
      throws DatabaseException {
    
    // Initialiae client session  
    ClientSession client = server.acquireClientSession();
    
    Number seqVal = null;
    
    // Initialize read query to read a single value
    ValueReadQuery query = new ValueReadQuery();
    
    // Set the SQL call to be executed
    query.setSQLString(" SELECT "+seqName+".NEXTVAL FROM Dual");
    
    seqVal = (Number)client.executeQuery(query);
    
    return new Integer(seqVal.intValue());
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -