List of usage examples for java.sql ResultSetMetaData getColumnName
String getColumnName(int column) throws SQLException;
From source file:org.owasp.webgoat.plugin.introduction.SqlInjectionLesson5a.java
public static String writeTable(ResultSet results, ResultSetMetaData resultsMetaData) throws IOException, SQLException { int numColumns = resultsMetaData.getColumnCount(); results.beforeFirst();/*from w w w . j a va 2 s .co m*/ StringBuffer t = new StringBuffer(); t.append("<p>"); if (results.next()) { for (int i = 1; i < (numColumns + 1); i++) { t.append(resultsMetaData.getColumnName(i)); t.append(", "); } t.append("<br />"); results.beforeFirst(); while (results.next()) { for (int i = 1; i < (numColumns + 1); i++) { t.append(results.getString(i)); t.append(", "); } t.append("<br />"); } } else { t.append("Query Successful; however no data was returned from this query."); } t.append("</p>"); return (t.toString()); }
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 ww .j a v a 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:architecture.common.adaptor.connector.jdbc.AbstractJdbcConnector.java
public static String lookupColumnName(ResultSetMetaData resultSetMetaData, int columnIndex) throws SQLException { String name = resultSetMetaData.getColumnLabel(columnIndex); if (name == null || name.length() < 1) { name = resultSetMetaData.getColumnName(columnIndex); }//from ww w . j a v a2 s . co m return name; }
From source file:com.zimbra.cs.mailbox.util.MetadataDump.java
private static Row getItemRow(DbConnection conn, int groupId, int mboxId, int itemId, boolean fromDumpster) throws ServiceException { PreparedStatement stmt = null; ResultSet rs = null;/*from w w w .ja v a 2 s. co m*/ try { String sql = "SELECT * FROM " + DbMailItem.getMailItemTableName(groupId, fromDumpster) + " WHERE mailbox_id = " + mboxId + " AND id = " + itemId; stmt = conn.prepareStatement(sql); rs = stmt.executeQuery(); if (!rs.next()) throw ServiceException.INVALID_REQUEST("No such item: mbox=" + mboxId + ", item=" + itemId, null); Row row = new Row(); ResultSetMetaData rsMeta = rs.getMetaData(); int cols = rsMeta.getColumnCount(); for (int i = 1; i <= cols; i++) { String colName = rsMeta.getColumnName(i); String colValue = rs.getString(i); if (rs.wasNull()) colValue = null; row.addColumn(colName, colValue); } return row; } catch (SQLException e) { throw ServiceException.INVALID_REQUEST("No such item: mbox=" + mboxId + ", item=" + itemId, e); } finally { DbPool.closeResults(rs); DbPool.closeStatement(stmt); } }
From source file:desktop.olayinka.file.transfer.model.DerbyJDBCHelper.java
public static JSONArray convert(ResultSet rs) throws SQLException, JSONException { ResultSetMetaData rsmd = rs.getMetaData(); int numColumns = rsmd.getColumnCount(); JSONArray jsonArray = new JSONArray(); while (rs.next()) { JSONObject obj = new JSONObject(); for (int i = 1; i < numColumns + 1; i++) { String columnName = rsmd.getColumnName(i); obj.put(columnName, rs.getObject(i)); }// w w w. j a va 2 s . co m jsonArray.put(obj); } return jsonArray; }
From source file:com.nextep.designer.sqlclient.ui.helpers.SQLHelper.java
public static INextepMetadata createOfflineMetadata(ResultSetMetaData md, String sqlQuery) throws SQLException { final NextepResultSetMetaData nmd = new NextepResultSetMetaData(); DMLParseResult parseResult = null;//from www. j a va2 s.c o m nmd.setColumnCount(md.getColumnCount()); for (int i = 1; i <= md.getColumnCount(); i++) { nmd.setColumnName(i, md.getColumnName(i)); nmd.setColumnType(i, md.getColumnType(i)); // Fetching tablename from driver String tableName = md.getTableName(i); // If not available we try to parse ourselves if (tableName == null || "".equals(tableName)) { // Parsing if (parseResult == null) { try { parseResult = parseSQL(sqlQuery, 1); } catch (RuntimeException e) { LOGGER.error("Error while parsing SQL : " + e.getMessage(), e); } } // Only providing name on single table select if (parseResult != null && parseResult.getFromTables().size() == 1) { tableName = parseResult.getFromTables().iterator().next().getTableName(); } } nmd.setTableName(i, tableName); } return nmd; }
From source file:org.apache.eagle.storage.jdbc.entity.JdbcEntitySerDeserHelper.java
/** * * @param resultSet/* ww w .jav a 2s . co m*/ * @param entityDefinition * @return * @throws SQLException * @throws IOException */ public static Map<String, Object> readInternal(ResultSet resultSet, JdbcEntityDefinition entityDefinition) throws SQLException, IOException { ResultSetMetaData metadata = resultSet.getMetaData(); int columnCount = metadata.getColumnCount(); Map<String, Object> row = new HashMap<String, Object>(); for (int i = 1; i <= columnCount; i++) { String columnName = metadata.getColumnName(i); JdbcSerDeser serDeser = entityDefinition.getJdbcSerDeser(columnName); if (serDeser == null) { throw new IOException("SQLSerDeser for column: " + columnName + " is null"); } Object value; if (entityDefinition.isField(columnName)) { try { value = serDeser.toJavaTypedValue(resultSet, entityDefinition.getColumnType(columnName), columnName, entityDefinition.getColumnQualifier(columnName)); } catch (NoSuchFieldException e) { LOG.error("No field {} in entity {}", columnName, entityDefinition.getInternal().getEntityClass()); throw new IOException(String.format("No field %s in entity %s", columnName, entityDefinition.getInternal().getEntityClass()), e); } } else { columnName = entityDefinition.getOriginalJavaTagName(columnName); // treat as tag or others value = resultSet.getObject(columnName); } row.put(columnName, value); } return row; }
From source file:org.apache.falcon.regression.core.util.HiveUtil.java
/** * Fetch rows from a given ResultSet and convert is a a list of string, each string is comma * separated column values. The output also has header with column names and footer with * number of rows returned./*from ww w . j a v a 2s . c o m*/ * @param rs result set * @return List of string - each string corresponds to the output output that you will get on * sql prompt * @throws SQLException */ public static List<String> fetchRows(ResultSet rs) throws SQLException { ResultSetMetaData metaData = rs.getMetaData(); List<String> output = new ArrayList<String>(); int numberOfColumns = metaData.getColumnCount(); StringBuilder sbCol = new StringBuilder(); for (int i = 1; i <= numberOfColumns; i++) { if (i > 1) { sbCol.append(","); } String columnName = metaData.getColumnName(i); // the column name looks like tab1.col1 // we want to remove table name else table equality will fail if (columnName.contains(".")) { columnName = columnName.split("\\.")[1]; } sbCol.append("'").append(columnName).append("'"); } LOGGER.info(sbCol.toString()); output.add(sbCol.toString()); int numberOfRows = 0; while (rs.next()) { StringBuilder sbVal = new StringBuilder(); numberOfRows++; for (int i = 1; i <= numberOfColumns; i++) { if (i > 1) { sbVal.append(","); } String columnValue = rs.getString(i); sbVal.append("'").append(columnValue != null ? columnValue : "").append("'"); } LOGGER.info(sbVal.toString()); output.add(sbVal.toString()); } Collections.sort(output); //sorting to ensure stability results across different runs String rowStr = (numberOfRows > 0 ? numberOfRows : "No") + (numberOfRows == 1 ? " row" : " rows") + " selected"; LOGGER.info(rowStr); output.add(rowStr); return output; }
From source file:com.zimbra.cs.mailbox.util.MetadataDump.java
private static List<Row> getRevisionRows(DbConnection conn, int groupId, int mboxId, int itemId, boolean fromDumpster) throws ServiceException { PreparedStatement stmt = null; ResultSet rs = null;//www .j a v a 2 s .co m try { String sql = "SELECT * FROM " + DbMailItem.getRevisionTableName(groupId, fromDumpster) + " WHERE mailbox_id = " + mboxId + " AND item_id = " + itemId + " ORDER BY mailbox_id, item_id, version DESC"; stmt = conn.prepareStatement(sql); rs = stmt.executeQuery(); List<Row> rows = new ArrayList<Row>(); while (rs.next()) { Row row = new Row(); ResultSetMetaData rsMeta = rs.getMetaData(); int cols = rsMeta.getColumnCount(); for (int i = 1; i <= cols; i++) { String colName = rsMeta.getColumnName(i); String colValue = rs.getString(i); if (rs.wasNull()) colValue = null; row.addColumn(colName, colValue); } rows.add(row); } return rows; } catch (SQLException e) { throw ServiceException.INVALID_REQUEST("No such item: mbox=" + mboxId + ", item=" + itemId, e); } finally { DbPool.closeResults(rs); DbPool.closeStatement(stmt); } }
From source file:cn.clickvalue.cv2.model.rowmapper.BeanPropertyRowMapper.java
/** * Determine the column name to use. The column name is determined based on a * lookup using ResultSetMetaData./*from ww w .ja v a 2s. c o m*/ * <p>This method implementation takes into account recent clarifications * expressed in the JDBC 4.0 specification: * <p><i>columnLabel - the label for the column specified with the SQL AS clause. * If the SQL AS clause was not specified, then the label is the name of the column</i>. * @return the column name to use * @param resultSetMetaData the current meta data to use * @param columnIndex the index of the column for the look up * @throws SQLException in case of lookup failure */ public static String lookupColumnName(ResultSetMetaData resultSetMetaData, int columnIndex) throws SQLException { String name = resultSetMetaData.getColumnLabel(columnIndex); if (name == null || name.length() < 1) { name = resultSetMetaData.getColumnName(columnIndex); } return name; }