List of usage examples for java.sql ResultSet getMetaData
ResultSetMetaData getMetaData() throws SQLException;
ResultSet
object's columns. From source file:com.mum.edu.cs472.dictServlet.java
public static JSONArray convertToJSON(ResultSet resultSet) throws Exception { JSONArray jsonArray = new JSONArray(); while (resultSet.next()) { int total_rows = resultSet.getMetaData().getColumnCount(); JSONObject obj = new JSONObject(); for (int i = 0; i < total_rows; i++) { obj.put(resultSet.getMetaData().getColumnLabel(i + 1).toLowerCase(), resultSet.getObject(i + 1)); jsonArray.add(obj);/*from w w w . j av a2 s . co m*/ } } return jsonArray; }
From source file:Main.java
public static String convertResultSetToXML(ResultSet rs) throws SQLException, ParserConfigurationException, TransformerException { ResultSetMetaData rsmd = rs.getMetaData(); int colCount = rsmd.getColumnCount(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.newDocument(); Element results = doc.createElement("Results"); doc.appendChild(results);// w w w . ja v a 2 s. c o m while (rs.next()) { Element row = doc.createElement("Row"); results.appendChild(row); for (int i = 1; i <= colCount; i++) { String columnName = rsmd.getColumnName(i); Object value = rs.getObject(i); if (value != null) { Element node = doc.createElement(columnName.replaceAll("\\(", "_").replaceAll("\\)", "_")); node.appendChild(doc.createTextNode(value.toString())); row.appendChild(node); } } } TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(doc), new StreamResult(writer)); String output = writer.getBuffer().toString().replaceAll("\n|\r", ""); return output; }
From source file:common.DBHelper.java
/** * Extract key from given ResultSet./*from ww w . j av a2 s. c om*/ * * @param key resultSet with key * @return key from given result set * @throws SQLException when operation fails */ public static Long getId(ResultSet key) throws SQLException { if (key.getMetaData().getColumnCount() != 1) { throw new IllegalArgumentException("Given ResultSet contains more columns"); } if (key.next()) { Long result = key.getLong(1); if (key.next()) { throw new IllegalArgumentException("Given ResultSet contains more rows"); } return result; } else { throw new IllegalArgumentException("Given ResultSet contain no rows"); } }
From source file:com.splout.db.common.SQLiteJDBCManager.java
private static List<HashMap<String, Object>> convertResultSetToList(ResultSet rs, int maxResults) throws SQLException { ResultSetMetaData md = rs.getMetaData(); int columns = md.getColumnCount(); List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>(); while (rs.next() && list.size() < maxResults) { HashMap<String, Object> row = new HashMap<String, Object>(columns); for (int i = 1; i <= columns; ++i) { row.put(md.getColumnName(i), rs.getObject(i)); }//from w w w .j a va 2 s . c o m list.add(row); } if (list.size() == maxResults) { throw new SQLException("Hard limit on number of results reached (" + maxResults + "), please use a LIMIT for this query."); } return list; }
From source file:io.cloudslang.content.database.services.SQLQueryService.java
public static void executeSqlQuery(@NotNull final SQLInputs sqlInputs) throws Exception { if (StringUtils.isEmpty(sqlInputs.getSqlCommand())) { throw new Exception("command input is empty."); }/*from ww w . j av a 2 s . com*/ ConnectionService connectionService = new ConnectionService(); try (final Connection connection = connectionService.setUpConnection(sqlInputs)) { connection.setReadOnly(true); Statement statement = connection.createStatement(sqlInputs.getResultSetType(), sqlInputs.getResultSetConcurrency()); statement.setQueryTimeout(sqlInputs.getTimeout()); final ResultSet results = statement.executeQuery(sqlInputs.getSqlCommand()); final ResultSetMetaData mtd = results.getMetaData(); int iNumCols = mtd.getColumnCount(); final StringBuilder strColumns = new StringBuilder(sqlInputs.getStrColumns()); for (int i = 1; i <= iNumCols; i++) { if (i > 1) { strColumns.append(sqlInputs.getStrDelim()); } strColumns.append(mtd.getColumnLabel(i)); } sqlInputs.setStrColumns(strColumns.toString()); while (results.next()) { final StringBuilder strRowHolder = new StringBuilder(); for (int i = 1; i <= iNumCols; i++) { if (i > 1) strRowHolder.append(sqlInputs.getStrDelim()); if (results.getString(i) != null) { String value = results.getString(i).trim(); if (sqlInputs.isNetcool()) value = SQLUtils.processNullTerminatedString(value); strRowHolder.append(value); } } sqlInputs.getLRows().add(strRowHolder.toString()); } } }
From source file:com.bt.aloha.testing.DbTestCase.java
private static String dump(ResultSet rs) throws SQLException { ResultSetMetaData meta = rs.getMetaData(); int colmax = meta.getColumnCount(); Object o = null;/* w w w. j ava2 s . c om*/ StringBuffer sb = new StringBuffer(); for (; rs.next();) { for (int i = 0; i < colmax; i++) { o = rs.getObject(i + 1); if (o != null) sb.append(o.toString()); else sb.append("null"); if (i < colmax - 1) sb.append(" "); } if (!rs.isLast()) sb.append("\n"); } return sb.toString(); }
From source file:com.adaptris.jdbc.JdbcResultSetImpl.java
private static JdbcResultRow mapRow(ResultSet resultSet) throws SQLException { ResultSetMetaData rsmd = resultSet.getMetaData(); int columnCount = rsmd.getColumnCount(); JdbcResultRow row = new JdbcResultRow(); for (int counter = 1; counter <= columnCount; counter++) { row.setFieldValue(StringUtils.defaultIfEmpty(rsmd.getColumnLabel(counter), rsmd.getColumnName(counter)), resultSet.getObject(counter), rsmd.getColumnType(counter)); }//from w w w.j a va 2s . c om return row; }
From source file:com.splout.db.engine.JDBCManager.java
public static QueryResult convertResultSetToQueryResult(ResultSet rs, int maxResults) throws SQLException { ResultSetMetaData md = rs.getMetaData(); int columns = md.getColumnCount(); String[] columnNames = new String[columns]; for (int i = 0; i < columns; i++) { columnNames[i] = md.getColumnName(i); }/*from w ww . j a v a2s . co m*/ List<Object[]> list = new ArrayList<Object[]>(); while (rs.next() && list.size() < maxResults) { Object[] row = new Object[columns]; for (int i = 1; i <= columns; ++i) { row[i] = rs.getObject(i); } list.add(row); } if (list.size() == maxResults) { throw new SQLException("Hard limit on number of results reached (" + maxResults + "), please use a LIMIT for this query."); } return new QueryResult(columnNames, list); }
From source file:at.molindo.dbcopy.util.Utils.java
public static void printRow(ResultSet pk) throws SQLException { System.out.println("--- " + pk.getRow() + ". row ---"); for (int i = 1; i <= pk.getMetaData().getColumnCount(); i++) { System.out.println(pk.getMetaData().getColumnName(i) + ": " + pk.getObject(i)); }//from w w w .ja v a 2 s . c o m }
From source file:Student.java
public static void checkData() throws Exception { Class.forName("org.hsqldb.jdbcDriver"); Connection conn = DriverManager.getConnection("jdbc:hsqldb:data/tutorial", "sa", ""); Statement st = conn.createStatement(); ResultSet mrs = conn.getMetaData().getTables(null, null, null, new String[] { "TABLE" }); while (mrs.next()) { String tableName = mrs.getString(3); System.out.println("\n\n\n\nTable Name: " + tableName); ResultSet rs = st.executeQuery("select * from " + tableName); ResultSetMetaData metadata = rs.getMetaData(); while (rs.next()) { System.out.println(" Row:"); for (int i = 0; i < metadata.getColumnCount(); i++) { System.out.println(" Column Name: " + metadata.getColumnLabel(i + 1) + ", "); System.out.println(" Column Type: " + metadata.getColumnTypeName(i + 1) + ": "); Object value = rs.getObject(i + 1); System.out.println(" Column Value: " + value + "\n"); }// ww w . jav a 2s . c o m } } }