List of usage examples for java.sql ResultSetMetaData getColumnName
String getColumnName(int column) throws SQLException;
From source file:org.dashbuilder.dataprovider.backend.sql.JDBCUtils.java
public static List<Column> getColumns(ResultSet resultSet, String[] exclude) throws SQLException { List<Column> columnList = new ArrayList<Column>(); List<String> columnExcluded = exclude == null ? new ArrayList<String>() : Arrays.asList(exclude); ResultSetMetaData meta = resultSet.getMetaData(); for (int i = 1; i <= meta.getColumnCount(); i++) { String name = meta.getColumnName(i); String alias = meta.getColumnLabel(i); if (!columnExcluded.contains(name) && !columnExcluded.contains(alias)) { ColumnType type = JDBCUtils.calculateType(meta.getColumnType(i)); int size = meta.getColumnDisplaySize(i); Column column = column(name, type, size).as(alias); columnList.add(column);/*from ww w . j a va 2 s. c o m*/ } } return columnList; }
From source file:org.dashbuilder.dataprovider.sql.JDBCUtils.java
public static List<Column> getColumns(ResultSet resultSet, String[] exclude) throws SQLException { List<Column> columnList = new ArrayList<Column>(); List<String> columnExcluded = exclude == null ? new ArrayList<String>() : Arrays.asList(exclude); ResultSetMetaData meta = resultSet.getMetaData(); for (int i = 1; i <= meta.getColumnCount(); i++) { String name = meta.getColumnName(i); String alias = meta.getColumnLabel(i); if (alias != null && !alias.trim().isEmpty()) { name = alias.trim();//from www .ja v a 2 s . com } if (!columnExcluded.contains(name) && !columnExcluded.contains(alias)) { ColumnType type = JDBCUtils.calculateType(meta.getColumnType(i)); if (type != null) { int size = meta.getColumnDisplaySize(i); Column column = SQLFactory.column(name, type, size); columnList.add(column); } } } return columnList; }
From source file:com.adaptris.core.util.JdbcUtil.java
public static Connection testConnection(Connection sqlConnection, String testStatement, boolean debugMode) throws SQLException { Statement stmt = sqlConnection.createStatement(); ResultSet rs = null;//from www . jav a 2s . c om try { if (isEmpty(testStatement)) { return sqlConnection; } if (debugMode) { rs = stmt.executeQuery(testStatement); 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("="); sb.append(rs.getObject(i)); sb.append("] "); } log.trace(sb.toString()); } } else { stmt.execute(testStatement); } } finally { JdbcUtil.closeQuietly(rs); JdbcUtil.closeQuietly(stmt); } return sqlConnection; }
From source file:Main.java
public static List<Map<String, Object>> map(ResultSet rs) throws SQLException { List<Map<String, Object>> results = new ArrayList<Map<String, Object>>(); try {/* w ww . ja va 2 s .com*/ if (rs != null) { ResultSetMetaData meta = rs.getMetaData(); int numColumns = meta.getColumnCount(); while (rs.next()) { Map<String, Object> row = new HashMap<String, Object>(); for (int i = 1; i <= numColumns; ++i) { String name = meta.getColumnName(i); Object value = rs.getObject(i); row.put(name, value); } results.add(row); } } } finally { close(rs); } return results; }
From source file:mangotiger.sql.SQL.java
public static Map<String, List<Object>> map(final Connection connection, final String sql, final Object[] parameters) throws SQLException { ResultSet resultSet = null;//from w w w . jav a2 s . c o m PreparedStatement statement = null; try { final Map<String, List<Object>> map = new TreeMap<String, List<Object>>(); statement = newPreparedStatement(connection, sql, parameters); resultSet = statement.executeQuery(); final ResultSetMetaData metaData = resultSet.getMetaData(); final int columnCount = metaData.getColumnCount(); for (int i = 0; i < columnCount; ++i) { final String columnName = metaData.getColumnName(i); map.put(columnName, new ArrayList<Object>()); } while (resultSet.next()) { for (int i = 0; i < columnCount; ++i) { final String columnName = metaData.getColumnName(i); map.get(columnName).add(resultSet.getObject(1)); } } return map; } catch (SQLException e) { log().error("unable to execute: " + asString(sql, parameters), e); throw e; } finally { close(statement, resultSet); } }
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)); }// ww w . j a v a2s .c o m return row; }
From source file:com.example.querybuilder.server.Jdbc.java
public static String getColumnName(ResultSetMetaData resultSetMetaData, int columnNumber) { try {//www. j a va 2 s . co m return resultSetMetaData.getColumnName(columnNumber); } catch (SQLException e) { throw new SqlRuntimeException(e); } }
From source file:ca.sqlpower.persistance.CatNap.java
public static void load(Connection con, String tableName, Object loadTo, String where) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, SQLException { BeanUtils.describe(loadTo);/* w w w . j ava 2 s . co m*/ Statement stmt = null; StringBuffer sql = new StringBuffer(); try { sql.append("SELECT * FROM " + tableName + " WHERE " + where); sql.append("\n"); stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(sql.toString()); while (rs.next()) { ResultSetMetaData metaData = rs.getMetaData(); for (int i = 0; i < metaData.getColumnCount(); i++) { String beanPropertyName = underscoreToCamelCaps(metaData.getColumnName(i).toLowerCase()); BeanUtils.setProperty(loadTo, beanPropertyName, rs.getObject(i)); } } } catch (SQLException ex) { System.err.println("Catnap: Insert failed. Statement was:\n" + sql); throw ex; } finally { try { if (stmt != null) stmt.close(); } catch (SQLException ex) { System.err.println( "Catnap: Couldn't close the statement. Damn. But at least you won a stack trace:"); ex.printStackTrace(); } } }
From source file:at.bestsolution.persistence.java.Util.java
public static Map<String, Object> mapResultSet(ResultSet set) throws SQLException { Map<String, Object> map = new HashMap<String, Object>(); ResultSetMetaData m = set.getMetaData(); int columnCount = m.getColumnCount(); for (int i = 0; i < columnCount; i++) { map.put(m.getColumnName(i), set.getObject(i)); }/*from w w w. ja v a2s .com*/ return map; }
From source file:eagle.storage.jdbc.entity.JdbcEntitySerDeserHelper.java
/** * * @param resultSet/*from w w w . j a va 2 s .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 = serDeser.readValue(resultSet, columnName, entityDefinition); row.put(columnName, value); } return row; }