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

📄 idgenerator.java

📁 采用JAVA开发
💻 JAVA
字号:
package com.gctech.sms.dao;

import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.gctech.sms.SmsException;
import com.gctech.sms.util.ConnectionManager;


/**
 * ID生成器.
 * 使用方法,String id = IDGenerator.nextId();
 * or String id = IDGenerator.nextId("seqName");
 * @author 王红宝
 * @version $Id: IDGenerator.java,v 1.2 2004/04/22 01:25:15 wanghb Exp $
 * **/

public class IDGenerator {
  IDGenerator(){}
  static final String DEFAULT_SEQUENCE = "SMS_SEQUENCE";

  /**
   * 得到一个缺省的sequence ID
   * 缺省的名字是ACCOUNT_SEQUENCE
   * 传递数据库连接
   * */
  public static String nextId(Connection con) throws  SQLException {
    return nextId(con, DEFAULT_SEQUENCE);
  }
  /**
   * 通过一个oracle seq得到下一个ID.
   * 外部传递连接。
   * **/
  public static String nextId(Connection con, String seqName) throws
      SQLException {
    Statement stmt = null;
    ResultSet rs = null;
    try {
      String rt = null;
      stmt = con.createStatement();
      rs = stmt.executeQuery("select "+seqName+".nextval from dual");
      if ( rs.next() ){
        rt = rs.getString(1);
      }
      return rt;
    }catch(SQLException sqle){
      throw sqle;
    }finally{
      try {
        if ( rs == null ){
        }else{
          rs.close();
        }
      }catch (Exception ex) {
        ex.printStackTrace();
      }
      try {
        if ( stmt == null ){
        }else{
          stmt.close();
        }
      }catch (Exception ex) {
        ex.printStackTrace();
      }
    }
  }
  /**
   * 得到一个缺省的sequence ID
   * 缺省的名字是ACCOUNT_SEQUENCE
   * */
  public static String nextId()throws SQLException {
    return nextId(DEFAULT_SEQUENCE);
  }
  /**
   * 通过一个oracle seq得到下一个ID.
   * **/
  public static String nextId(String seqName)
      throws SQLException {
    Connection con = null;
    try {
      con = ConnectionManager.getInstance().getConnection(IDGenerator.class);
      return nextId(con, seqName);
    }finally{
      try {
        if ( con == null ){
        }else{
          con.close();
        }
      }catch (Exception ex) {
        ex.printStackTrace();
      }
    }
  }
}

⌨️ 快捷键说明

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