try (Connection con = DriverManager.getConnection(connectionUrl); Statement
stmt = con.createStatement()) {
createColdCallingTable(stmt);
// Determine the column set column
String columnSetColName = null; String strCmd = "SELECT name FROM sys.columns WHERE object_id=(SELECT OBJECT_ID('ColdCalling')) AND is_column_set = 1";
try (ResultSet rs = stmt.executeQuery(strCmd)) {
if (rs.next()) { columnSetColName = rs.getString(1); System.out.println(columnSetColName + " is the column set column!"); } }
DocumentBuilder db = dbf.newDocumentBuilder(); InputSource is = new InputSource(); while (rs.next()) { // Iterate through the columns for (int i = 1; i <= rsmd.getColumnCount(); ++i) { String name = rsmd.getColumnName(i); String value = rs.getString(i);
// If this is the column set column
if (name.equalsIgnoreCase(columnSetColName)) { System.out.println(name);
// Instead of printing the raw XML, parse it
if (value != null) { // Add artificial root node "sparse" to ensure XML is well formed String xml = "<sparse>" + value + "</sparse>";
node that was added NodeList list = doc.getChildNodes(); Node root = list.item(0); // This is the <sparse> node NodeList sparseColumnList = root.getChildNodes(); // These are the xml column nodes
// Iterate through the XML document
for (int n = 0; n < sparseColumnList.getLength(); + +n) { Node sparseColumnNode = sparseColumnList.item(n); String columnName = sparseColumnNode.getNodeName(); // Note that the column value is not in the sparseColumNode, it is the value of // the // first child of it Node sparseColumnValueNode = sparseColumnNode.getFirstChild(); String columnValue = sparseColumnValueNode.getNodeValue();
System.out.println("\t" + columnName + "\t: " +
columnValue); } } } else { // Just print the name + value of non-sparse columns System.out.println(name + "\t: " + value); } } System.out.println();// New line between rows } } } catch (Exception e) { e.printStackTrace(); } }