3/27/2007

XML as database

Here is how you can connect to the database:
try { 
//Load the DB2 JDBC Type 2 Driver with DriverManager
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
}catch (ClassNotFoundException e) {
e.printStackTrace();
}

Getting connection:
connection = DriverManager.getConnection(url, user, pass);
the prepare statement:PreparedStatement stmt = connection.prepareStatement(sql);
Result Set:
option 1:ResultSet resultSet = stmt.executeQuery();
option 2:InputStream inputStream = resultSet.getBinaryStream(1);
option 3:DB2Xml db2xml = (DB2Xml) resultSet.getObject(1);

Selecting a XML value:String sql = "SELECT PID, DESCRIPTION from XMLPRODUCT where PID = ?";
PreparedStatement stmt = connection.prepareStatement(sql);stmt.setString(1, "100-105-09");
ResultSet resultSet = stmt.executeQuery();
String xml = resultSet.getString("DESCRIPTION"); // orInputStream inputStream = resultSet.getBinaryStream("DESCRIPTION"); // orReader reader = resultSet.getCharacterStream("DESCRIPTION"); // orDB2Xml db2xml = (DB2Xml) resultSet.getObject("DESCRIPTION");


you can use other methods available in java too..!

getString( )
getBinaryStream( )
getCharacterStrem( )
getObject( )


Inserting in XML file:

String sql = "INSERT INTO xmlproduct VALUES(?, ?)";
PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setString(1, "100-105-09");
File binFile = new File("productBinIn.xml");
InputStream inBin = new FileInputStream(xmlFile);
stmt.setBinaryStream(2, inBin, (int) binFile.getLength());stmt.execute();


XML database driver

2 comments:

Arun said...
This comment has been removed by the author.
Arun said...

This is news to me. Cool. But what i understand is that you are trying to save an XML file in a single column of the table. (which is cool). But most of the time we don't do this, due to the obvious reason it is a performance monster. Most of the time what we do is to get the data from the db and build an xml out of it for various purposes (pass it on to a mobile client, make Ajax components etc). And in those cases, WebRowSet does a good job.

Tired of seeing that 500 Bad gateway error while deploying a Springboot application in AWS...?

By default, Spring Boot applications will listen on port 8080. Elastic Beanstalk assumes that the application will listen on port 5000. Th...