Monday, March 26, 2007

Using XML as database with Java

here is how you can use XML file as a database,you can get the driver here

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();
}



Here is an example of XML as database
Import the package:

import com.ibm.db2.jcc.DB2Xml;

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"); // or
InputStream inputStream = resultSet.getBinaryStream("DESCRIPTION"); // or
Reader reader = resultSet.getCharacterStream("DESCRIPTION"); // or
DB2Xml db2xml = (DB2Xml) resultSet.getObject("DESCRIPTION");

you can use other methods available in java

  • 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();



all you need is the db2 drivers you can find it here

https://www14.software.ibm.com/webapp/iwm/web/preLogin.do?lang=en_US&source=swg-dm-db2jdbcdriver


3 comments:

dave said...

hi..
just wanna ask..do you know something bot j2ee ?
how to connect them with xml database ? need any dbms ? tq

Anonymous said...

Now do you worried about that in the game do not had enough shaiya gold to play the game, now you can not worried, my friend told me a website, in here you can buy a lot shaiya money and only spend a little money, do not hesitate, it was really, in here we had much cheap shaiya gold, we can sure that you will get the shaiya online gold, quick to come here to buy shaiya gold .


Now do you worried about that in the game do not had enough silkroad gold to play the game, now you can not worried, my friend told me a website, in here you can buy a lot sro gold and only spend a little money, do not hesitate, it was really, in here we had much silkroad online gold, we can sure that you will get the silk road gold, quick to come here to buy cheap silkroad gold.

Anonymous said...

The Tax Return Crack-Up<2>
I was not shocked because this was old news -- practically ancient, in fact. In R. Microsoft Office Emmett Tyrrell, Office 2010 Jr.'s most recent book The Clinton Microsoft Office 2010 Crack-
Up, page fiv Office 2007 e, paragraph two, we learn that in Bill Clinton's "first four years out of the White H Microsoft Office 2007 ouse, he ea Office 2010 key rned over Office 2010 download $43 million Office 2010 Professional after
expenses... Microsoft outlook "
The next Outlook 2010 page directs Windows 7 us to Appendix Microsoft outlook 2010 I, a list of the conniving couple's fees for speeches and book royalties and other income. The first