*
* @throws SQLException
*/
public static void testCreateXMLTable() throws SQLException {
String ct_sql = "CREATE TABLE Customer (Cid BIGINT NOT NULL PRIMARY KEY, Info XML)";
Connection conn = DBUtils.makeConnection();
Statement stmt = conn.createStatement();
stmt.executeUpdate(ct_sql);
stmt.close();
conn.close();
}
/**
* 插入数据
*
* @throws SQLException
* @throws IOException
*/
public static void testInsertXMLTable() throws SQLException, IOException {
String xml = "<customerinfo xmlns=\"http://posample.org\" Cid=\"1000\">\n" +
"<name>Robert Shoemaker</name>\n" +
"<addr country=\"Canada\">\n" +
"<street>1596 Baseline</street>\n" +
"<city>zhengzhou</city>\n" +
"<prov-state>Ontario</prov-state>\n" +
"<pcode-zip>N8X 7F8</pcode-zip>\n" +
"</addr>\n" +
"<phone type=\"work\">905-555-2937</phone>\n" +
"</customerinfo>";
String ins_sql = "INSERT INTO CUSTOMER (CID, INFO) VALUES (1000, ?)";
Connection conn = DBUtils.makeConnection();
conn.setAutoCommit(false);
PreparedStatement pstmt = conn.prepareStatement(ins_sql);
byte[] b = xml.getBytes();
InputStream ins = new ByteArrayInputStream(b);
pstmt.setBinaryStream(1, ins, b.length);
pstmt.executeUpdate();
conn.commit();
ins.close();
pstmt.close();
conn.close();
}
/**
* XQuery查询数据
*
* @throws SQLException
*/
public static void testQueryXMLTable() throws SQLException {
String query_sql = "SELECT XMLQUERY (\n" +
"'declare default element namespace \"http://posample.org\";\n" +
"for $d in $doc/customerinfo\n" +
"return <out>{$d/name}</out>'\n" +
"passing INFO as \"doc\")\n" +
"FROM Customer as c\n" +
"WHERE XMLEXISTS ('declare default element namespace \"http://posample.org\";\n" +
"$i/customerinfo/addr[city=\"zhengzhou\"]' passing c.INFO as \"i\")";
Connection conn = DBUtils.makeConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query_sql);
StringBuffer xmls = new StringBuffer();
while (rs.next()) {
xmls.append(rs.getString(1)).append("\n");
}
System.out.println(xmls.toString());
stmt.close();
conn.close();
}
/**
* XQuery更新数据
*
* @throws SQLException
* @throws IOException
*/
public static void testUpdateXMLTable() throws SQLException, IOException {
String xml = "<customerinfo xmlns=\"http://posample.org\" Cid=\"1002\">\n" +
"<name>Jim Noodle</name>\n" +
"<addr country=\"Canada\">\n" +
"<street>1150 Maple Drive</street>\n" +
"<city>Newtown</city>\n" +
"<prov-state>Ontario</prov-state>\n" +
"<pcode-zip>Z9Z 2P2</pcode-zip>\n" +
"</addr>\n" +
"<phone type=\"work\">905-555-7258</phone>\n" +
"</customerinfo>";
String up_sql = "UPDATE customer SET info =?" +
"WHERE XMLEXISTS (\n" +
"'declare default element namespace \"http://posample.org\";\n" +
"$doc/customerinfo[@Cid = 1000]'\n" +
"passing INFO as \"doc\")";
Connection conn = DBUtils.makeConnection();
conn.setAutoCommit(false);
PreparedStatement pstmt = conn.prepareStatement(up_sql);
byte[] b = xml.getBytes();
InputStream ins = new ByteArrayInputStream(b);
pstmt.setBinaryStream(1, ins, b.length);
pstmt.executeUpdate();
conn.commit();
ins.close();
pstmt.close();
conn.close();
}
相关专题
- DB2 (442篇文章)
- Java环境安装配置 (5824篇文章)
- DB2中的数据处理 (488篇文章)
- Java编程开发手册 (8505篇文章)
- XML详解 (1570篇文章)
- Java应用开发篇 (1122篇文章)
- Java网络及通讯编程 (686篇文章)
- 开发应用 (632篇文章)
- Web开发 (434篇文章)
- 灵活有效的数据仓库解决方案:设计并实现仓库E (23次浏览)
- 嵌入式数据库系统Berkeley DB (12次浏览)
- 灵活有效的数据仓库解决方案:仓库设计和数据 (10次浏览)
- 在专家的帮助下设计数据仓库 (7次浏览)
- 数据仓库杂谈 (7次浏览)
- DB2仓库管理器7.2概述 (6次浏览)
- DB2数据仓库系统下配置存储带宽 (5次浏览)
- DB2从AIX server上转移(迁移)到linux上 (5次浏览)
- 应用BCU构建高性能数据仓库系统 (4次浏览)
- IBM DB2 9pureXML技术向沪上企业抛出红绣球 (4次浏览)



