/** * Copyright (c) 2011-2014, OpenIoT * * This file is part of OpenIoT. * * OpenIoT is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, version 3 of the License. * * OpenIoT is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with OpenIoT. If not, see <http://www.gnu.org/licenses/>. * * Contact: OpenIoT mailto: info@openiot.eu * @author Behnaz Bostanipour */ package org.openiot.gsn.http.ac; import org.apache.log4j.Logger; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; /** * Created by IntelliJ IDEA. * User: Behnaz Bostanipour * Author: Jason Hunter * Date: Apr 12, 2010 * Time: 5:01:58 PM * To change this template use File | Settings | File Templates. */ public class HtmlResultSet { private ResultSet rs; private static transient Logger logger= Logger.getLogger( HtmlResultSet.class ); public void setResultSet(ResultSet rs) { this.rs = rs; } public String toString()// can be called at most once { StringBuffer out = new StringBuffer(); // Start a table to display the result set out.append("<TABLE>\n"); try { ResultSetMetaData rsmd = rs.getMetaData(); int numcols = rsmd.getColumnCount(); // Title the table with the result set's column labels out.append("<TR>"); for (int i = 1; i <= numcols; i++) { out.append("<TH>" + rsmd.getColumnLabel(i)); } out.append("</TR>\n"); while(rs.next()) { out.append("<TR>"); // start a new row for (int i = 1; i <= numcols; i++) { out.append("<TD>"); // start a new data element Object obj = rs.getObject(i); if (obj != null) out.append(obj.toString()); else out.append(" "); } out.append("</TR>\n"); } // End the table out.append("</TABLE>\n"); } catch (SQLException e) { out.append("</TABLE><H1>ERROR:</H1> " + e.getMessage() + "\n"); } return out.toString(); } }