protected void executeQuery(Statement stmt, String q) throws SQLException { q = q.replace("$PREFIX", getPrefix()); LOG.info(" Executing " + q); ResultSet rs = stmt.executeQuery(q); StringBuilder header = new StringBuilder(); for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) { if (i > 1) { header.append("|"); } header.append(rs.getMetaData().getColumnName(i)); } LOG.info(header); int seq = 0; while (true) { boolean valid = rs.next(); if (!valid) break; seq++; StringBuilder line = new StringBuilder(); for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) { if (i > 1) { line.append("|"); } line.append(rs.getString(i)); } LOG.info(seq + ":" + line); } }
/** EJECUTA UNA CONSULTA Y GENERA LA TABLA HTML */ protected String getHTML(String SQL) throws Exception { StringBuffer html = new StringBuffer(); html.append("<TABLE border='1'>"); html.append("<TR>"); if (conectar()) { ResultSet rs = ejecutarSQL(SQL); ResultSetMetaData rsm = rs.getMetaData(); html.append("<TR>"); for (int i = 1; i <= rsm.getColumnCount(); i++) { html.append("<TH>" + rsm.getColumnName(i) + "</TH>"); } html.append("</TR>"); while (rs.next()) { html.append("<TR>"); for (int i = 1; i <= rsm.getColumnCount(); i++) { html.append("<TD>" + rs.getString(i) + ".</TD>"); } html.append("</TR>"); } desconectar(); } html.append("</TR>"); html.append("</TABLE>"); return html.toString(); } // Fin getHTML
protected ArrayList<String> getSQL(String SQL) throws Exception { StringBuffer html = new StringBuffer(); ArrayList<String> v = new ArrayList<String>(); if (conectar()) { ResultSet rs = ejecutarSQL(SQL); ResultSetMetaData rsm = rs.getMetaData(); while (rs.next()) { String r = ""; for (int i = 1; i <= rsm.getColumnCount(); i++) { r += rs.getString(i) + "-"; } v.add(r); } desconectar(); } return v; } // Fin getHTML
/** EJECUTA UNA CONSULTA Y GENERA XML */ protected String getXML() throws Exception { StringBuffer xml = new StringBuffer(); xml.append("<registros>"); if (conectar()) { ResultSet rs = ejecutarSQL(this.SQL); while (rs.next()) { ResultSetMetaData rsm = rs.getMetaData(); xml.append("<registro>"); for (int i = 1; i <= rsm.getColumnCount(); i++) { xml.append("<" + rsm.getColumnName(i) + ">"); xml.append(rs.getString(i)); xml.append("</" + rsm.getColumnName(i) + ">"); } xml.append("</registro>"); } desconectar(); } xml.append("</registros>"); return xml.toString(); } // Fin getXML
// // Find all the methods for java.sql objects in the Connection which raise // SQLFeatureNotSupportedException. // private void connectionWorkhorse( Connection conn, HashSet<String> unsupportedList, HashSet<String> notUnderstoodList) throws Exception { vetSavepoint(conn, unsupportedList, notUnderstoodList); vetLargeObjects(conn, unsupportedList, notUnderstoodList); DatabaseMetaData dbmd = conn.getMetaData(); PreparedStatement ps = conn.prepareStatement("select * from sys.systables where tablename = ?"); ps.setString(1, "foo"); ParameterMetaData parameterMetaData = ps.getParameterMetaData(); ResultSet rs = ps.executeQuery(); ResultSetMetaData rsmd = rs.getMetaData(); Statement stmt = conn.createStatement(); CallableStatement cs = conn.prepareCall("CALL SYSCS_UTIL.SET_RUNTIMESTATISTICS(0)"); ParameterMetaData csmd = cs.getParameterMetaData(); // // The vetObject() method calls all of the methods in these objects // in a deterministic order, calling the close() method last. // Inspect these objects in an order which respects the fact that // the objects are closed as a result of calling vetObject(). // vetObject(dbmd, unsupportedList, notUnderstoodList); vetObject(stmt, unsupportedList, notUnderstoodList); vetObject(csmd, unsupportedList, notUnderstoodList); vetObject(cs, unsupportedList, notUnderstoodList); vetObject(rsmd, unsupportedList, notUnderstoodList); vetObject(rs, unsupportedList, notUnderstoodList); vetObject(parameterMetaData, unsupportedList, notUnderstoodList); vetObject(ps, unsupportedList, notUnderstoodList); vetObject(conn, unsupportedList, notUnderstoodList); // No need to close the objects. They were closed by vetObject(). }
/** * M�todo de prueba Recibe como argumento el nombre del archivo de configuracion. Para usarlo: * java JDBCMiddler archivo.conf */ protected static void main(String args[]) throws Exception { // Se recibe como argumento del main el archivo de // configuracion que contiene el driver // la url, el login y la clave String parametros[] = leerParametros(args[0]); int n = 1; JDBCMiddler middler = new JDBCMiddler(parametros[0], parametros[1], parametros[2], parametros[3]); if (middler.conectar()) { System.out.println(parametros[4]); ResultSet rs = middler.ejecutarSQL(parametros[4]); while (rs.next()) { ResultSetMetaData rsm = rs.getMetaData(); System.out.println("****************************"); System.out.println("Registro: " + (n++)); for (int i = 1; i <= rsm.getColumnCount(); i++) { System.out.print(rsm.getColumnName(i) + ": "); System.out.println(rs.getString(i)); } } middler.desconectar(); System.out.println("todo bien"); } } // fin main