List of usage examples for java.sql ResultSetMetaData getColumnCount
int getColumnCount() throws SQLException;
ResultSet
object. From source file:net.mlw.vlh.adapter.jdbc.dynabean.fix.JDBCDynaClass.java
/** * <p>Introspect the metadata associated with our result set, and populate * the <code>properties</code> and <code>propertiesMap</code> instance * variables.</p>/*from w w w . j a va2s.c o m*/ * * @param resultSet The <code>resultSet</code> whose metadata is to * be introspected * * @exception SQLException if an error is encountered processing the * result set metadata */ protected void introspect(ResultSet resultSet) throws SQLException { // Accumulate an ordered list of DynaProperties List list = new ArrayList(); ResultSetMetaData metadata = resultSet.getMetaData(); int n = metadata.getColumnCount(); for (int i = 1; i <= n; i++) { // JDBC is one-relative! DynaProperty dynaProperty = createDynaProperty(metadata, i); if (dynaProperty != null) { list.add(dynaProperty); } } // Convert this list into the internal data structures we need properties = (DynaProperty[]) list.toArray(new DynaProperty[list.size()]); for (int i = 0; i < properties.length; i++) { propertiesMap.put(properties[i].getName(), properties[i]); } }
From source file:com.micromux.cassandra.jdbc.MetadataResultSetsTest.java
private String toString(ResultSet result) throws SQLException { StringBuilder sb = new StringBuilder(); while (result.next()) { ResultSetMetaData metadata = result.getMetaData(); int colCount = metadata.getColumnCount(); sb.append(String.format("(%d) ", result.getRow())); for (int i = 1; i <= colCount; i++) { sb.append(" " + showColumn(i, result)); }/* ww w. j a v a 2 s . com*/ sb.append("\n"); } return sb.toString(); }
From source file:com.zimbra.cs.db.JdbcClient.java
private Object[] getCurrentRow(ResultSet rs) throws SQLException { ResultSetMetaData md = rs.getMetaData(); int colCount = md.getColumnCount(); Object[] row = new Object[colCount]; for (int i = 0; i < colCount; i++) { row[i] = rs.getObject(i + 1);/*from www. j av a 2 s . c o m*/ } return row; }
From source file:ResultsDecoratorSQL.java
public void write(ResultSet rs) throws IOException, SQLException { ResultSetMetaData md = rs.getMetaData(); // This assumes you're not using a Join!! String tableName = md.getTableName(1); int cols = md.getColumnCount(); StringBuffer sb = new StringBuffer("insert into ").append(tableName).append("("); for (int i = 1; i <= cols; i++) { sb.append(md.getColumnName(i));// w ww. java 2 s . c om if (i != cols) { sb.append(", "); } } sb.append(") values ("); String insertCommand = sb.toString(); while (rs.next()) { println(insertCommand); for (int i = 1; i <= cols; i++) { String tmp = rs.getString(i); if (rs.wasNull()) { print("null"); } else { int type = md.getColumnType(i); // Don't quote numeric types; quote all others for now. switch (type) { case Types.BIGINT: case Types.DECIMAL: case Types.DOUBLE: case Types.FLOAT: case Types.INTEGER: print(tmp); break; default: tmp = tmp.replaceAll("'", "''"); print("'" + tmp + "'"); } } if (i != cols) { print(", "); } } println(");"); } }
From source file:com.abixen.platform.module.chart.service.impl.AbstractDatabaseService.java
public List<String> getColumns(Connection connection, String tableName) { List<String> columns = new ArrayList<>(); try {/*from ww w.ja v a 2 s . c o m*/ Statement stmt = connection.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM " + tableName); ResultSetMetaData rsmd = rs.getMetaData(); int columnCount = rsmd.getColumnCount(); IntStream.range(1, columnCount + 1).forEach(i -> { try { columns.add(rsmd.getColumnName(i)); } catch (SQLException e) { e.printStackTrace(); } }); } catch (SQLException e) { e.printStackTrace(); } return columns; }
From source file:com.netspective.axiom.sql.ResultSetUtils.java
/** * Given a ResultSet, return a Map of all the column names in the ResultSet * in lowercase as the key and the index of the column as the value. */// ww w. jav a 2 s .c o m public Map getColumnNamesIndexMap(ResultSet rs) throws SQLException { Map map = new HashMap(); ResultSetMetaData rsmd = rs.getMetaData(); int colsCount = rsmd.getColumnCount(); for (int i = 1; i <= colsCount; i++) { map.put(rsmd.getColumnName(i).toLowerCase(), new Integer(i)); } return map; }
From source file:com.adaptris.jdbc.connection.FailoverConnection.java
private void testConnection() throws SQLException { Statement stmt = sqlConnection.createStatement(); ResultSet rs = null;// w w w . j av a 2 s .c o m try { if (isEmpty(config.getTestStatement())) { return; } if (config.getAlwaysValidateConnection()) { if (isDebugMode()) { rs = stmt.executeQuery(config.getTestStatement()); if (rs.next()) { StringBuffer sb = new StringBuffer("TestStatement Results - "); ResultSetMetaData rsm = rs.getMetaData(); for (int i = 1; i <= rsm.getColumnCount(); i++) { sb.append("["); sb.append(rsm.getColumnName(i)); sb.append("="); try { sb.append(rs.getString(i)); } catch (Exception e) { sb.append("'unknown'"); } sb.append("] "); } logR.trace(sb.toString()); } } else { stmt.execute(config.getTestStatement()); } } } finally { JdbcUtil.closeQuietly(rs); JdbcUtil.closeQuietly(stmt); } }
From source file:com.arsmentis.cordova.jdbc.Jdbc.java
private JSONArray execute(String sql) throws SQLException, JSONException { if (connection == null) { throw new SQLException("Not connected"); }/*from w ww . j av a2 s . c o m*/ JSONArray results = new JSONArray(); Statement statement = connection.createStatement(); if (statement.execute(sql)) { ResultSet resultSet = statement.getResultSet(); ResultSetMetaData columns = resultSet.getMetaData(); while (resultSet.next()) { JSONObject row = new JSONObject(); for (int i = 1; i <= columns.getColumnCount(); i++) { row.put(columns.getColumnName(i), resultSet.getObject(i)); } results.put(row); } resultSet.close(); } statement.close(); return results; }
From source file:com.netspective.axiom.sql.ResultSetUtils.java
public Object[] getResultSetSingleRowArray(ResultSet rs) throws SQLException { if (rs.next()) { ResultSetMetaData rsmd = rs.getMetaData(); int colsCount = rsmd.getColumnCount(); Object[] result = new Object[colsCount]; for (int i = 1; i <= colsCount; i++) { result[i - 1] = rs.getObject(i); }//from w w w .j a v a2 s . c o m return result; } else return null; }
From source file:com.netspective.axiom.sql.ResultSetUtils.java
public Object[] getResultSetSingleRowAsArray(ResultSet rs) throws SQLException { if (rs.next()) { ResultSetMetaData rsmd = rs.getMetaData(); int colsCount = rsmd.getColumnCount(); Object[] result = new Object[colsCount]; for (int i = 1; i <= colsCount; i++) { result[i - 1] = rs.getObject(i); }//from w ww . j a v a 2 s .co m return result; } else return null; }