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

📄 dbdatawriter.java

📁 利用java反射机制
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            }else if (requestor.getOptimisticLock() == requestor.KEY_AND_MODIFIED_COLUMNS){                for (int i = 1;i <= requestor.getMetaData().getColumnCount();i++){                    String catalog = requestor.getMetaData().getCatalogName(i);                    String schema = requestor.getMetaData().getSchemaName(i);                    String table = requestor.getMetaData().getTableName(i);                    if (modifiedCatalog != null){                        if (!modifiedCatalog.equals(catalog)){                            continue;                        }                    }                    if (modifiedSchema != null){                        if (!modifiedSchema.equals(schema)){                            continue;                        }                    }                    if (modifiedTable != null){                        if (!modifiedTable.equals(table)){                            continue;                        }                    }                    String columnName = requestor.getMetaData().getFullColumnName(i);                    if (where.indexOf(columnName) > -1){                        continue;// this is PK column - proccesed already                    }                    if (!requestor.getMetaData().isWritable(i) || requestor.getMetaData().isReadOnly(i)){                        continue;                    }                    fieldName = requestor.getMetaData().getFieldName(i);                    Object value = requestor.getMetaData().getReadMethod(i).invoke(domainObject, readArg);                    Object origValue = domainObject.getOriginalValue(fieldName);                    if (origValue == null){                        if (value == null){                            continue;                        }else{                            where.append(columnName).append(" IS NULL AND ");                        }                    }else{                        if (value == null){                            where.append(columnName).append(" = ? AND ");                            params.add(origValue);                        }else{                            if (origValue.equals(value)){                                continue;                            }else{                                where.append(columnName).append(" = ? AND ");                                params.add(origValue);                            }                        }                    }                }            }else if (requestor.getOptimisticLock() == requestor.KEY_AND_UPDATEBLE_COLUMNS){                for (int i = 1;i <= requestor.getMetaData().getColumnCount();i++){                    String catalog = requestor.getMetaData().getCatalogName(i);                    String schema = requestor.getMetaData().getSchemaName(i);                    String table = requestor.getMetaData().getTableName(i);                    if (modifiedCatalog != null){                        if (!modifiedCatalog.equals(catalog)){                            continue;                        }                    }                    if (modifiedSchema != null){                        if (!modifiedSchema.equals(schema)){                            continue;                        }                    }                    if (modifiedTable != null){                        if (!modifiedTable.equals(table)){                            continue;                        }                    }                         String columnName = requestor.getMetaData().getFullColumnName(i);                                        if (where.indexOf(columnName) > -1){                        continue;// this is PK column - proccesed already                    }                    if (!requestor.getMetaData().isWritable(i) || requestor.getMetaData().isReadOnly(i)){                        continue;                    }                    fieldName = requestor.getMetaData().getFieldName(i);                    Object value = requestor.getMetaData().getReadMethod(i).invoke(domainObject, readArg);                                        Object origValue = domainObject.getOriginalValue(fieldName);                    if (origValue == null){                        where.append(columnName).append(" IS NULL AND ");                    }else{                        where.append(columnName).append(" = ? AND ");                        params.add(origValue);                    }                                    }            }else{                throw new IllegalArgumentException("Invalid Optimistic Lock settings");            }            if (debug) System.out.println("julp ============= " + new java.util.Date() + " " + this.getClass() + "::storeObject::where 1 : " + where);            if (debug) System.out.println("julp ============= " + new java.util.Date() + " " + this.getClass() + "::storeObject::requestor.KEY_COLUMNS 1 : " + requestor.KEY_COLUMNS);            if (requestor.getOptimisticLock() != requestor.KEY_COLUMNS){                idx = where.length();                where = where.delete(idx - 5, idx - 1);            }            if (debug) System.out.println("julp ============= " + new java.util.Date() + " " + this.getClass() + "::storeObject::where 2 : " + where);            sql.append(where);            if (debug) System.out.println("julp ============= " + new java.util.Date() + " " + this.getClass() + "::storeObject::storeObject: " + sql + "; params: " + params);            if (requestor.isGenerateSQLOnly()){                if (generatedSQL == null){                    generatedSQL = new ArrayList();                }                Object[] entry = new Object[2];                entry[0] = sql.toString();                entry[1] = params.toArray();                generatedSQL.add(entry);            }else{                int rowsAffected = requestor.getDBServices().execute(sql.toString(), params);                setModifiedCount(modifiedCount + rowsAffected);                if (requestor.isThrowOptimisticLockUpdateException() && rowsAffected != 1){                    if (debug) System.out.println("julp ============= " + new java.util.Date() + " " + this.getClass() + "::storeObject::rowsAffected: " + rowsAffected);                    return false;                }            }        }catch(Exception e){            setWhatIsWrong(e);            e.printStackTrace();            if (debug) System.out.println("julp ============= " + new java.util.Date() + " " + this.getClass() + "::storeObject::Exception in storeObject: " + e);            return false;        }        return true;    }        protected boolean createObject(DomainObject domainObject){        String fieldName = null;        List params = new ArrayList();        try{            StringBuffer sql = new StringBuffer("INSERT INTO ");            sql.append(fullTableName);            sql.append(" (");            for (int i = 1;i <= requestor.getMetaData().getColumnCount();i++){                String catalog = requestor.getMetaData().getCatalogName(i);                String schema = requestor.getMetaData().getSchemaName(i);                String table = requestor.getMetaData().getTableName(i);                if (modifiedCatalog != null){                    if (!modifiedCatalog.equals(catalog)){                        continue;                    }                }                if (modifiedSchema != null){                    if (!modifiedSchema.equals(schema)){                        continue;                    }                }                if (modifiedTable != null){                    if (!modifiedTable.equals(table)){                        continue;                    }                }                                if (!requestor.getMetaData().isWritable(i) || requestor.getMetaData().isReadOnly(i)){                    continue;                }                String columnName = null;                if (requestor.isNoFullColumnName()){                    columnName = requestor.getMetaData().getColumnName(i);                }else{                    columnName = requestor.getMetaData().getFullColumnName(i);                }                                                sql.append(columnName).append(", ");                Object value = requestor.getMetaData().getReadMethod(i).invoke(domainObject, readArg);                                params.add(value);            }            int idx = sql.lastIndexOf(", ");            sql = sql.delete(idx, idx + 2);            sql.append(") VALUES (");                        ListIterator li = params.listIterator();            while (li.hasNext()){                Object value = li.next();                if (value == null){                    sql.append("NULL, ");                    li.remove();                }else{                    sql.append("?, ");                }            }                        idx = sql.lastIndexOf(",");            sql = sql.delete(idx, idx + 2);            sql.append(")");                        if (requestor.isGenerateSQLOnly()){                if (generatedSQL == null){                    generatedSQL = new ArrayList();                }                Object[] entry = new Object[2];                entry[0] = sql.toString();                entry[1] = params.toArray();                generatedSQL.add(entry);            }else{                int rowsAffected = requestor.getDBServices().execute(sql.toString(), params);                if (rowsAffected != 1){                    throw new SQLException("INSERT INTO " + fullTableName + " has failed");                }                setCreatedCount(createdCount + rowsAffected);            }        }catch(Exception e){            setWhatIsWrong(e);            e.printStackTrace();            return false;        }        return true;    }        /** Getter for property removedCount.     * @return Value of property removedCount.     *     */    public int getRemovedCount() {        return removedCount;    }        /** Setter for property removedCount.     * @param removedCount New value of property removedCount.     *     */    protected void setRemovedCount(int removedCount) {        this.removedCount = removedCount;    }        /** Getter for property createdCount.     * @return Value of property createdCount.     *     */    public int getCreatedCount() {        return createdCount;    }        /** Setter for property createdCount.     * @param createdCount New value of property createdCount.     *     */    protected void setCreatedCount(int createdCount) {        this.createdCount = createdCount;    }        /** Getter for property modifiedCount.     * @return Value of property modifiedCount.     *     */    public int getModifiedCount() {        return modifiedCount;    }        /** Setter for property modifiedCount.     * @param modifiedCount New value of property modifiedCount.     *     */    protected void setModifiedCount(int modifiedCount) {        this.modifiedCount = modifiedCount;    }        public void reset(){        removedCount = 0;        createdCount = 0;        modifiedCount = 0;        objectList = null;        generatedSQL = null;        removedObjects = null;        createdObjects = null;        modifiedObjects = null;        metaData = null;        fullTableName = null;        modifiedTable = null;        modifiedSchema = null;        modifiedCatalog = null;        whatIsWrong = null;    }        /** Getter for property generatedSQL.     * @return Value of property generatedSQL.     *     */    public java.util.List getGeneratedSQL() {        return generatedSQL;    }        /** Getter for property whatIsWrong.     * @return Value of property whatIsWrong.     *     */    public java.lang.Throwable getWhatIsWrong() {        return whatIsWrong;    }        /** Setter for property whatIsWrong.     * @param whatIsWrong New value of property whatIsWrong.     *     */    public void setWhatIsWrong(java.lang.Throwable whatIsWrong) {        this.whatIsWrong = whatIsWrong;    }        /** Getter for property modifiedCatalog.     * @return Value of property modifiedCatalog.     *     */    public java.lang.String getModifiedCatalog() {        return modifiedCatalog;    }        /** Setter for property modifiedCatalog.     * @param modifiedCatalog New value of property modifiedCatalog.     *     */    public void setModifiedCatalog(java.lang.String modifiedCatalog) {        this.modifiedCatalog = modifiedCatalog;    }        /** Getter for property generatedSQLasXML.     * @return Value of property generatedSQLasXML.     *     */    public java.lang.String getGeneratedSQLasXML() {        if (this.generatedSQL == null || this.generatedSQL.isEmpty()){            return "";        }        StringBuffer sb = new StringBuffer("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n<generated-sql>\n");        Iterator it = this.generatedSQL.iterator();        while (it.hasNext()){            sb.append(" <sql>\n");            Object[] obj = (Object[]) it.next();            sb.append("  <statement><![CDATA[").append(obj[0]).append("]]></statement>\n");                        Object[] arg = (Object[]) obj[1];            if (arg != null && arg.length > 0){                sb.append("  <params>\n");                for (int i = 0;i < arg.length;i++){                    sb.append("   <param>\n");                    sb.append("    <value>").append("<![CDATA[").append(arg[i].toString()).append("]]>").append("</value>\n");                    sb.append("    <datatype>").append(arg[i=i].getClass()).append("</datatype>\n");                    sb.append("   </param>\n");                }                                sb.append("  </params>\n");            }            sb.append(" </sql>\n");                    }        sb.append("</generated-sql>\n");        return sb.toString();    }    }

⌨️ 快捷键说明

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