Java Reference
In-Depth Information
return xmlDoc;
}
// create an XML Document with the specified root tag
private Document createXmlDoc(String rootTag){
xmlDoc = new DocumentImpl();
Element root = (Element)xmlDoc.createElement(rootTag);
xmlDoc.appendChild (root);
return xmlDoc;
}
// methods to return an XMLStatement
public Statement createStatement(){
return new XMLStatement(xmlDoc);
}
public XMLStatement createStatement(URL url){
return new XMLStatement(xmlDoc);
}
}
In addition to connecting to a data source, the
Connection
object is also responsible for returning a
Statement
object when its
createStatement()
method is called. The
createStatement()
method creates a new
Statement
object, passing it the DOM document contained in the
Connection
.
The XMLStatement class
The
java.sql.Statement
object acts as a top-level command interpreter for
execute()
and
executeQuery()
methods. In this implementation, the simpler
CREATE
and
INSERT
commands are
handled locally, and queries are handled by an
XMLQuery
object.
The primary methods of the
Statement
object are:
public ResultSet executeQuery(String sqlQuery) - executeQuery()
creates a
new
XMLQuery
object, using it to process the SQL query. The
XMLQuery
object is illustrated in
Listing 19-10
.
public int executeUpdate(String sqlString) - executeUpdate()
creates a
new
XMLCommand
object, passing it to either the createTable() method or the insert() method.
private boolean createTable(XMLCommand sql) - createTable()
uses
the
splitColumns()
method of
XMLCommand
to return the column list as a
Vector
of
columns, which it then uses to create the table. The table is defined in the Vectors
columnNameVector
and
columnTypeVector
.
private boolean insert(XMLCommand sql) - insert() uses columnNameVector
and
columnTypeVector
to create an XML element for each data field, nesting them inside a row
element representing the inserted row.
Note the reference to a custom data type:
ATTRIBUTE
, which specifies that the data be added as an
attribute. The
XMLStatement
object handles
SQL INSERT
commands in one of two ways. If the data
type is
ATTRIBUTE
, the data String is inserted into the XML element as an attribute. Otherwise, it is
appended as an element.
Listing 19-5
shows the code for the
XMLStatement
object.
Listing 19-5: XMLStatement class