项目里对框架的要求除了功能,扩展性,性能等方面的要求外还有对项目开发周期与新手学习周期和难易度的考虑。由于项目开发时间压力很大,而且项目组员对
sql语句非常熟悉,而且处于简化普通开发人员开发量的考虑在此项目中没有使用hibernate和ibatis等已经成熟的持久层框架,而是采用自己开
发的O/R mapping类来实现通用的插入删除修改以及查询(单表)功能。基本能够满足项目里的6,70%的功能要求。
原理采用java的反射来找出javabean中的所有属性和值,生成标准插入删除修改和查询的sql语句,解决了开发人员的后顾之忧(拼写sql经常出
错)。不过由于没有类似hibernate和ibatis的属性和数据库字段的对应配置文件,所以java
bean的属性和数据库字段名要求一致(大小写可以随意),此限制因为java bean可以使用工具批量生成所以也无所谓了。java
bean(同时是value bean,data
bean,POJO,减少代码量)可以通过以下途径生成:首先生成schema文件(可以采用powerdesigner生成物理模型后自动在生成xml
模型再生成xml schema文件,或者直接用xml spy从数据库物理表反向生成xml
schema),然后通过castor(jbuilder自带,eclipse需要安装castor的插件)来生成java
bean。一切就是这么简单。项目大量的O/R
转换工作就完成了,前台显示(jsp)中使用的和中间控制层,业务逻辑层,数据访问(DAO)层都使用这些javabean。项目中的大量功能就这样不用
写一句代码就能完成了。
public class DataAccess {
private Class tempClass;
private Class[] parameterClassArray = new Class[1];
private Object[] paramenterValues = new Object[1];
private Object tempObject;
private Method tempMethod;
private String tempClassName;
private String tempColumnName;
private String[] tempMethodNames = new String[80];
private String strSql = null;
private String strTableName;
private String[] tempColumnNames = new String[80]; //对象所有属性名
private String[] tempColumnTypes = new String[80]; //对象所有属性类型
private static java.util.Random randomSeed = new java.util.Random();
private static Hashtable codeTable = new Hashtable();
/**
* 空构造
*/
public DataAccess() {
}
public boolean execute(DbConnection conn, String sql) throws DbException {
Statement stm = null;
try {
stm = conn.getStatement();
boolean result = stm.execute(sql);
return result;
} catch (Exception ex) {
try {
stm.close();
} catch (Exception e) {
}
throw new DbException(this.getClass().getName(), ".execute sql"
+ sql, ex);
}
}
public int executeUpdate(DbConnection conn, String sql) throws DbException {
Statement stm = null;
try {
stm = conn.getStatement();
int result = stm.executeUpdate(sql);
return result;
} catch (Exception ex) {
try {
stm.close();
} catch (Exception e) {
}
throw new DbException(this.getClass().getName(), ".execute sql"
+ sql, ex);
}
}
public ArrayList selectValueList(DbConnection dbconn, String querysql)
throws DbException {
ArrayList valueList = new ArrayList();
HashMap value = null;
Statement stm = null;
try {
stm = dbconn.getStatement();
ResultSet rs = stm.executeQuery(querysql);
ResultSetMetaData md = rs.getMetaData();
while (rs.next()) {
value = new HashMap();
for (int i = md.getColumnCount(); i > 0; i--) {
String columnName = md.getColumnName(i);
Object o = rs.getObject(i);
value.put(columnName, o);
}
valueList.add(value);
}
return valueList;
} catch (Exception ex) {
try {
stm.close();
} catch (Exception e) {
}
throw new DbException(this.getClass().getName(),
".selectValueList sql" + querysql, ex);
}
}
public HashMap selectValue(DbConnection dbconn, String querysql)
throws DbException {
HashMap value = null;
Statement stmt = null;
try {
stmt = dbconn.getStatement();
ResultSet rs = stmt.executeQuery(querysql);
if (rs.next() == false)
return null;
ResultSetMetaData md = rs.getMetaData();
value = new HashMap();
for (int i = md.getColumnCount(); i > 0; i--) {
String columnName = md.getColumnName(i);
Object valueObject = rs.getObject(i);
value.put(columnName, valueObject);
}
return value;
} catch (Exception ex) {
try {
stmt.close();
} catch (Exception e) {
}
throw new DbException(this.getClass().getName(), ".selectValue sql"
+ querysql, ex);
}
}
/**
* insert function
*
* @param conn:
* connect Object used to execute insert
* @param rowValue:Class
* Object include values
*/
public boolean insert(DbConnection dbconn, Object rowValue)
throws ApplicationException {
Field[] tempFields;
String strColumnNameList = "";
String strColumnValueList = "";
tempClass = rowValue.getClass();
tempClassName = tempClass.getName();
tempFields = tempClass.getDeclaredFields();
strTableName = tempClassName.substring(
tempClassName.lastIndexOf(".") + 1, tempClassName.length());
int m = ObjectTool.getFieldNamesAndTypes(rowValue, tempColumnNames,
tempColumnTypes);
if (m == 0) {
return false;
}
try {
tempObject = tempClass.newInstance();
parameterClassArray[0] = rowValue.getClass();
for (int i = 0; i < m; i++) {
tempColumnName = tempColumnNames[i];
tempMethodNames[i] = "get" + tempColumnName;
tempMethod = tempClass.getDeclaredMethod(tempMethodNames[i],
null);
paramenterValues[0] = null;
if (tempMethod.invoke(rowValue, null) != null) {
strColumnValueList = strColumnValueList
+ ObjectTool.getObjectOpStr(tempMethod.invoke(
rowValue, null)) + ",";
strColumnNameList = strColumnNameList + tempColumnName
+ ",";
}
}
} catch (Exception ex) {
throw new ReflectException(this.getClass().getName(), "insert", ex);
}
strColumnNameList = strColumnNameList.substring(0, strColumnNameList
.length() - 1);
strColumnValueList = strColumnValueList.substring(0, strColumnValueList
.length() - 1);
strTableName = strTableName.toLowerCase();
strSql = "insert into " + strTableName + " (" + strColumnNameList + ")"
+ " values(" + strColumnValueList + ")";
LogManager.log(LogManager.LOGTYPE_DEBUG, "strSql=" + strSql);
Statement stmt = null;
try {
stmt = dbconn.getStatement();
stmt.execute(strSql);
stmt.close();
return true;
} catch (SQLException ex) {
try {
stmt.close();
} catch (Exception e) {
}
throw new DbException(this.getClass().getName(), "insert sql:[ "
+ strSql + " ]", ex);
}
}
/**
* delete function
*
* @param conn:connection
* object,already open
* @param pkNames:primary
* key name Array
* @param rowValue:object
* include primary key value
* @return:ture if success execute
*/
public boolean delete(DbConnection dbconn, String[] pkNames, Object rowValue)
throws ApplicationException {
Field[] tempFields;
String strColumnNameList = "";
String strColumnValueList = "";
tempClass = rowValue.getClass();
tempClassName = tempClass.getName();
tempFields = tempClass.getDeclaredFields();
String tempPKName = "";
String tempMethodName = "";
String strWhere = "";
boolean pkFlag = false;
if (pkNames.length == 0) {
return false;
}
for (int i = 0; i < pkNames.length; i++) {
pkNames[i] = pkNames[i].toUpperCase();
}
strTableName = tempClassName.substring(
tempClassName.lastIndexOf(".") + 1, tempClassName.length());
try {
tempObject = tempClass.newInstance();
parameterClassArray[0] = rowValue.getClass();
for (int i = 0; i < pkNames.length; i++) {
tempPKName = pkNames[i];
tempMethodName = "get" + tempPKName;
tempMethod = tempClass.getDeclaredMethod(tempMethodName, null);
if (tempMethod.invoke(rowValue, null) != null) {
strWhere = strWhere
+ tempPKName
+ " = "
+ ObjectTool.getObjectOpStr(tempMethod.invoke(
rowValue, null)) + " and ";
} else {
return false;
}
}
strWhere = strWhere.substring(0, strWhere.length() - 5);
} catch (Exception ex) {
throw new ReflectException(this.getClass().getName(), "delete", ex);
}
strTableName = strTableName.toLowerCase();
strSql = "delete from " + strTableName + " where " + strWhere;
LogManager.log(LogManager.LOGTYPE_DEBUG, "strSql=" + strSql);
Statement stmt = null;
try {
stmt = dbconn.getStatement();
stmt.execute(strSql);
stmt.close();
return true;
} catch (Exception ex) {
try {
stmt.close();
} catch (Exception e) {
}
throw new DbException(this.getClass().getName(), "delete", ex);
}
}
public boolean delete(DbConnection dbConn, String strWhere, Object rowValue)
throws ApplicationException {
Field[] tempFields;
String strColumnNameList = "";
String strColumnValueList = "";
tempClass = rowValue.getClass();
tempClassName = tempClass.getName();
tempFields = tempClass.getDeclaredFields();
String tempPKName = "";
String tempMethodName = "";
strTableName = tempClassName.substring(
tempClassName.lastIndexOf(".") + 1, tempClassName.length());
try {
tempObject = tempClass.newInstance();
parameterClassArray[0] = rowValue.getClass();
} catch (Exception ex) {
throw new ReflectException(this.getClass().getName(), "delete", ex);
}
strTableName = strTableName.toLowerCase();
strSql = "delete from " + strTableName + " where " + strWhere;
LogManager.log(LogManager.LOGTYPE_DEBUG, "strSql=" + strSql);
Statement stmt = null;
try {
stmt = dbConn.getStatement();
stmt.execute(strSql);
stmt.close();
return true;
} catch (Exception ex) {
try {
stmt.close();
} catch (Exception e) {
}
throw new DbException(this.getClass().getName(), "delete", ex);
}
}
/**
* 带有两个主键的表记录更新
*
* @param conn
* :数据库连接
* @param pkNames
* :主键数组
* @param rowValue
* :要求更新记录的databean实例
* @return :成功与否
*/
public boolean update(DbConnection dbconn, String[] pkNames, Object rowValue)
throws ApplicationException {
Field[] tempFields;
String strSet = "";
tempClass = rowValue.getClass();
tempClassName = tempClass.getName();
tempFields = tempClass.getDeclaredFields();
String tempPKName = "";
String tempMethodName = "";
String strWhere = "";
String hasMethodName="";
Method hasMethod=null;
boolean pkFlag = false;
if (pkNames.length == 0) {
return false;
}
for (int i = 0; i < pkNames.length; i++) {
pkNames[i] = pkNames[i].toUpperCase();
}
strTableName = tempClassName.substring(
tempClassName.lastIndexOf(".") + 1, tempClassName.length());
int m = ObjectTool.getFieldNamesAndTypes(rowValue, tempColumnNames,
tempColumnTypes);
if (m == 0) {
return false;
}
try {
////生成set语句
tempObject = tempClass.newInstance();
parameterClassArray[0] = rowValue.getClass();
for (int i = 0; i < m; i++) {
tempColumnName = tempColumnNames[i];
pkFlag = false;
for (int j = 0; j < pkNames.length; j++) {
tempPKName = pkNames[j];
if (tempColumnName.equals(tempPKName)) {
pkFlag = true;
}
}
if (!pkFlag) { //主键名和值不放在set语句中
tempMethodNames[i] = "get" + tempColumnName;
hasMethodName="has"+tempColumnName;
boolean flag=true;
try{//判断是否有has方法,如果有判断是否为true,只有为true时才可更新
hasMethod=tempClass.getDeclaredMethod(hasMethodName,null);
flag=((java.lang.Boolean)hasMethod.invoke(rowValue,null)).booleanValue();
}catch(Exception e){
}
tempMethod = tempClass.getDeclaredMethod(
tempMethodNames[i], null);
paramenterValues[0] = null;
String tempValue = ObjectTool.getObjectOpStr(tempMethod
.invoke(rowValue, null));
if (tempValue != null&&flag) { //去除null
if (!(tempValue.equals("null"))) {
strSet = strSet + tempColumnName + "=" + tempValue
+ ",";
}
}
}
}
// 生成where 语句
for (int i = 0; i < pkNames.length; i++) {
tempMethodName = "get" + pkNames[i];
tempMethod = tempClass.getDeclaredMethod(tempMethodName, null);
if (tempMethod.invoke(rowValue, null) != null) {
strWhere = strWhere
+ pkNames[i]
+ " = "
+ ObjectTool.getObjectOpStr(tempMethod.invoke(
rowValue, null)) + " and ";
} else {
return false;
}
}
} catch (Exception ex) {
throw new ReflectException(this.getClass().getName(), "update", ex);
}
strWhere = strWhere.substring(0, strWhere.length() - 5);
strTableName = strTableName.toLowerCase();
if(!strSet.equals("")){
strSql = "update " + strTableName + " set "
+ strSet.substring(0, strSet.length() - 1);
strSql = strSql + " where " + strWhere;
LogManager.log(LogManager.LOGTYPE_DEBUG, "strSql=" + strSql);
Statement stmt = null;
try {
stmt = dbconn.getStatement();
stmt.execute(strSql);
stmt.close();
return true;
}
catch (Exception ex) {
try {
stmt.close();
}
catch (Exception e) {
}
throw new DbException(this.getClass().getName(), strSql, ex);
}
}
return false;
}
/**
* select function
*
* @param conn:connection
* object,already open
* @param pkNames:primary
* key name Array
* @param rowValue:object
* include primary key value
* @return:ture if success execute,i have put all the return values in
* rowValue(you input)
*/
public ArrayList select(DbConnection dbconn, String[] pkNames,
Object rowValue) throws ApplicationException {
String strColumnNameList = "";
Field[] tempFields;
ArrayList returnList = new ArrayList();
String pkMethodName;
String strWhere = "";
ResultSet res = null;
ResultSetMetaData rsmd;
tempClass = rowValue.getClass();
tempClassName = tempClass.getName();
tempFields = tempClass.getDeclaredFields();
String tempPKName = null;
if (pkNames.length == 0) {
return returnList;
}
for (int i = 0; i < pkNames.length; i++) {
pkNames[i] = pkNames[i].toUpperCase();
}
//表名
strTableName = tempClassName.substring(
tempClassName.lastIndexOf(".") + 1, tempClassName.length());
//生成检索列
int columnNumber = ObjectTool.getFieldNamesAndTypes(rowValue,
tempColumnNames, tempColumnTypes);
if (columnNumber == 0) {
return returnList;
}
Statement stmt = null;
try {
// Connection conn = dbconn.getConnection();
tempObject = tempClass.newInstance();
parameterClassArray[0] = rowValue.getClass();
for (int i = 0; i < columnNumber; i++) {
tempColumnName = tempColumnNames[i];
strColumnNameList = strColumnNameList + tempColumnName + ",";
}
strColumnNameList = strColumnNameList.substring(0,
strColumnNameList.length() - 1);
//生成条件语句
for (int i = 0; i < pkNames.length; i++) {
tempPKName = pkNames[i];
pkMethodName = "get" + tempPKName;
tempMethod = tempClass.getDeclaredMethod(pkMethodName, null);
if (tempMethod.invoke(rowValue, null) != null) {
strWhere = strWhere
+ tempPKName
+ " = "
+ ObjectTool.getObjectOpStr(tempMethod.invoke(
rowValue, null)) + " and ";
} else {
return returnList;
}
}
strWhere = strWhere.substring(0, strWhere.length() - 5);
//create sql string
strTableName = strTableName.toLowerCase();
strSql = "select " + strColumnNameList + " from " + strTableName
+ " where " + strWhere;
LogManager.log(LogManager.LOGTYPE_DEBUG, "strSql=" + strSql);
//执行查询
stmt = dbconn.getStatement();
res = stmt.executeQuery(strSql);
int count = 0;
//把查询结果设置到rowValue 中
while (res.next()) {
rsmd = res.getMetaData();
int cols = rsmd.getColumnCount();
String tempColumnName;
String tempMethodName;
//由于java传参是传值,所以要将原对象赋值就需要从第二个才开始建立新对象,
//不建立新对象,返回的数组集合都是一个对象
if (count > 0) {
rowValue = (rowValue.getClass()).newInstance();
}
count++;
for (int i = 1; i <= cols; i++) {
tempColumnName = rsmd.getColumnName(i);
tempMethodName = "set" + tempColumnName;
paramenterValues[0] = ObjectTool.getObjectByStr8859(res
.getObject(tempColumnName));
if (paramenterValues[0] != null) {
parameterClassArray[0] = paramenterValues[0].getClass();
String tempColumnType = tempColumnTypes[i - 1];
ObjectTool.ivokeSetMethod(rowValue,
parameterClassArray, paramenterValues,
tempColumnType, tempMethodName);
}
}
returnList.add(rowValue);
}
res.close();
stmt.close();
return returnList;
} catch (Exception ex) {
try {
res.close();
stmt.close();
} catch (Exception e) {
}
throw new DbException(this.getClass().getName(), "select", ex);
}
}
/**
* select function
*
* @param conn:connection
* object,already open
* @param strWhere
* 单表查询where字句
* @param rowValue:object
* include primary key value
* @return:ture if success execute,i have put all the return values in
* rowValue(you input)
*/
public java.util.ArrayList select(DbConnection dbconn, String strWhere,
Object rowValue) throws ApplicationException {
String strColumnNameList = "";
Field[] tempFields;
String pkMethodName;
ResultSet res = null;
ResultSetMetaData rsmd;
java.util.ArrayList objectArray = new java.util.ArrayList();
tempClass = rowValue.getClass();
tempClassName = tempClass.getName();
tempFields = tempClass.getDeclaredFields();
//表名
strTableName = tempClassName.substring(
tempClassName.lastIndexOf(".") + 1, tempClassName.length());
//生成检索列
int columnNumber = ObjectTool.getFieldNamesAndTypes(rowValue,
tempColumnNames, tempColumnTypes);
if (columnNumber == 0) {
return null;
}
Statement stmt = null;
try {
Connection conn = dbconn.getConnection();
tempObject = tempClass.newInstance();
parameterClassArray[0] = rowValue.getClass();
for (int i = 0; i < columnNumber; i++) {
tempColumnName = tempColumnNames[i];
strColumnNameList = strColumnNameList + tempColumnName + ",";
}
strColumnNameList = strColumnNameList.substring(0,
strColumnNameList.length() - 1);
//create sql string
strTableName = strTableName.toLowerCase();
strSql = "select " + strColumnNameList + " from " + strTableName;
if (strWhere != null) {
if (!"".equals(strWhere)) {
strSql += " where " + strWhere;
}
}
LogManager.log(LogManager.LOGTYPE_DEBUG, "strSql=" + strSql);
//执行查询
stmt = conn.createStatement();
res = stmt.executeQuery(strSql);
//把查询结果设置到rowValue 中
while (res.next()) {
rsmd = res.getMetaData();
int cols = rsmd.getColumnCount();
String tempColumnName;
String tempMethodName;
rowValue = tempClass.newInstance();
for (int i = 1; i <= cols; i++) {
tempColumnName = rsmd.getColumnName(i);
tempMethodName = "set" + tempColumnName;
paramenterValues[0] = ObjectTool.getObjectByStr8859(res
.getObject(tempColumnName));
if (paramenterValues[0] != null) {
parameterClassArray[0] = paramenterValues[0].getClass();
String tempColumnType = tempColumnTypes[i - 1];
ObjectTool.ivokeSetMethod(rowValue,
parameterClassArray, paramenterValues,
tempColumnType, tempMethodName);
}
}
objectArray.add(rowValue);
}
res.close();
stmt.close();
return objectArray;
} catch (Exception ex) {
try {
res.close();
stmt.close();
} catch (Exception e) {
}
throw new DbException(this.getClass().getName(), "select", ex);
}
}
/**
* select function
*
* @param conn:connection
* object,already open
* @param strWhere
* 多表联合查询where字句
* @param rowValue:object
* include primary key value
* @return:ture if success execute,i have put all the return values in
* rowValue(you input)
*/
public java.util.ArrayList select(DbConnection dbconn, String strWhere,
Object rowValue, String reftable) throws ApplicationException {
String strColumnNameList = "";
String tablenames = null;
Field[] tempFields;
String pkMethodName;
ResultSet res = null;
ResultSetMetaData rsmd;
java.util.ArrayList objectArray = new java.util.ArrayList();
tempClass = rowValue.getClass();
tempClassName = tempClass.getName();
tempFields = tempClass.getDeclaredFields();
//表名
strTableName = tempClassName.substring(
tempClassName.lastIndexOf(".") + 1, tempClassName.length());
if (reftable != null && !"".equals(reftable.trim()))
tablenames = strTableName + "," + reftable;
else
tablenames = strTableName;
//生成检索列
int columnNumber = ObjectTool.getFieldNamesAndTypes(rowValue,
tempColumnNames, tempColumnTypes);
if (columnNumber == 0) {
return null;
}
Statement stmt = null;
try {
Connection conn = dbconn.getConnection();
tempObject = tempClass.newInstance();
parameterClassArray[0] = rowValue.getClass();
for (int i = 0; i < columnNumber; i++) {
tempColumnName = tempColumnNames[i];
strColumnNameList = strColumnNameList + strTableName + "."
+ tempColumnName + ",";
}
strColumnNameList = strColumnNameList.substring(0,
strColumnNameList.length() - 1);
//create sql string
strSql = "select " + strColumnNameList + " from "
+ tablenames.toLowerCase();
if (strWhere != null) {
if (!"".equals(strWhere)) {
strSql += " where " + strWhere;
}
}
LogManager.log(LogManager.LOGTYPE_DEBUG, "strSql=" + strSql);
//执行查询
stmt = conn.createStatement();
res = stmt.executeQuery(strSql);
//把查询结果设置到rowValue 中
while (res.next()) {
rsmd = res.getMetaData();
int cols = rsmd.getColumnCount();
String tempColumnName;
String tempMethodName;
rowValue = tempClass.newInstance();
for (int i = 1; i <= cols; i++) {
tempColumnName = rsmd.getColumnName(i);
tempMethodName = "set" + tempColumnName;
paramenterValues[0] = ObjectTool.getObjectByStr8859(res
.getObject(tempColumnName));
if (paramenterValues[0] != null) {
parameterClassArray[0] = paramenterValues[0].getClass();
String tempColumnType = tempColumnTypes[i - 1];
ObjectTool.ivokeSetMethod(rowValue,
parameterClassArray, paramenterValues,
tempColumnType, tempMethodName);
}
}
objectArray.add(rowValue);
}
res.close();
stmt.close();
return objectArray;
} catch (Exception ex) {
try {
res.close();
stmt.close();
} catch (Exception e) {
}
throw new DbException(this.getClass().getName(), "select", ex);
}
}
/**
* get a identify key value from sequence
*
* @param conn:connection
* object,already open
* @param pkName:primary
* key name
* @param rowValue:object
* include primary key value
* @return:"-1" 失败 其它为主键值
*/
public synchronized String getCode(DbConnection dbconn, String pkName, Object rowValue)
throws ApplicationException {
//return getCode();
Field[] tempFields;
String pkMethodName;
pkName = pkName.toUpperCase();
ResultSet res = null;
ResultSetMetaData rsmd;
//sequence name
String strSeqName;
String tempColumnType = "";
String tableIndex = rowValue.getClass().getName()+pkName;
String code = (String)codeTable.get(tableIndex);
if (code != null){
code = (Integer.parseInt( code)+1) + "";
int len = 16 - code.length();
for (int i = 0; i < len - 1; i++) {
code = "0" + code;
}
codeTable.put(tableIndex,code);
//return code;
}
int m = ObjectTool.getFieldNamesAndTypes(rowValue, tempColumnNames,
tempColumnTypes);
if (m == 0) {
return "-1";
}
for (int i = 0; i < m; i++) {
if (pkName.equals(tempColumnNames[i])) {
tempColumnType = tempColumnTypes[i];
break;
}
}
tempClass = rowValue.getClass();
tempClassName = tempClass.getName();
tempFields = tempClass.getDeclaredFields();
strSeqName = tempClassName.substring(
tempClassName.lastIndexOf(".") + 1, tempClassName.length());
strSeqName = strSeqName.toLowerCase();
strSql = "select MAX(" + pkName + ")+1 as id from " + strSeqName;
strSql = strSql.toLowerCase();
Statement stmt = null;
try {
stmt = dbconn.getStatement();
res = stmt.executeQuery(strSql);
//把查询结果设置到rowValue 中
res.next();
Object pkValue = res.getObject("id");
if (pkValue == null) {
pkValue = new String("1");
LogManager.log(LogManager.LOGTYPE_DEBUG, "getCode 取得主键类型"
+ pkValue.getClass().getName());
}
if ("java.lang.String".equals(tempColumnType)) {
//@todo 需要给串型主键找到新的主键生成器
//前面加入零,保证排序准确,共长15位
int len = 16 - (pkValue + "").length();
for (int i = 0; i < len - 1; i++) {
pkValue = "0" + pkValue;
}
//pkValue = "1" + pkValue;
}
String tempMethodName = "set" + pkName;
paramenterValues[0] = pkValue;
ObjectTool.ivokeSetMethod(rowValue, parameterClassArray,
paramenterValues, tempColumnType, tempMethodName);
res.close();
stmt.close();
if(code == null){
code = pkValue.toString();
}else if(Integer.parseInt(code)<Integer.parseInt(pkValue.toString())){
code = pkValue.toString();
}
codeTable.put(tableIndex,code);
return code;
} catch (SQLException ex) {
try {
res.close();
stmt.close();
} catch (Exception e) {
}
throw new DbException(this.getClass().getName(), "getCode", ex);
} catch (ApplicationException ex) {
throw new DbException(this.getClass().getName(), "getCode", ex);
}
}
/**
* get a identify key value from sequence
*
* @param conn:connection
* object,already open
* @param pkName:primary
* key name
* @param rowValue:object
* include primary key value
* @return:"-1" 失败 其它为主键值
*/
public String getCode(DbConnection dbconn) throws ApplicationException {
return getCode();
/*
* String strSql
= "select MAX(PKID)+1 as id from pkfactory";
//执行查询
* String pkValue = null; boolean transactionStatus =
* dbconn.isTransactionStatus(); Statement stmt = null; ResultSet res =
* null; try { if (!transactionStatus) { dbconn.beginTransaction(); }
* stmt = dbconn.getStatement(); res = stmt.executeQuery(strSql);
* //把查询结果设置到
rowValue 中 res.next(); pkValue = res.getString
("id");
* res.close(); if (pkValue == null) { pkValue = new String("1"); strSql =
* "insert into pkfactory values (1)"; } else { strSql = "update
* pkfactory set PKID=PKID+1"; } stmt.executeUpdate(strSql);
* stmt.close(); if (!transactionStatus) { dbconn.commit(); } return
* pkValue; } catch (SQLException ex) { try { res.close(); stmt.close(); }
* catch (Exception e) {} throw new GetCodeException(ex); } catch
* (ApplicationException ex) { throw ex; //new GetCodeException(ex); }
*/
}
/**
* get a identify key value from sequence
*
* @return:"-1" 失败 其它为主键值
*/
public static String getCode() {
return new String(Hex.encodeHex(org.apache.commons.id.uuid.UUID
.randomUUID().getRawBytes()));
/*
* 采用apache组织的common
-sandbox-id.jar生成uuid原方法有重复现象 Calendar noww =
* Calendar.getInstance(); String tempInt =
* String.valueOf(randomSeed.nextInt(9999)); String tempStringg; String
* tempYear = String.valueOf(noww.get(Calendar.YEAR)); String tempMonth =
* String.valueOf(noww.get(Calendar.MONTH) + 1); if (tempMonth.length() ==
* 1) { tempMonth = "0" + tempMonth; } String tempDay =
* String.valueOf(noww.get(Calendar.DAY_OF_MONTH)); if (tempDay.length() ==
* 1) { tempDay = "0" + tempDay; } String tempHour =
* String.valueOf(noww.get(Calendar.HOUR_OF_DAY)); if (tempHour.length() ==
* 1) { tempHour = "0" + tempHour; } String tempMinute =
* String.valueOf(noww.get(Calendar.MINUTE)); if (tempMinute.length() ==
* 1) { tempMinute = "0" + tempMinute; } String tmpSecond =
* String.valueOf(noww.get(Calendar.MILLISECOND)); if
* (tmpSecond.length() == 1) { tmpSecond = "0" + tmpSecond; } String
* tempMillinSecond = String.valueOf(noww.get(Calendar.MILLISECOND)); if
* (tempMillinSecond.length() == 1) { tempMillinSecond = "00" +
* tempMillinSecond; } else if (tempMillinSecond.length() == 2) {
* tempMillinSecond = "0" + tempMillinSecond; } tempStringg = tempYear +
* tempMonth + tempDay + tempHour + tempMinute + tempMillinSecond +
* tempInt; return Long.toHexString(Long.parseLong(tempStringg));
*/
}
public static void main(String[] args) {
DataAccess da = new DataAccess();
try {
System.out.println(da.getCode());
} catch (Exception ex) {
System.out.print("wrong");
}
}
}