📄 dbidgenerator.java
字号:
package com.ywh.dbcp;
import java.util.Vector;
import java.util.List;
import java.util.HashMap;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import org.apache.commons.dbcp.ConnectionFactory;
/**
* <p>Title: 数据库工具类</p>
* <p>Description: 提供一些数据库使用的类</p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: greatom</p>
* @author 王红宝
* @version 1.0
* 对应的最大ID DDL是
*
* create table SYMAX_ID(
* TABLE_NAME VARCHAR2(30) not null,
* COLUMN_NAME VARCHAR2(30) not null,
* START_VALUE NUMBER(18) not null,
* PREFIX VARCHAR2(20),
* STOP_VALUE NUMBER(18),
* REMARKS VARCHAR2(100), constraint SYMAX_ID_PK primary key (TABLE_NAME,COLUMN_NAME));
*/
public class DBIDGenerator extends PhraseIDGenerator
{
public DBIDGenerator(ConnectionFactory factory,
int length, String tbName, String column)
{
super(length, tbName);
this.column = column;
this.factory = factory;
}
protected void refresh()
{
Connection conn = null;
try
{
if ( factory == null ){
System.out.println("connection factory is null!");
}
conn = factory.createConnection();
Statement stmt = null;
ResultSet rs = null;
conn.setAutoCommit(true); // temporary workaround, should throw exception instead
stmt = conn.createStatement();
stmt.executeUpdate("UPDATE SYMAX_ID SET START_VALUE = START_VALUE + " +
_originLength + " WHERE TABLE_NAME = '" + _tableName.toUpperCase() +
"' AND COLUMN_NAME='" + column.toUpperCase() + "'");
rs = stmt.executeQuery("SELECT START_VALUE FROM SYMAX_ID WHERE TABLE_NAME = '" + _tableName.toUpperCase() +
"' AND COLUMN_NAME='" + column.toUpperCase() + "'");
if(!rs.next())
{
throw new Exception("Maxids table has no row for table " + _tableName.toUpperCase() +
" and column for " + column);
}
_minID = rs.getInt(1) - _originLength;
_length = _originLength;
rs.close();
stmt.close();
}catch(Exception e)
{
e.printStackTrace();
}finally{
if ( conn != null ){
try
{
conn.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
}
}
private String column;
private ConnectionFactory factory;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -