📄 importxmlworker.java
字号:
private static final char COMMA = ','; private static final String RECORDS = "\nRecords inserted: "; // the date format private String dateFormatString; // the current table's column meta-data private Vector<ColumnData> columns; // the ongoing rollback count private int rollbackCount = 0; // the table tag name private String tableTag; // the table tag attribute private String tableAtt; // the table identifier as shown in the XML file private String tableIdentifier; // the row identifier as shown in the XML file private String rowIdentifier; // denotes the first tag pass private boolean firstPass; // whether the row tag has been encountered private boolean rowTagFound; // denotes the second tag pass private int passes; // the date format for DATE type fields private SimpleDateFormat df; // whether the table name is an attribute private boolean hasTableAttribute; // whether this is to be run as a batch process private boolean isBatch; // whether we are parsing date formats private boolean parsingDates; private boolean importThisTable; // the table currently being processed private String tableName; // columns in the import file to be ignored private Map<String,String> ignoredColumns; // currently bound variables in the prepared statement private Map<String,String> boundVariables; public ImportXMLHandler() { dateFormatString = getParent().getDateFormat(); isBatch = getParent().runAsBatchProcess(); if (dateFormatString != null) { if (dateFormatString.length() == 0) { dateFormatString = null; } else { df = new SimpleDateFormat(dateFormatString); } } ImportExportXMLPanel _parent = (ImportExportXMLPanel)getParent(); hasTableAttribute = _parent.hasTableNameAsAttribute(); tableIdentifier = _parent.getTableIdentifier(); rowIdentifier = _parent.getRowIdentifier(); parsingDates = _parent.parseDateValues(); if (hasTableAttribute) { tableTag = (tableIdentifier.substring(0, tableIdentifier.indexOf(COMMA))).trim(); tableAtt = (tableIdentifier.substring( tableIdentifier.indexOf(COMMA)+1)).trim(); } else { tableTag = tableIdentifier; } firstPass = true; rowTagFound = false; passes = 0; } protected void reset() { firstPass = true; rowTagFound = false; passes = 0; tableName = null; lastStartElement = null; columns = null; } private String lastStartElement; public void startElement(String nameSpaceURI, String localName, String qName, Attributes attrs) throws SAXException { contents.reset(); passes++; lastStartElement = localName; if (tableTransferType == ImportExportProcess.MULTIPLE_TABLE && !hasTableAttribute) { if (fileFormat == ImportExportProcess.SINGLE_FILE) { tableTag = tablesArray[tableCount]; } else { tableTag = tablesArray[0]; } } // check if we have found the row tag (only if we haven't) if (!rowTagFound && localName.equalsIgnoreCase(rowIdentifier)) { rowTagFound = true; } if (localName.equalsIgnoreCase(tableTag)) { firstPass = false; rowTagFound = false; if (hasTableAttribute) { if (attrs.getIndex(tableAtt) == -1) { appendProgressErrorText( "The attribute name entered was not found.\nProcess is exiting."); processResult = FAILED; getParent().cancelTransfer(); throw new SAXException(SAX_NO_PRINT_EXCEPTION); } else { tableName = attrs.getValue(tableAtt); } } else { tableName = tableTag; } tableName = findTableName(tableName); // table is in file but not selected for import so skip if (tableName == null) { importThisTable = false; return; } else { importThisTable = true; } // reset table counters tableInsertCount = 0; tableCommitCount = 0; tableRowCount = 0; rollbackCount = 0; // increment the table count tableCount++; // retrieve the columns to be imported (or all) try { columns = getColumns(tableName); } catch (SQLException e) {} int columnCount = columns.size(); if (parsingDates && dateFormatString == null) { // check for a date data type boolean hasDateField = false; for (int j = 0; j < columnCount; j++) { if (dateFormatString == null) { ColumnData cd = (ColumnData)columns.get(j); int sqlType = cd.getSQLType(); if (sqlType == Types.DATE || sqlType == Types.TIME || sqlType == Types.TIMESTAMP) { hasDateField = true; break; } } } if (hasDateField && dateFormatString == null) { dateFormatString = verifyDate(); df = new SimpleDateFormat(dateFormatString); } } if (boundVariables == null) { boundVariables = new HashMap<String,String>(); } else { boundVariables.clear(); } outputBuffer.append("---------------------------\nTable: "); outputBuffer.append(tableName); outputBuffer.append("\nImport File: "); outputBuffer.append(currentImportFileName); appendProgressText(outputBuffer); // prepare the statement try { prepareStatement(tableName, columns); } catch (Exception e) { processResult = FAILED; outputExceptionError("Error preparing import SQL statement", e); throw new SAXException(SAX_NO_PRINT_EXCEPTION); } } // if we encounter a row tag but have not yet // encountered a table tag, display error else if (rowTagFound && firstPass) { appendProgressErrorText( "The table tag name entered was not found.\nProcess is exiting."); processResult = FAILED; getParent().cancelTransfer(); throw new SAXException(SAX_NO_PRINT_EXCEPTION); } // if we have not encountered a row tag and // this is after the second pass ie. schema and // table elements have been found, show error else if (passes > 2 && !rowTagFound) { appendProgressErrorText( "The XML tag elements for a table row as entered were not found.\n" + "Process is exiting."); processResult = FAILED; getParent().cancelTransfer(); throw new SAXException(SAX_NO_PRINT_EXCEPTION); } } private boolean cancelled; public void endElement(String nameSpaceURI, String localName, String qName) throws SAXException { if (!importThisTable) { return; } try { if (Thread.interrupted()) { cancelled = true; printTableResult(tableRowCount, tableCommitCount, tableName); throw new InterruptedException(); } // check if we have reached the end of a row tag if (localName.equalsIgnoreCase(rowIdentifier)) { // check all variables are bound - insert NULL otherwise for (int i = 0, n = columns.size(); i < n; i++) { ColumnData columnData = columns.get(i); String columnName = columnData.getColumnName(); Object bind = boundVariables.get(columnName); if (bind == VARIABLE_NOT_BOUND) { prepStmnt.setNull(i + 1, columnData.getSQLType()); } } try { tableRowCount++; rollbackCount++; totalRecordCount++; if (isBatch) { // add to batch if in batch mode prepStmnt.addBatch(); } else { // just execute otherwise int result = prepStmnt.executeUpdate(); tableInsertCount += result; commitCount += result; } if (rollbackCount == rollbackSize) { if (isBatch) { int result = getBatchResult(prepStmnt.executeBatch())[0]; tableInsertCount += result; commitCount += result; prepStmnt.clearBatch(); } conn.commit(); totalInsertCount += commitCount; tableCommitCount = tableInsertCount; rollbackCount = 0; commitCount = 0; } } 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); } } return; } // check if we have reached the end of a table tag else if (localName.equalsIgnoreCase(tableTag)) { if (isBatch) { int[] batchResult = null;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -