Here is a simplistic example for reading xml-file into WebRowSet object and then loading data from it to database.
import javax.sql.rowset.RowSetProvider;
import javax.sql.rowset.WebRowSet;
import javax.sql.rowset.spi.SyncProviderException;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class WebRowSetTest {
public static void main(String[] args) throws SQLException {
// Create WebRowSet object and populate it with data from xml-file.
WebRowSet receiver = RowSetProvider.newFactory().createWebRowSet();
Path file = Paths.get("priceList.xml");
try (InputStream in = Files.newInputStream(file)) {
receiver.readXml(in);
} catch (IOException x) {
x.printStackTrace();
}
System.out.println("WebRowSet deserialiazed.");
// Establish connection with database
String connectionURL = "jdbc:mysql://localhost:3306/testdb";
Properties connectionProps = new Properties();
connectionProps.put("user", "root");
connectionProps.put("password", "1234");
connectionProps.put("serverTimezone", "Europe/Moscow");
Connection conn = DriverManager.getConnection(connectionURL, connectionProps);
conn.setAutoCommit(false);
// Load data from WebRowSet object to database.
try {
receiver.acceptChanges(conn);
} catch (SyncProviderException spe) {
System.out.println("You need specify how to resolve the conflict.");
}
// Close connection.
conn.close();
}
}
There is another method for reading xml-file which uses Reader instead InputStream. So, I could replace the lines of code for reading xml-file into WebRowSet with something like this:
FileReader fReader = null;
try {
fReader = new FileReader("priceList.xml");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
receiver.readXml(fReader);
But isn't that true, that reading xml-file into WebRowSet object using InputStream is faster than using Reader? If so, then what is the purpose of readXml(Reader reader) in this case?