List of usage examples for java.sql ResultSetMetaData getColumnCount
int getColumnCount() throws SQLException;
ResultSet
object. From source file:org.jtalks.poulpe.util.databasebackup.persistence.DbTableData.java
/** * The method prepares table's data in the shape and passes every {@link Row} into given RowProcessor. * /*from w w w. j a v a 2 s. c om*/ * @param processor * injected logic to perform some actions under passing rows. see details for {@link RowProcessor}. * @throws SQLException * if any errors during work with database occur. */ public void getData(final RowProcessor processor) throws SQLException { try { jdbcTemplate.query(SELECT_FROM + tableName, new RowCallbackHandler() { @Override public void processRow(ResultSet rs) throws SQLException { ResultSetMetaData metaData = rs.getMetaData(); int columnCount = metaData.getColumnCount(); Row row = new Row(); for (int i = 1; i <= columnCount; i++) { ColumnMetaData columnMetaData = ColumnMetaData.getInstance(metaData.getColumnName(i), SqlTypes.getSqlTypeByJdbcSqlType(metaData.getColumnType(i))); row.addCell(columnMetaData, rs.getObject(i)); } try { processor.process(row); } catch (RowProcessingException e) { throw new SQLException(e); } } }); } catch (DataAccessException e) { throw new SQLException(e); } }
From source file:com.gdo.project.util.SqlUtils.java
/** * Tests if a column exists in the result set. * /* w w w .j a va2s . c om*/ * @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:uk.org.rbc1b.roms.controller.report.ReportsController.java
private ReportResults extractResults(String sql) throws SQLException { Connection con = DataSourceUtils.getConnection(dataSource); Statement s = con.createStatement(); ResultSet rs = s.executeQuery(sql); ReportResults reportResults = new ReportResults(); ResultSetMetaData rsmd = rs.getMetaData(); int columnCount = rsmd.getColumnCount(); List<Integer> columnTypeIds = new ArrayList<Integer>(); for (int i = 0; i < columnCount; i++) { columnTypeIds.add(rsmd.getColumnType(i + 1)); }//from ww w . ja v a 2s . c o m reportResults.columnNames = new ArrayList<String>(); for (int i = 0; i < columnCount; i++) { reportResults.columnNames.add(rsmd.getColumnLabel(i + 1)); } reportResults.resultRows = new ArrayList<List<String>>(); while (rs.next()) { List<String> resultRow = new ArrayList<String>(); for (int i = 0; i < columnCount; i++) { Integer columnTypeId = columnTypeIds.get(i); if (columnTypeId.intValue() == Types.BOOLEAN || columnTypeId.intValue() == Types.BIT) { resultRow.add(Boolean.valueOf(rs.getBoolean(i + 1)).toString()); } else { resultRow.add(rs.getString(i + 1)); } } reportResults.resultRows.add(resultRow); } return reportResults; }
From source file:de.ufinke.cubaja.sql.ObjectFactoryGenerator.java
private void createSearchMap(ResultSetMetaData metaData) throws SQLException { int size = metaData.getColumnCount(); searchMap = new HashMap<String, SearchEntry>(); for (int i = 1; i <= size; i++) { String name = metaData.getColumnName(i).toLowerCase(); SearchEntry entry = new SearchEntry(name, i, metaData.getColumnType(i)); searchMap.put(Util.createMethodName(name, "set"), entry); }// w ww .j a va 2s . c o m if (searchMap.size() == 1) { singleColumnSqlType = metaData.getColumnType(1); } }
From source file:gridool.util.csv.CsvWriter.java
private void writeColumnNames(final ResultSetMetaData metadata) throws SQLException { final int numColumns = metadata.getColumnCount(); for (int i = 1; i <= numColumns; i++) { if (i != 1) { write(separator);// w w w. j a va 2 s . c om } String columnName = metadata.getColumnName(i); write(columnName); } write(lineSeparator); }
From source file:com.vangent.hieos.logbrowser.util.TableModel.java
public TableModel(String sqlRequest, Map fieldsAndFormats, Connection c) throws SQLException { this.fieldsAndFormats = fieldsAndFormats; ResultSet statementResult;//from w ww . ja v a2 s .c o m log.debug("TABLE_MODEL_SYSLOG: database connection created\n"); Statement statement = c.createStatement(); log.debug("TABLE_MODEL_SYSLOG: statement created\n"); statementResult = statement.executeQuery(sqlRequest); log.debug("TABLE_MODEL_SYSLOG: Query executed\n"); log.debug("<--" + new GregorianCalendar().getTime() + " TableModel close Database \n"); ResultSetMetaData metaData = statementResult.getMetaData(); int columnCount = metaData.getColumnCount(); dataVector = new Vector<Vector<Object>>(); headerVector = new Vector<String>(); log.debug("TABLE_MODEL_SYSLOG: colomn count : " + columnCount + "\n"); log.debug("TABLE_MODEL_SYSLOG: Table--------------------------------------"); for (int i = 0; i < columnCount; i++) { headerVector.add(metaData.getColumnName((i + 1))); log.debug(metaData.getColumnName((i + 1)) + "\t"); } while (statementResult.next()) { Vector<Object> tmp = new Vector<Object>(columnCount); for (int j = 0; j < columnCount; j++) { String columnName = getColumnName(j); Object columnData = statementResult.getObject(columnName); columnData = getFormattedData(columnName, columnData); tmp.add(columnData); log.debug(columnData + "\t"); } log.debug("\n"); dataVector.add(tmp); } }
From source file:com.myapp.dao.SalesOrderDAO.java
public ArrayList<SalesOrder> getAllOrders() throws SQLException { ArrayList<SalesOrder> retVal = null; try {//from ww w . j ava 2s. co m // Load the driver. Class.forName("org.relique.jdbc.csv.CsvDriver"); // Create a connection. CSV file is in D: Connection conn = DriverManager.getConnection("jdbc:relique:csv:C:"); // Create a Statement object to execute the query with. Statement stmt = conn.createStatement(); // Select columns from SalesOrder.csv ResultSet rs = stmt.executeQuery("SELECT * FROM SalesOrder"); //loop through rs // Clean up ResultSetMetaData metaData = rs.getMetaData(); int resultColumnCount = metaData.getColumnCount(); while (rs.next()) { if (resultColumnCount > 1) { SalesOrder so = new SalesOrder(); so.setSalesOrderID(rs.getString(1)); so.setRevisionNumber(rs.getString(2)); so.setOrderDate(rs.getString(3)); so.setDueDate(rs.getString(4)); so.setShipDate(rs.getString(5)); so.setStatus(rs.getString(6)); so.setOnlineOrderFlag(rs.getString(7)); so.setSalesOrderNumber(rs.getString(8)); so.setPurchaseOrderNumber(rs.getString(9)); so.setAccountNumber(rs.getString(10)); so.setCustomerID(rs.getString(11)); so.setSalesPersonID(rs.getString(12)); so.setTerritoryID(rs.getString(13)); so.setBillToAddressID(rs.getString(14)); so.setShipToAddressID(rs.getString(15)); so.setShipMethodID(rs.getString(16)); so.setCreditCardID(rs.getString(17)); so.setCreditCardApprovalCode(rs.getString(18)); so.setCurrencyRateID(rs.getString(19)); so.setSubTotal(rs.getString(20)); so.setTaxAmt(rs.getString(21)); so.setFreight(rs.getString(22)); so.setTotalDue(rs.getString(23)); so.setComment(rs.getString(24)); so.setModifiedDate(rs.getString(25)); retVal.add(so); } else { Object obj = rs.getObject(1); } } conn.close(); } catch (Exception e) { System.out.println("EXCEPTION: " + e.getMessage()); } return retVal; }
From source file:annis.sqlgen.FindSqlGenerator.java
public Match mapRow(ResultSet rs, int rowNum) throws SQLException { Match match = new Match(); // get size of solution ResultSetMetaData metaData = rs.getMetaData(); int columnCount = metaData.getColumnCount(); // the order of columns is not determined and I have to combined two // values, so save them here and combine later String node_name = null;//from w w w . j a v a 2 s. c o m List<String> corpus_path = null; //get path if (outputCorpusPath) { for (int column = 1; column <= columnCount; ++column) { if (corpusPathExtractor != null && metaData.getColumnName(column).startsWith("path_name")) { corpus_path = corpusPathExtractor.extractCorpusPath(rs, metaData.getColumnName(column)); } } } // one match per column for (int column = 1; column <= columnCount; ++column) { if (metaData.getColumnName(column).startsWith("node_name")) { node_name = rs.getString(column); } else // no more matches in this row if an id was NULL if (rs.wasNull()) { break; } if (outputCorpusPath && node_name != null) { match.setSaltId(buildSaltId(corpus_path, node_name)); node_name = null; } } return match; }
From source file:kr.co.bitnine.octopus.testutils.MemoryDatabaseTest.java
private void verifyTableEquals(JSONObject expectedTable, ResultSet actualRows) throws Exception { ResultSetMetaData actualRowsMetaData = actualRows.getMetaData(); JSONArray expectedSchema = (JSONArray) expectedTable.get("table-schema"); assertEquals(expectedSchema.size(), actualRowsMetaData.getColumnCount()); for (int i = 0; i < expectedSchema.size(); i++) assertEquals(expectedSchema.get(i), actualRowsMetaData.getColumnName(i + 1)); for (Object rowObj : (JSONArray) expectedTable.get("table-rows")) { JSONArray row = (JSONArray) rowObj; actualRows.next();// w ww .j a v a 2s . co m for (int i = 0; i < row.size(); i++) { Object expected = row.get(i); Object actual; int sqlType = actualRowsMetaData.getColumnType(i + 1); switch (sqlType) { case Types.INTEGER: if (expected instanceof Boolean) expected = (long) ((Boolean) expected ? 1 : 0); actual = actualRows.getLong(i + 1); break; case Types.NULL: case Types.FLOAT: case Types.VARCHAR: actual = actualRows.getObject(i + 1); break; default: throw new RuntimeException("java.sql.Types " + sqlType + " is not supported"); } assertEquals(expected, actual); } } assertFalse(actualRows.next()); }
From source file:com.netspective.axiom.sql.QueryResultSet.java
public void fillReportFromMetaData(TabularReport report) throws SQLException { ResultSetMetaData rsmd = resultSet.getMetaData(); int numColumns = rsmd.getColumnCount(); TabularReportColumns columns = report.getColumns(); columns.clear();//from ww w . jav a2 s .c o m for (int c = 1; c <= numColumns; c++) { TabularReportColumn column = null; int dataType = rsmd.getColumnType(c); switch (dataType) { case Types.INTEGER: case Types.SMALLINT: case Types.BIGINT: case Types.TINYINT: case Types.BIT: column = new NumericColumn(); break; case Types.FLOAT: case Types.REAL: column = new DecimalColumn(); break; case Types.NUMERIC: case Types.DECIMAL: if (rsmd.getScale(c) > 0) column = new DecimalColumn(); else column = new NumericColumn(); break; default: column = new GeneralColumn(); break; } column.setColIndex(c - 1); column.setHeading(new StaticValueSource( TextUtils.getInstance().sqlIdentifierToText(rsmd.getColumnLabel(c), true))); column.setDataType(dataType); column.setWidth(rsmd.getColumnDisplaySize(c)); columns.add(column); } report.finalizeContents(); }