📄 importxmlworker.java
字号:
try { batchResult = getBatchResult(prepStmnt.executeBatch()); int result = batchResult[0]; tableInsertCount += result; commitCount += result; tableCommitCount = tableInsertCount; } catch (BatchUpdateException e) { logException(e); batchResult = getBatchResult(e.getUpdateCounts()); errorCount += batchResult[1]; outputBuffer.append("An error occured during the batch process: "); outputBuffer.append(e.getMessage()); SQLException _e = e.getNextException(); while (_e != null) { outputBuffer.append("\nNext Exception: "); outputBuffer.append(_e.getMessage()); _e = _e.getNextException(); } outputBuffer.append("\n\nRecords processed to "); outputBuffer.append("the point where this error occurred: "); outputBuffer.append(batchResult.length); appendProgressErrorText(outputBuffer); processResult = FAILED; if (haltOnError) { throw new SAXException(SAX_NO_PRINT_EXCEPTION); } } if (tableRowCount != tableInsertCount) { conn.rollback(); if (haltOnError) { getParent().cancelTransfer(); processResult = FAILED; throw new SAXException(); } } } if (cancelled) { throw new InterruptedException(); } // do the commit rollback size selected is end of file if (rollbackSize == ImportExportProcess.COMMIT_END_OF_FILE) { conn.commit(); totalInsertCount += commitCount; tableCommitCount = tableInsertCount; commitCount = 0; } // log some record progress figures printTableResult(tableRowCount, tableInsertCount, tableName); return; } // ----------------------------------------- // must be a column value if (!isSelectedColumn(localName)) { return; } int index = getColumnIndex(localName); if (index == -1) { if (localName.equals(lastStartElement)) { // if it doesn't exist in the insert table // check the column selections in cases of // single file import if (isIgnoredColumn(lastStartElement)) { return; } outputBuffer.append("The column "); outputBuffer.append(localName); outputBuffer.append(" specified within the "); outputBuffer.append("import\nfile does not "); outputBuffer.append("exist in the current table"); appendProgressErrorText(outputBuffer); processResult = FAILED; throw new SAXException(SAX_NO_PRINT_EXCEPTION); } else { return; } } String value = contents.toString(); if (value != null && value.trim().length() == 0) { value = null; } ColumnData cd = (ColumnData)columns.get(index); try { setValue(value, (index+1), cd.getSQLType(), false, df); // this is now a bound variable boundVariables.put(cd.getColumnName(), VARIABLE_BOUND); } catch (ParseException e) { errorCount++; processResult = FAILED; outputBuffer.append("Error parsing date value for column "); outputBuffer.append(cd.getColumnName()); outputExceptionError(null, e); throw new SAXException(SAX_NO_PRINT_EXCEPTION); } catch (NumberFormatException e) { errorCount++; processResult = FAILED; outputBuffer.append("Error parsing value for column "); outputBuffer.append(cd.getColumnName()); outputExceptionError(null, e); throw new SAXException(SAX_NO_PRINT_EXCEPTION); } } catch (InterruptedException e) { if (processResult != FAILED) { processResult = CANCELLED; } try { prepStmnt.cancel(); conn.rollback(); } catch (SQLException e2) { outputExceptionError("Error rolling back transaction", e); } throw new SAXException("Process interrupted"); } // catch if all else dumps catch (SQLException e) { logException(e); errorCount++; processResult = FAILED; outputExceptionError("Error importing table data from XML", e); if (haltOnError) { throw new SAXException(SAX_NO_PRINT_EXCEPTION); } } catch (Exception e) { logException(e); errorCount++; processResult = FAILED; outputExceptionError("Error importing table data to file", e); if (haltOnError) { throw new SAXException(SAX_NO_PRINT_EXCEPTION); } } } private int[] getBatchResult(int[] updateCount) { int insert = 0; int success = 0; int errors = 0; // annoying as becoming a little db specific, // but Oracle returns -2 on a successful batch // execution using prepared statement (-3 on error) ConnectionDataSource cds = (ConnectionDataSource) ConnectionManager.getDataSource(getParent().getDatabaseConnection()); if (cds.isUsingOracleThinDriver()) { success = -2; } else { success = 1; } for (int i = 0; i < updateCount.length; i++) { if (updateCount[i] == success) { insert++; } else { errors++; } } int[] result = {insert, errors}; return result; } /** * Evaluates whether the specified column is a selected * import column in cases of a single file import where * column selection is allowed. * * @param columnName - the column to be evaluated */ private boolean isSelectedColumn(String columnName) { // return true for a multi-table import if (parent.getTableTransferType() == ImportExportProcess.MULTIPLE_TABLE) { return true; } // check the cache first if (ignoredColumns != null) { if (ignoredColumns.containsKey(columnName)) { return false; } } // check the selected columns Vector<ColumnData> columns = parent.getSelectedColumns(); for (int i = 0, n = columns.size(); i < n; i++) { if (columns.get(i).getColumnName().equalsIgnoreCase(columnName)) { return true; } } return false; } /** * Evaluates whether the specified column is not a selected * import column in cases of a single file import where * column selection is allowed. * * @param columnName - the column to be evaluated */ private boolean isIgnoredColumn(String columnName) { // return false for a multi-table import if (parent.getTableTransferType() == ImportExportProcess.MULTIPLE_TABLE) { return false; } // check the cache first if (ignoredColumns != null) { if (ignoredColumns.containsKey(columnName)) { return true; } } // check the selected columns Vector<ColumnData> columns = parent.getSelectedColumns(); for (int i = 0, n = columns.size(); i < n; i++) { if (columns.get(i).getColumnName().equalsIgnoreCase(columnName)) { return false; } } // otherwise add to the ignored list if (ignoredColumns == null) { ignoredColumns = new HashMap<String,String>(); } ignoredColumns.put(columnName, columnName); return true; } private int getColumnIndex(String columnName) { for (int i = 0, n = columns.size(); i < n; i++) { ColumnData cd = columns.get(i); String _columnName = cd.getColumnName(); if (_columnName.equalsIgnoreCase(columnName)) { return i; } } return -1; } private String findTableName(String name) { for (int i = 0; i < tablesArray.length; i++) { if (name.equalsIgnoreCase(tablesArray[i])) { return tablesArray[i]; } } return null; } public void characters(char[] data, int start, int length) { contents.write(data, start, length); } public void ignorableWhitespace(char[] data, int start, int length) { characters(data, start, length); } public void error(SAXParseException spe) throws SAXException { throw new SAXException(spe.getMessage()); } } // ImportXMLHandler }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -