xquery for $y in db2-fn:xmlcolumn('CUSTOMERS.CONTACTINFO')/Customer/Address
where $y/zip=95116 return $y
. . .
// 连接数据库的代码,这里省略
. . .
public static String getAddress() ...{
String Address;
Address= null;
![]()
try ...{
int zipCode = 95116;
String query = "select xmlquery("
+ " '$y/Customer/Address[zip=$z]' "
+ " passing contactinfo as \"y\", "
+ "cast (? as Integer) as \"z\") " + "from CUSTOMERS";
![]()
poStmt = conn.prepareStatement(query);
poStmt.setInt(1, zipCode);
rs = poStmt.executeQuery();
![]()
boolean flag = rs.next();
while (flag) ...{
Address= rs.getString(1);
System.out.println(Address);
flag = rs.next();
![]()
}
![]()
} catch (SQLException sqle) ...{
System.out.println("Error Msg: " + sqle.getMessage());
System.out.println("SQLState: " + sqle.getSQLState());
System.out.println("SQLError: " + sqle.getErrorCode());
System.out.println("Rollback the transaction and quit the program");
System.out.println();
![]()
System.exit(1);
}
![]()
return Address;
}
. . .
// 连接数据库的代码,这里省略
. . .
![]()
public static String getAddress() ...{
String Address;
Address= null;
![]()
try ...{
//int zipCode = 95116;
String query="xquery for $y in
db2-fn:xmlcolumn('CUSTOMERS.CONTACTINFO')/Customer/Address
where $y/zip=95116 return $y";poStmt = conn.prepareStatement(query);
//poStmt.setInt(1, zipCode);
rs = poStmt.executeQuery();
![]()
boolean flag = rs.next();
while (flag) ...{
Address= rs.getString(1);
System.out.println(Address);
flag = rs.next();
![]()
}
![]()
} catch (SQLException sqle) ...{
System.out.println("Error Msg: " + sqle.getMessage());
System.out.println("SQLState: " + sqle.getSQLState());
System.out.println("SQLError: " + sqle.getErrorCode());
System.out.println("Rollback the transaction and quit the program");
System.out.println();
![]()
System.exit(1);
}
![]()
return Address;
}
<Address xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<street>5401 Julio Ave.</street>
<city>San Jose</city>
<state>CA</state>
<zip>95116</zip>
</Address>
| 第1页: 准备 | 第2页: 在Java 应用程序中嵌入 XQuery |