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

📄 dbphraseidgenerator.java

📁 采用JAVA开发
💻 JAVA
字号:
package com.ywh.dbcp;

import java.sql.*;
import java.util.*;
import org.apache.commons.dbcp.ConnectionFactory;


public class DBPhraseIDGenerator extends PhraseIDGenerator
{
  private static HashMap generators = null;
  private static ConnectionFactory _pool;
  protected DBPhraseIDGenerator(int length, String tbName)
  {
    super(length, tbName);
  }
  protected void refresh()
  {
     Connection conn = null;
     try
     {
       conn = _pool.createConnection();
       Statement stmt = null;
       ResultSet rs = null;
       conn.setAutoCommit(true);  // temporary workaround, should throw exception instead
       stmt = conn.createStatement();
       stmt.executeUpdate("UPDATE maxids SET maxid = maxid + " + _originLength + " WHERE tablename = '" + _tableName.toUpperCase() + "'");
       rs = stmt.executeQuery("SELECT maxid FROM maxids WHERE tablename = '" + _tableName.toUpperCase() + "'");
       if(!rs.next())
       {
         throw new Exception("Maxids table has no row for table " + _tableName.toUpperCase());
       }
       _minID = rs.getInt(1) - _originLength;
       _length = _originLength;
       rs.close();
       stmt.close();
     }catch(Exception e)
     {
       e.printStackTrace();
     }finally{
       try
       {
         conn.close();
       }catch(Exception e)
       {
         e.printStackTrace();
       }
     }
  }

  public static void setConnectionFactory(ConnectionFactory pool)
  {
    _pool = pool;
  }
  public static IDGenerator getInstance(String tbName, int length)
  {
    if ( generators == null )
     {
       generators = new HashMap();
     }
     Object o = generators.get(tbName);
     if ( o == null )
     {
       IDGenerator generator = new DBPhraseIDGenerator(length, tbName);
       generators.put(tbName, generator);
       o = generator;
     }
     return (IDGenerator)o;
  }
  public static IDGenerator getInstance(java.lang.String tbName)
  {
     return getInstance(tbName, 10);
   }
}

⌨️ 快捷键说明

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