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,
where filename is the name ( without .xml extension ) of XML file which is used as database. If file do not exist , driver will create one.

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.

CREATE TABLE sample ( firstName varchar(20) primarykey , lastName varchar(20) not null , business varchar(20) , pay varchar(20) )

You can INSERT values into table on standard way.If you try insert duplicate value for primary key driver will throw SQLException.
Also will throw SQLException if you try to insert NULL value into column which is defined to be NOT NULL.

INSERT INTO sample ( firstName , lastName , business , pay ) VALUES ( 'Mike' , 'Johnson' , 'driver' , 100.50 )

You can UPDATE values into table on standard way.WHERE statement work with AND only.
OR is not supported.If you try insert duplicate value for primary key driver will throw SQLException.
Also will throw SQLException if you try to insert NULL value into column which is defined to be NOT NULL.

UPDATE sample SET pay=2000 , firstName='Willson' WHERE firstName='Mike' AND lastName='Johnson'

SELECT statement support WHERE only with AND , not with OR.You can use '*' instead of column names if you want to select all columns.

SELECT firstName , lastName , pay FROM sample WHERE pay='100.50' AND lastName='Johnson'

DELETE is supported also.For WHERE clause is same as mention above.

DELETE FROM sample WHERE firstName='John' AND lastName='Johnson' AND pay='100.50'

Deleting table is supported with DROP TABLE clause.

DROP TABLE sample

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.
Format of XML file is shown in example.
<database> is root tag.
<ddl> is tag where CREATE TABLE clause is saved.If CREATE TABLE clause exist for
some table it is used by driver to find out which column table has,and which column is
primary key and which column can't be null.
In case of exist CREATE TABLE clause column which have null value is not
shown as empty tag ( because driver can find out which columns exist in table ).
In case of missing CREATE TABLE clause, driver would not perform PRIMARYKEY or NOT NULL check ,
and column with 'null' value will be present as empty tag.
In there are no CREATE TABLE clause, driver will try to find first row in xml file, and
will find out which columns table has.
Fields which have 'null' value will be present as empty tag, and method getString() will return
"null" String.
If you want insert 'null' value in some field, insert "null" in that field
<dml> tag contain all data in database.
Every database row start with table name tag which have child nodes
which are column names with values.
XmlConnection class implements autoCommit(boolean arg) method. If autoCommit is set to true, which is
default value, after every call to execute(), XML file will be saved. This is slow variant, because saving file
in file system is slow.
Driver work much faster if autoCommit is set to false.
In this case, XML file will be saved when you call method commit() on object of class XmlConnection.
This is much faster.
Because we use regular expresions, if you want to use '=', '@', '/' charcacters, this characters
will be inserted in database as escape array of characters( '=' is ~EQUAL~, '@' is ~AT~, '/' is ~SLASH~ ).
Comma(,) must be between single(') or double quotes(").
Single quote can be between double quotes("), or as double occurence.
Double quote can be inserted using double occurence.

Example usage:

INSERT INTO sample ( firstName, lastName ) VALUES ( "Some,name'with''special,chars" , 'last""name,with''special,chars')

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>