??
項目里對框架的要求除了功能,擴展性,性能等方面的要求外還有對項目開發周期與新手學習周期和難易度的考慮。由于項目開發時間壓力很大,而且項目組員對
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");
????????}
????}
}