Retrieving XML data by using the IMS Universal JDBC driver
You can use the IMS Universal JDBC driver to retrieve XML data from an IMS database as a character large object (CLOB) through an SQL SELECT statement.
To retrieve XML data in your IMS Universal JDBC driver application:
Procedure
The following code sample shows how to retrieve XML data from the Hospital database.
package uxml.samples;
import java.io.*;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.ibm.ims.jdbc.IMSDataSource;
public class RetrieveXMLSamples{
public static void main(String argv[]) throws SQLException,IOException {
IMSDataSource ds = new IMSDataSource();
ds.setDatabaseName("class://uxml.samples.BMP255NewSyntaxDatabaseView");
ds.setDatastoreName("IMS1");
ds.setDatastoreServer("yourhost.yourdomain.com");
ds.setPortNumber(5555);
ds.setDriverType(IMSDataSource.DRIVER_TYPE_4);
ds.setUser("myUserId");
ds.setPassword("myPass");
// Specify file path of XML schema
System.setProperty("http://www.ibm.com/ims/schema-resolver/file/path",
"uxml/samples");
Connection conn = null;
try {
conn = ds.getConnection();
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT hospxml FROM PCB01.HOSPITAL");
StringWriter sw = new StringWriter();
while (rs.next()) {
Clob clob = rs.getClob(1);
Reader reader = clob.getCharacterStream();
char[] buffer = new char[1000];
int read = reader.read(buffer);
while (read != -1) {
sw.write(buffer,0,read);
read = reader.read(buffer);
}
}
String result = sw.toString();
System.out.println(result);
System.out.println();
conn.commit();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
if (!conn.isClosed()) {
conn.rollback();
conn.close();
}
}
}
}