XmlJdbc - a JDBC driver for XML files |
About XmlJdbc is a simple JDBC driver that uses XML files as database tables. |
How is it used The XmlJDBC driver is used just like any other JDBC driver. The xmljdbc.jar and xmlutil.jar files should be included in your application's classpath. The driver class is org.webdocwf.util.xml.XmlDriver
The connection URL is jdbc:webdocwf:xml:filename, This example code shows how the driver is used. import java.sql.*; public class DemoDriver { public static void main(String[] args) { try { // load the driver into memory Class.forName("org.webdocwf.util.xml.XmlDriver"); // create a connection. The first command line parameter is assumed to // be the XML file Connection conn = DriverManager.getConnection("jdbc:webdocwf:xml:" + args[0] ); // create a Statement object to execute the query with Statement stmt = conn.createStatement(); // Select the ID and NAME columns from sample.csv ResultSet results = stmt.executeQuery("SELECT firstName , lastName FROM sample"); // dump out the results while (results.next()) { System.out.println("firstName= " + results.getString("firstName") + " lastName= " + results.getString("lastName")); // index columns with number System.out.println("firstName= " + results.getString(1) + " lastName= " + results.getString(2)); } // clean up results.close(); stmt.close(); // XmlDriver also support PreparedStatements. // PreparedStatement.setString() and PreparedStatement.setBytes() are methods that can be used. // PreparedStatement.getString() and PreparedStatement.getBytes() are methods that can be used. // // setBytes and getBytes are used to work with binary objects // // create PreparedStatement PreparedStatement pstmt = conn.prepareStatement("INSERT INTO sample ( ID, NAME ) VALUES (?, ?) "); pstmt.setString(1, "1"); pstmt.setString(2, "somename" ); pstmt.executeUpdate(); pstmt.clearParameters(); pstmt.close(); conn.close(); } catch(Exception e) { System.out.println("Oops-> " + e); } } } |
Supported SQL statements The driver supports only simple SQL statements , and not support advanced statements.
Examples:
You can define PRIMARYKEY on some column.Also you can define NOT NULL columns on standard way. You can INSERT values into table on standard way.If you try insert duplicate value for
primary key driver will throw SQLException. You can UPDATE values into table on standard way.WHERE statement work with AND only. SELECT statement support WHERE only with AND , not with OR.You can use '*' instead of
column names if you want to select all columns. DELETE is supported also.For WHERE clause is same as mention above. Deleting table is supported with DROP TABLE clause. |
Format of XML file used as database
Encoding used in XML file is 'ISO-8859-1',which is "Latin-1" the Western European and English language character set. Example database : <database> <ddl>/* Some option comment */ CREATE TABLE sample ( firstName varchar(20) primarykey , lastName varchar(20) not null , business varchar(20) , pay varchar(20) ) </ddl> <ddl> CREATE TABLE sample2 ( firstName varchar(20) primarykey ) </ddl> <dml> <SAMPLE> <FIRSTNAME>John</FIRSTNAME> <LASTNAME>Johnson</LASTNAME> <PAY>100</PAY> </SAMPLE> <SAMPLE> <FIRSTNAME>Mike</FIRSTNAME> <LASTNAME>Wilson</LASTNAME> <PAY>100</PAY> </SAMPLE> /* Table which hasn't CREATE TABLE clause */ <TABLE1> <NAME>SomeName</NAME> <CITY></CITY> </TABLE1> </dml> </database> |