📄 xmlsqlutil.java
字号:
package oracle.otnsamples.util;import java.sql.Types;import java.sql.ResultSet;/* * @author : Neelesh * @Version : 1.0 * * Development Environment : Oracle JDeveloper 10g * Name of the Application : XMLSQLUtil.java * Creation/Modification History : * * Neelesh 07-Sep-2003 created * */import java.sql.Connection;import java.sql.PreparedStatement;/** * This is a utility to convert the results of an SQL query to an XML document. * For more information on this concept and more functionality, see XML SQL Utility * (XSU) which is a part of Oracle XML Developer's Kit (XDK). * This is a very primitive implementation of one of the functionality provided * by XSU. */public class XMLSQLUtil { /** * This method creates an XML document from the given query,bind parameters * and the connection. * The root element of this XML is <ROWSET>. * Each row in the result is wrapped in a <ROW> element * The values of columns are wrapped in elements named with column names. * So a query on a table with columns name and address will generate * <ROWSET> * <ROW> * <NAME>Neelesh</NAME> * <ADDRESS>Oracle</ADDRESS> * </ROW> * </ROWSET> * The ROW element is repeated for each row. * @param query -SQL query * @param bindParams -List of bind params for the query * @param con -SQL Connection * @return StringBuffer - XML document representing the query results. */ public StringBuffer getXML(String query,Object[] bindParams,Connection con) { ResultSet rSet=null; PreparedStatement pst = null; StringBuffer buffer= new StringBuffer("<ROWSET>"); try{ pst = con.prepareStatement(query); for (int i = 0; i < bindParams.length; i++) { if(bindParams[i]==null){ pst.setNull(i+1,Types.JAVA_OBJECT); }else{ pst.setObject(i+1,bindParams[i]); } } rSet = pst.executeQuery(); int cols=rSet.getMetaData().getColumnCount(); String[] colNames = new String[cols]; for (int i = 0; i < cols; i++) { colNames[i]=rSet.getMetaData().getColumnName(i+1); } while(rSet.next()){ buffer.append("\n\t<ROW>\n\t\t"); for (int i = 0; i < cols; i++) { buffer.append("\n\t\t\t<"); buffer.append(colNames[i]); buffer.append(">"); buffer.append(rSet.getObject(colNames[i])); buffer.append("</"); buffer.append(colNames[i]); buffer.append(">"); } buffer.append("\n\t</ROW>"); } } catch (Exception ex) { ex.printStackTrace(); buffer.append("<ERROR>"); buffer.append(ex.getMessage()); buffer.append("</ERROR>"); } finally { try { if(rSet!=null){ rSet.close(); } if(pst!=null) { pst.close(); } } catch (Exception ex) { } } buffer.append("\n</ROWSET>"); return buffer; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -