0

我有testWebRowset()一个webRowset对象,它创建一个 xml,填充它并操作一些数据,然后它尝试读取 xml 文件。绝对不会添加或删除列...之后它会创建另一个WebRowSet尝试读取此 xml 的实例。在这一点上,该方法readXml()抛出一个 SQLException 说“无效的列索引”,我只是看不出这是什么原因。

你能帮我弄清楚吗?谢谢。

编码:

public void testWebRowSet() throws SQLException, IOException {
    FileReader fReader;
    FileWriter fWriter;
    String priceListFileName = "pricelist.xml";
    int[] keyCols = {1};
    WebRowSet priceList = new WebRowSetImpl();

    priceList.setUsername("tom");
    priceList.setPassword("mypassword");
    priceList.setUrl("jdbc:derby://localhost:1527/TestDatabase/");
    priceList.setCommand("select * from item");
    priceList.setKeyColumns(keyCols);

    // Populate the WebRowSet
    priceList.execute();
    System.out.println("Size of the WebRowSet is: " + priceList.size());

    // Insert a new row
    priceList.moveToInsertRow();
    priceList.updateString("ITEMID", "S1002");
    priceList.updateString("NAME", "Scanner");
    priceList.updateFloat("PRICE", 8.99f);
    priceList.insertRow();
    priceList.moveToCurrentRow();
    System.out.println("New row inserted");
    System.out.println("Size of the WebRowSet is: " + priceList.size());

    //Delete the row with "Scanner"
    priceList.beforeFirst();
    while(priceList.next()) {
        if(priceList.getString(2).equals("Scanner")) {
            System.out.println("Deleting row with Scanner...");
            priceList.deleteRow();
            break;
        }
    }

    // Update price of Printer
    priceList.beforeFirst();
    while(priceList.next()) {
        if(priceList.getString(2).equals("Printer")) {
            System.out.println("Updating row with Printer...");
            priceList.updateFloat(3, 6.99f);
            priceList.updateRow();
            break;
        }
    }

    int size1 = priceList.size();
    fWriter = new FileWriter(priceListFileName);
    priceList.writeXml(fWriter);
    fWriter.flush();
    fWriter.close();

    // Create the receiving WebRowSet object
    WebRowSet receiver = new WebRowSetImpl();
    receiver.setUrl("jdbc:derby://localhost:1527/TestDatabase/");
    receiver.setUsername("tom");
    receiver.setPassword("mypassword");

    //Now read the XML file.
    fReader = new FileReader(priceListFileName);
    receiver.readXml(fReader);
    int size2 = receiver.size();
    if(size1 == size2) {
        System.out.println("WebRowSet serialized and "
                + "deserialiazed properly");
    } else {
        System.out.println("Error....serializing/deserializng the WebRowSet");
    }
    priceList.acceptChanges();
}

堆栈跟踪:

org.xml.sax.SAXException: Error inserting values : Invalid column index
    at com.sun.rowset.internal.XmlReaderContentHandler.endElement(XmlReaderContentHandler.java:741)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:606)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.endElement(XMLSchemaValidator.java:858)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1742)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2900)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:607)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:116)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:489)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:835)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:123)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1210)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:568)
    at com.sun.rowset.internal.WebRowSetXmlReader.readXML(WebRowSetXmlReader.java:109)
    at com.sun.rowset.WebRowSetImpl.readXml(WebRowSetImpl.java:182)
    at DB.DB.testWebRowSet(DB.java:175)
    at DB.DBtest.main(DBtest.java:28)
Exception in thread "main" java.sql.SQLException: Error inserting values : Invalid column index
    at com.sun.rowset.WebRowSetImpl.readXml(WebRowSetImpl.java:200)
    at DB.DB.testWebRowSet(DB.java:175)
    at DB.DBtest.main(DBtest.java:28)
Java Result: 1

XML 文件:

