public void process(XPathContext context) throws XPathException { // Prepare the SQL statement (only do this once) Controller controller = context.getController(); Item conn = arguments[CONNECTION].evaluateItem(context); if (!(conn instanceof ObjectValue && ((ObjectValue) conn).getObject() instanceof Connection)) { DynamicError de = new DynamicError("Value of connection expression is not a JDBC Connection"); de.setXPathContext(context); throw de; } Connection connection = (Connection) ((ObjectValue) conn).getObject(); String dbCol = arguments[COLUMN].evaluateAsString(context); String dbTab = arguments[TABLE].evaluateAsString(context); String dbWhere = arguments[WHERE].evaluateAsString(context); NamePool pool = controller.getNamePool(); int rowCode = pool.allocate("", "", rowTag); int colCode = pool.allocate("", "", colTag); PreparedStatement ps = null; ResultSet rs = null; DynamicError de = null; try { StringBuffer statement = new StringBuffer(); statement.append("SELECT " + dbCol + " FROM " + dbTab); if (dbWhere != "") { statement.append(" WHERE " + dbWhere); } // System.err.println("-> SQL: " + statement.toString()); // -- Prepare the SQL statement ps = connection.prepareStatement(statement.toString()); controller.setUserData(this, "sql:statement", ps); // -- Execute Statement rs = ps.executeQuery(); // -- Print out Result Receiver out = context.getReceiver(); String result = ""; int icol = rs.getMetaData().getColumnCount(); while (rs.next()) { // next row // System.out.print("<- SQL : "+ rowStart); out.startElement(rowCode, StandardNames.XDT_UNTYPED, locationId, 0); for (int col = 1; col <= icol; col++) { // next column // Read result from RS only once, because // of JDBC-Specifications result = rs.getString(col); out.startElement(colCode, StandardNames.XDT_UNTYPED, locationId, 0); if (result != null) { out.characters(result, locationId, options); } out.endElement(); } // System.out.println(rowEnd); out.endElement(); } // rs.close(); if (!connection.getAutoCommit()) { connection.commit(); } } catch (SQLException ex) { de = new DynamicError("(SQL) " + ex.getMessage()); de.setXPathContext(context); throw de; } finally { boolean wasDEThrown = (de != null); if (rs != null) { try { rs.close(); } catch (SQLException ex) { de = new DynamicError("(SQL) " + ex.getMessage()); de.setXPathContext(context); } } if (ps != null) { try { ps.close(); } catch (SQLException ex) { de = new DynamicError("(SQL) " + ex.getMessage()); de.setXPathContext(context); } } if (!wasDEThrown && de != null) { throw de; // test so we don't lose the real exception } } }