📄 idgeneratormanager.java
字号:
/*
* IDGeneratorManager.java
* Generated using xgen and texen from beanmanager.vm
* Wed Jul 24 15:41:49 CST 2002
*/
package com.sure.util.database;
import java.sql.SQLException;
import java.sql.Connection;
import java.util.Vector;
import java.util.NoSuchElementException;
import com.sure.dataabstraction.DBManager;
import com.sure.dataabstraction.DBPoolException;
/**
* (整型)ID生成器,应用Singleton模式,调用getInstance方法得到IDGeneratorManager的实例。
* @author little.-.bird@263.net
*/
public class IDGeneratorManager {
static IDGeneratorManager idm = null;
public IDGeneratorManager() {}
/**
* 得到全局唯一性的实例
* @return 实例化的IDGeneratorManager对象
*/
public static synchronized IDGeneratorManager getInstance() {
if(idm == null) {
idm = new IDGeneratorManager();
}
return idm;
}
/**
* 通过键值得到ID的对象
* @param cn 数据库连接
* @param primaryKey 键值
* @return 与键值相对应的对象
* @throws SQLException
*/
public IDGenerator getBeanByPrimaryKey (Connection cn, String primaryKey) throws SQLException {
String where = "Where NAME = '" + primaryKey + "'";
Vector beans = IDGeneratorPersistent.load(cn, where);
IDGenerator bean = (IDGenerator)beans.firstElement();
return bean;
}
/**
* 通过键值得到ID的对象,如果该ID不存在,则会新创建一个。缺省创建的是起始值为1
* @param primaryKey 键值
* @return 与键值相对应的对象
* @throws SQLException
*/
public IDGenerator getBeanByPrimaryKey (String primaryKey) throws SQLException {
Connection cn = null;
IDGenerator id = null;
try {
cn = DBManager.getConnection();
id = getBeanByPrimaryKey (cn, primaryKey);
} catch(NoSuchElementException e) { //如果没有这个键值,创建一个先
id = create(cn, primaryKey);
} catch(DBPoolException e) {
//logError here
System.out.println("无法建立数据库连接");
e.printStackTrace();
} finally {
release(cn);
return id;
}
}
/**
* 以给定的键值创建一个ID的对象
* @param cn 数据库连接
* @param primaryKey 键值
* @return 与键值相对应的对象
* @throws SQLException
*/
public IDGenerator create (Connection cn, String primaryKey) throws SQLException {
IDGenerator id = new IDGenerator(primaryKey);
IDGeneratorPersistent idp = new IDGeneratorPersistent(id);
idp.persist(cn);
return id;
}
/**
* 以给定的ID对象创建一个ID的对象
* @param cn 数据库连接
* @param id 键值
* @return 与键值相对应的对象
* @throws SQLException
*/
public IDGenerator create (Connection cn, IDGenerator id) throws SQLException {
IDGeneratorPersistent idp = new IDGeneratorPersistent(id);
idp.persist(cn);
return id;
}
/**
* 将给定的ID对象保存到数据库中
* @param cn 数据库连接
* @param id 键值
* @throws SQLException
*/
public void save (Connection cn, IDGenerator id) throws SQLException {
IDGeneratorPersistent idp = new IDGeneratorPersistent(id, true);
idp.persist(cn);
}
/**
* 根据给定的键值把键值的改变保存到数据库中
* @param cn 数据库连接
* @param primaryKey 键值
* @return 当前值的下一个值
* @throws SQLException
*/
public int setNextValue (Connection cn, String primaryKey) throws SQLException {
boolean autoCommit = cn.getAutoCommit();
IDGenerator id = null;
try {
id = getBeanByPrimaryKey (cn, primaryKey);
id.setValue(id.getNextValue().intValue());
id.setNextValue(id.getNextValue().intValue() + 1);
save(cn, id);
} catch(NoSuchElementException e) { //如果没有这个键值,创建一个先
id = create(cn, primaryKey);
}
return id.getValue().intValue();
}
/**
* 根据给定的键值把键值的改变保存到数据库中
* @param primaryKey 键值
* @return 当前值的下一个值
* @throws SQLException
*/
public int setNextValue (String primaryKey) throws SQLException {
Connection cn = null;
int value = 1;
try {
cn = DBManager.getConnection();
value = setNextValue(cn, primaryKey);
cn.commit();
} catch(DBPoolException e) {
//logError here
System.out.println("无法建立数据库连接");
e.printStackTrace();
} finally {
release(cn);
return value;
}
}
/**
* 根据给定的键值把数据库中的值给删掉
* @param cn 数据库连接
* @param primaryKey 键值
* @throws SQLException
*/
public void remove (Connection cn, String primaryKey) throws SQLException {
String where = "Where name='" + primaryKey + "'";
IDGeneratorPersistent.delete(cn, where);
}
/**
* 将数据库连接释放掉
* @param cn 数据库连接
*/
protected void release(Connection cn) {
if(cn == null)
return;
try {
cn.close();
} catch(SQLException e) {
//log Error here;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -