List of usage examples for java.sql ResultSetMetaData getColumnLabel
String getColumnLabel(int column) throws SQLException;
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 w ww. java2 s. co 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; }
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 w w w .j av a2 s.c o m*/ } } return columnList; }
From source file:it.alidays.mapengine.codegenerator.MapperEngineCodeGenerator.java
private static void manageRetrieve(Retrieve retrieve, Connection connection, String packageName, File destinationDir)/*from ww w .ja v a2 s. com*/ throws SQLException, JClassAlreadyExistsException, IOException, MapperEngineCodeGeneratorException { logger.info("Generating map for {}", retrieve.getId()); int vuidCount = RetrieveHandler.getVuidCount(retrieve.getContent()); String content = retrieve.getContent().replaceAll(RetrieveHandler.VUID_KEY, "?"); Map<String, Integer> columns = new LinkedHashMap<>(); logger.info("\tRetrieving columns' name"); try (PreparedStatement preparedStatement = connection.prepareStatement(content)) { for (int index = 1; index <= vuidCount; index++) { preparedStatement.setObject(index, "_"); } ResultSet resultSet = preparedStatement.executeQuery(); ResultSetMetaData resultSetMetaData = resultSet.getMetaData(); for (int i = 1, n = resultSetMetaData.getColumnCount(); i <= n; i++) { String columnName = Utils.arrangeColumnName(resultSetMetaData.getColumnLabel(i)); Integer columnType = resultSetMetaData.getColumnType(i); columns.put(columnName, columnType); } } logger.info("\tRetrieved {} columns' name", columns.size()); createMapClass(retrieve, columns, packageName, destinationDir); createRetrieveClass(retrieve, packageName, destinationDir); logger.info("Map successfully generated for {}", retrieve.getId()); }
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 w w w.j av a2s. c om } 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: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 w w w. j a v a2 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:common.DB.java
public static ResultList query(String query) { ArrayList list = new ArrayList(); ResultList result = new ResultList(list); try {/*from w ww . j a v a 2 s .co m*/ DB.getConnection(); if (connection != null) { statement = connection.createStatement(); resultSet = statement.executeQuery(query); ResultSetMetaData md = resultSet.getMetaData(); int columns = md.getColumnCount(); list.add(new HashMap(columns)); while (resultSet.next()) { HashMap row = new HashMap(columns); for (int i = 1; i <= columns; ++i) { row.put(md.getColumnLabel(i), resultSet.getString(i)); } list.add(row); } } else { return null; } } catch (SQLException e) { e.printStackTrace(); } close(); return result; }
From source file:wzw.sql.ResultSetConverter.java
/** * Convert a ResultSet Object to List<Map>. * @param rs/* ww w . java2 s . c o m*/ * @return * @throws SQLException */ public static List<Map<String, Object>> toMapList(ResultSet rs) throws SQLException { if (rs == null) { return null; } List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); ResultSetMetaData rsmd = rs.getMetaData(); if (rsmd.getColumnCount() >= 0) { while (rs.next()) { Map<String, Object> temph = new Hashtable<String, Object>(); for (int j = 1; j <= rsmd.getColumnCount(); j++) { if (rs.getObject(j) != null) { // Null temph.put(rsmd.getColumnLabel(j).toLowerCase(), rs.getObject(j)); } } list.add(temph); } } return list; }
From source file:com.hangum.tadpole.engine.sql.util.resultset.ResultSetUtils.java
/** * column label name of table// ww w . j a va 2 s.co m * * @param isShowRowNum * @param rs * @return * @throws Exception */ public static Map<Integer, String> getColumnLabelName(boolean isShowRowNum, ResultSet rs) throws Exception { Map<Integer, String> mapColumnName = new HashMap<Integer, String>(); int intStartIndex = 0; if (isShowRowNum) { intStartIndex++; mapColumnName.put(0, "#"); } ResultSetMetaData rsm = rs.getMetaData(); for (int i = 0; i < rsm.getColumnCount(); i++) { mapColumnName.put(i + intStartIndex, rsm.getColumnLabel(i + 1)); } return mapColumnName; }
From source file:org.silverpeas.dbbuilder.sql.QueryExecutor.java
public static List<Map<String, Object>> executeLoopQuery(Connection connection, String query, Object[] parameters) throws Exception { Statement stmt = null;/* w w w .j a va 2 s. com*/ PreparedStatement pstmt = null; ArrayList array = new ArrayList(); ResultSet results = null; try { if (parameters == null) { stmt = connection.createStatement(); results = stmt.executeQuery(query); } else { pstmt = connection.prepareStatement(query); for (int i = 0; i < parameters.length; i++) { pstmt.setObject(i + 1, parameters[i]); } results = pstmt.executeQuery(); } ResultSetMetaData meta = results.getMetaData(); // Tant qu'on a des enregistrements dans le result set while (results.next()) { // Stockage d'un enregistrement HashMap<String, Object> h = new HashMap<String, Object>(meta.getColumnCount()); // Pour chaque colonne du result set for (int i = 1; i <= meta.getColumnCount(); i++) { Object ob = results.getObject(i); h.put(meta.getColumnLabel(i).toUpperCase(), ob); } array.add(h); } } catch (SQLException sqlex) { } finally { DbUtils.closeQuietly(results); DbUtils.closeQuietly(stmt); DbUtils.closeQuietly(pstmt); } return array; }
From source file:com.tesora.dve.client.ClientTestNG.java
private static void verifyMetadata(ResultSetMetaData rsmd, String[] namesAndAliases) throws SQLException { if (namesAndAliases == null) { for (int i = 1; i <= rsmd.getColumnCount(); i++) { System.out.println(rsmd.getColumnName(i) + " as " + rsmd.getColumnLabel(i)); }//ww w . j a v a2 s . com return; } int ncolumns = namesAndAliases.length / 2; assertEquals(ncolumns, rsmd.getColumnCount()); for (int i = 0; i < ncolumns; i++) { String ename = namesAndAliases[2 * i]; String ealias = namesAndAliases[2 * i + 1]; assertEquals(ename, rsmd.getColumnName(i + 1)); assertEquals(ealias, rsmd.getColumnLabel(i + 1)); } }