List of usage examples for java.sql ResultSetMetaData getColumnLabel
String getColumnLabel(int column) throws SQLException;
From source file:com.manydesigns.portofino.persistence.QueryUtils.java
/** * Runs a SQL query against a session. The query can contain placeholders for the parameters, as supported by * {@link PreparedStatement}./*from ww w.ja v a 2 s. co m*/ * * @param session the session * @param queryString the query * @param parameters parameters to substitute in the query * @return the results of the query as an Object[] (an array cell per column) */ // hongliangpan add public static List<Map<String, Object>> runSqlReturnMap(Session session, final String queryString, final Object[] parameters) { final List<Map<String, Object>> result = new ArrayList<Map<String, Object>>(); try { session.doWork(new Work() { public void execute(Connection connection) throws SQLException { PreparedStatement stmt = connection.prepareStatement(queryString); ResultSet rs = null; try { for (int i = 0; i < parameters.length; i++) { stmt.setObject(i + 1, parameters[i]); } rs = stmt.executeQuery(); ResultSetMetaData md = rs.getMetaData(); int cc = md.getColumnCount(); while (rs.next()) { Map<String, Object> t_row = new LinkedHashMap<String, Object>(); for (int i = 0; i < cc; i++) { Object t_value = rs.getObject(i + 1); t_row.put(md.getColumnLabel(i + 1), t_value); } result.add(t_row); } } finally { if (null != rs) { rs.close(); } if (null != stmt) { stmt.close(); } } } }); } catch (HibernateException e) { session.getTransaction().rollback(); session.beginTransaction(); throw e; } return result; }
From source file:com.linuxrouter.netcool.session.QueryUtils.java
public ArrayList<HashMap<String, Object>> executeQuery(String dbName, String sql) { Long start = System.currentTimeMillis(); ArrayList<HashMap<String, Object>> result = new ArrayList<>(); HashMap<Integer, String> colTypes = new HashMap<Integer, String>(); HashMap<Integer, String> colNames = new HashMap<Integer, String>(); try {// w w w.java 2s . c o m //connection caching... Connection con = null; if (connectionMap.get(dbName) == null) { BasicDataSource ds = DbUtils.getSimpleDataSourceByName(dbName); con = ds.getConnection(); connectionMap.put(dbName, con); } else { con = connectionMap.get(dbName); } Statement st = con.createStatement(); ResultSet rs = st.executeQuery(sql); ResultSetMetaData metaData = rs.getMetaData(); int colCount = metaData.getColumnCount(); for (int i = 1; i <= colCount; i++) { colTypes.put(i, metaData.getColumnTypeName(i)); colNames.put(i, metaData.getColumnLabel(i)); } while (rs.next()) { HashMap<String, Object> dado = new HashMap<>(); for (int i = 1; i <= colCount; i++) { dado.put(colNames.get(i), rs.getObject(i)); } result.add(dado); } rs.close(); st.close(); //con.close(); Long end = System.currentTimeMillis(); //logger.debug("Query on external DB took: " + (end - start) + "ms"); } catch (SQLException ex) { logger.error("Erro ao executar query:", ex); } return result; }
From source file:net.mlw.vlh.adapter.jdbc.util.ResultSetMapGenerator.java
public ResultSetMapGenerator(ResultSet result, boolean useName, boolean lowerCase) throws SQLException { this.result = result; ResultSetMetaData metadata = result.getMetaData(); int columnCount = metadata.getColumnCount(); names = new String[columnCount]; for (int i = 0; i < columnCount; i++) { names[i] = (useName) ? metadata.getColumnName(i + 1) : metadata.getColumnLabel(i + 1); if (names[i] == null || names[i].length() == 0) { names[i] = (useName) ? metadata.getColumnLabel(i + 1) : metadata.getColumnName(i + 1); }/*from ww w. j a va2 s. com*/ if (lowerCase) { names[i] = names[i].toLowerCase(); } } LOGGER.debug(names); }
From source file:org.bireme.interop.toJson.SQL2Json.java
private List<String> getColName(final ResultSetMetaData rsmd) throws SQLException { assert rsmd != null; final List<String> lst = new ArrayList<>(); final int num = rsmd.getColumnCount(); for (int idx = 1; idx <= num; idx++) { lst.add(rsmd.getColumnLabel(idx)); }//from w w w . jav a 2 s . c o m return lst; }
From source file:com.gdo.project.util.SqlUtils.java
/** * Tests if a column exists in the result set. * // w w w. ja v a2 s. c o m * @param field * the label column searched. * @param rs * the result set. * @return <tt>true</tt> if the column exists, <tt>false</tt> otherwise. */ public static boolean hasColumn(String field, ResultSet rs) throws SQLException { ResultSetMetaData meta = rs.getMetaData(); int numCol = meta.getColumnCount(); for (int i = 1; i < numCol + 1; i++) { if (meta.getColumnLabel(i).equals(field)) { return true; } } return false; }
From source file:velo.adapters.VeloDbUtilsRowProcessor.java
/** * Convert a <code>ResultSet</code> row into a <code>Map</code>. This * implementation returns a <code>Map</code> with case insensitive column * names as keys. Calls to <code>map.get("COL")</code> and * <code>map.get("col")</code> return the same value. * @see org.apache.commons.dbutils.RowProcessor#toMap(java.sql.ResultSet) *///from w w w .j a v a 2 s . c o m public Map toMap(ResultSet rs) throws SQLException { Map result = new CaseInsensitiveHashMap(); ResultSetMetaData rsmd = rs.getMetaData(); int cols = rsmd.getColumnCount(); for (int i = 1; i <= cols; i++) { result.put(rsmd.getColumnLabel(i), rs.getObject(i)); } return result; }
From source file:com.opencsv.ResultSetHelperService.java
@Override public String[] getColumnNames(ResultSet rs) throws SQLException { ResultSetMetaData metadata = rs.getMetaData(); String[] nameArray = new String[metadata.getColumnCount()]; for (int i = 0; i < metadata.getColumnCount(); i++) { nameArray[i] = metadata.getColumnLabel(i + 1); }//w ww.ja v a 2s . c om return nameArray; }
From source file:net.mlw.vlh.adapter.jdbc.dynabean.fix.JDBCDynaClass.java
/** * <p>Factory method to create a new DynaProperty for the given index * into the result set metadata.</p> * //ww w . j a va2 s.co m * @param metadata is the result set metadata * @param i is the column index in the metadata * @return the newly created DynaProperty instance */ protected DynaProperty createDynaProperty(ResultSetMetaData metadata, int i) throws SQLException { String name = (useName) ? metadata.getColumnName(i) : metadata.getColumnLabel(i); if (lowerCase) { name = name.toLowerCase(); } String className = null; try { className = metadata.getColumnClassName(i); } catch (SQLException e) { // this is a patch for HsqlDb to ignore exceptions // thrown by its metadata implementation } // Default to Object type if no class name could be retrieved // from the metadata Class clazz = Object.class; if (className != null) { clazz = loadClass(className); } return new DynaProperty(name, clazz); }
From source file:cherry.foundation.etl.ExtractorResultSetExtractor.java
@Override public Long extractData(ResultSet rs) throws SQLException, DataAccessException { try {//from w w w.ja v a2 s .c om ResultSetMetaData metaData = rs.getMetaData(); Column[] col = new Column[metaData.getColumnCount()]; for (int i = 1; i <= col.length; i++) { col[i - 1] = new Column(); col[i - 1].setType(metaData.getColumnType(i)); col[i - 1].setLabel(metaData.getColumnLabel(i)); } consumer.begin(col); long count; for (count = 0L; rs.next(); count++) { Object[] record = new Object[col.length]; for (int i = 1; i <= record.length; i++) { record[i - 1] = rs.getObject(i); } consumer.consume(record); limiter.tick(); } consumer.end(); return count; } catch (IOException ex) { throw new IllegalStateException(ex); } }
From source file:de.tudarmstadt.ukp.dkpro.core.io.jdbc.JdbcReader.java
private void query() throws ResourceInitializationException { try {//from w w w . j a v a 2 s .c o m Statement statement = sqlConnection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); resultSet = statement.executeQuery(query); resultSet.last(); resultSetSize = resultSet.getRow(); resultSet.beforeFirst(); completed = 0; // Store available column names columnNames = new HashSet<String>(); ResultSetMetaData meta = resultSet.getMetaData(); for (int i = 1; i < meta.getColumnCount() + 1; i++) { String columnName = meta.getColumnLabel(i); columnNames.add(columnName); if (!CAS_COLUMNS.contains(columnName)) { getLogger().warn("Unknown column [" + columnName + "]."); } } } catch (SQLException e) { throw new ResourceInitializationException(e); } }