<?xml version="1.0"?>
<webRowSet xmlns="http://java.sun.com/xml/ns/jdbc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/jdbc http://java.sun.com/xml/ns/jdbc/webrowset.xsd">
  <properties>
    <command>select * from item</command>
    <concurrency>1008</concurrency>
    <datasource><null/></datasource>
    <escape-processing>true</escape-processing>
    <fetch-direction>1000</fetch-direction>
    <fetch-size>0</fetch-size>
    <isolation-level>2</isolation-level>
    <key-columns>
      <column>1</column>
    </key-columns>
    <map>
    </map>
    <max-field-size>0</max-field-size>
    <max-rows>0</max-rows>
    <query-timeout>0</query-timeout>
    <read-only>true</read-only>
    <rowset-type>ResultSet.TYPE_SCROLL_INSENSITIVE</rowset-type>
    <show-deleted>false</show-deleted>
    <table-name>item</table-name>
    <url>jdbc:derby://localhost:1527/TestDatabase/</url>
    <sync-provider>
      <sync-provider-name>com.sun.rowset.providers.RIOptimisticProvider</sync-provider-name>
      <sync-provider-vendor>Oracle Corporation</sync-provider-vendor>
      <sync-provider-version>1.0</sync-provider-version>
      <sync-provider-grade>2</sync-provider-grade>
      <data-source-lock>1</data-source-lock>
    </sync-provider>
  </properties>
  <metadata>
    <column-count>4</column-count>
    <column-definition>
      <column-index>1</column-index>
      <auto-increment>false</auto-increment>
      <case-sensitive>true</case-sensitive>
      <currency>false</currency>
      <nullable>1</nullable>
      <signed>false</signed>
      <searchable>true</searchable>
      <column-display-size>6</column-display-size>
      <column-label>ITEMID</column-label>
      <column-name>ITEMID</column-name>
      <schema-name>TOM</schema-name>
      <column-precision>6</column-precision>
      <column-scale>0</column-scale>
      <table-name>ITEM</table-name>
      <catalog-name></catalog-name>
      <column-type>12</column-type>
      <column-type-name>VARCHAR</column-type-name>
    </column-definition>
    <column-definition>
      <column-index>2</column-index>
      <auto-increment>false</auto-increment>
      <case-sensitive>true</case-sensitive>
      <currency>false</currency>
      <nullable>1</nullable>
      <signed>false</signed>
      <searchable>true</searchable>
      <column-display-size>15</column-display-size>
      <column-label>NAME</column-label>
      <column-name>NAME</column-name>
      <schema-name>TOM</schema-name>
      <column-precision>15</column-precision>
      <column-scale>0</column-scale>
      <table-name>ITEM</table-name>
      <catalog-name></catalog-name>
      <column-type>12</column-type>
      <column-type-name>VARCHAR</column-type-name>
    </column-definition>
    <column-definition>
      <column-index>3</column-index>
      <auto-increment>false</auto-increment>
      <case-sensitive>false</case-sensitive>
      <currency>false</currency>
      <nullable>1</nullable>
      <signed>true</signed>
      <searchable>true</searchable>
      <column-display-size>22</column-display-size>
      <column-label>PRICE</column-label>
      <column-name>PRICE</column-name>
      <schema-name>TOM</schema-name>
      <column-precision>15</column-precision>
      <column-scale>0</column-scale>
      <table-name>ITEM</table-name>
      <catalog-name></catalog-name>
      <column-type>8</column-type>
      <column-type-name>DOUBLE</column-type-name>
    </column-definition>
    <column-definition>
      <column-index>4</column-index>
      <auto-increment>false</auto-increment>
      <case-sensitive>false</case-sensitive>
      <currency>false</currency>
      <nullable>1</nullable>
      <signed>true</signed>
      <searchable>true</searchable>
      <column-display-size>22</column-display-size>
      <column-label>QUANTITY</column-label>
      <column-name>QUANTITY</column-name>
      <schema-name>TOM</schema-name>
      <column-precision>15</column-precision>
      <column-scale>0</column-scale>
      <table-name>ITEM</table-name>
      <catalog-name></catalog-name>
      <column-type>8</column-type>
      <column-type-name>DOUBLE</column-type-name>
    </column-definition>
  </metadata>
  <data>
    <modifyRow>
      <columnValue>S1002</columnValue>
      <columnValue>Scanner</columnValue>
      <columnValue>8.99</columnValue>
      <columnValue><null/></columnValue>
    </modifyRow>
    <currentRow>
      <columnValue>001</columnValue>
      <columnValue>Item 01</columnValue>
      <columnValue>2000.0</columnValue>
      <columnValue>20.0</columnValue>
    </currentRow>
    <currentRow>
      <columnValue>002</columnValue>
      <columnValue>Item 02</columnValue>
      <columnValue>1000.0</columnValue>
      <columnValue>5.0</columnValue>
    </currentRow>
    <currentRow>
      <columnValue>003</columnValue>
      <columnValue>Item 03</columnValue>
      <columnValue>20.2</columnValue>
      <columnValue>25.0</columnValue>
    </currentRow>
    <currentRow>
      <columnValue>004</columnValue>
      <columnValue>Item 04</columnValue>
      <columnValue>1500.0</columnValue>
      <columnValue>40.0</columnValue>
    </currentRow>
    <currentRow>
      <columnValue>005</columnValue>
      <columnValue>Item 05</columnValue>
      <columnValue>1600.0</columnValue>
      <columnValue>40.0</columnValue>
    </currentRow>
    <currentRow>
      <columnValue>006</columnValue>
      <columnValue>Item 06</columnValue>
      <columnValue>1700.0</columnValue>
      <columnValue>45.0</columnValue>
    </currentRow>
    <currentRow>
      <columnValue>007</columnValue>
      <columnValue>Item 07</columnValue>
      <columnValue>1800.0</columnValue>
      <columnValue>1200.0</columnValue>
    </currentRow>
    <currentRow>
      <columnValue>P1002</columnValue>
      <columnValue>Canon Printer</columnValue>
      <columnValue>60.2</columnValue>
      <columnValue>9.0</columnValue>
    </currentRow>
  </data>
</webRowSet>
4

0 回答 0