List of usage examples for java.sql ResultSet getMetaData
ResultSetMetaData getMetaData() throws SQLException;
ResultSet
object's columns. From source file:com.krawler.workflow.module.dao.DataObjectRowMapper.java
public Object mapRow(ResultSet rs, int rowNumber) throws SQLException { Map<String, Object> mappedObject = new HashMap<String, Object>(); ResultSetMetaData rsmd = rs.getMetaData(); int columnCount = rsmd.getColumnCount(); for (int index = 1; index <= columnCount; index++) { String column = columnMap.get(index); if (column == null) { column = JdbcUtils.lookupColumnName(rsmd, index).toLowerCase(); columnMap.put(index, column); }/*from ww w .j av a 2 s .com*/ Object value = rs.getObject(index); if (formatter != null && value != null && value instanceof Date) value = formatter.format(value); mappedObject.put(column, value); } return mappedObject; }
From source file:com.nilesh.GenericResourse.java
/** * Retrieves representation of an instance of com.nilesh.GenericResourse * @return an instance of java.lang.String *///from www . j a v a 2s . co m @GET @Path("/products") @Produces(MediaType.APPLICATION_JSON) public ArrayList<product> getXml() throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException { //TODO return proper representation object Statement smt = conn.createStatement(); ResultSet rs = smt.executeQuery("select * from product"); ResultSetMetaData rsmd = rs.getMetaData(); int col = rsmd.getColumnCount(); while (rs.next()) { product pro = new product(rs.getInt("productid"), rs.getString("name"), rs.getString("description"), rs.getInt("quantity")); products.add(pro); } return products; }
From source file:ca.fastenalcompany.order.OrderManager.java
public void connect() { DBManager db = new DBManager(); Connection conn = null;/*from w ww. ja v a 2 s . com*/ try { conn = db.getMysqlConn(); String query = "select * from inventory"; Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(query); while (rs.next()) { for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) System.out.println(rs.getString(i)); } } catch (SQLException ex) { Logger.getLogger(FastenalCompany.class.getName()).log(Level.SEVERE, null, ex); } finally { try { if (conn != null) conn.close(); } catch (SQLException ex) { Logger.getLogger(FastenalCompany.class.getName()).log(Level.SEVERE, null, ex); } } }
From source file:com.qualogy.qafe.business.integration.rdb.MetaDataRowMapper.java
/** * Method for mapping rows inherited from RowMapper. * Method maps rows to a Map using columnname. *///www . j av a 2s. c o m public Object mapRow(ResultSet rs, int rowNum) throws SQLException { // Map<String, Object> output = new CaseInsensitiveMap(); Map<String, Object> output = new DataMap(); ResultSetMetaData md = rs.getMetaData(); for (int i = 1; i <= md.getColumnCount(); i++) { output.put(md.getColumnName(i), rs.getObject(i)); } return output; }
From source file:com.aw.core.dao.AWQueryExecuter.java
private String[] buildColumnNames(ResultSet rs, BeanWrapper wrapper) throws SQLException { ResultSetMetaData meta = rs.getMetaData(); List<String> colNames = new ArrayList<String>(meta.getColumnCount()); for (int i = 0; i < meta.getColumnCount(); i++) { String colName = meta.getColumnName(i + 1); if (wrapper != null && wrapper.isWritableProperty(colName)) colNames.add(colName);// w ww .j av a 2 s . co m } return colNames.toArray(new String[colNames.size()]); }
From source file:org.wte4j.impl.service.SimpleDbViewModelService.java
@Override public WteDataModel createModel(Template<?> template, Object input) { String viewName = template.getProperties().get(VIEW_NAME); String pkColumnName = template.getProperties().get(PRIMARY_KEY_COLUMN_NAME); Integer pk = (Integer) input; Map<String, Object> dataMap = new HashMap<String, Object>(); //TODO PreparedStatement! String query = "select * from " + viewName + " where " + pkColumnName + "=" + pk; try {/*from w w w .j a v a 2 s .c o m*/ Connection connection = null; Statement statement = null; try { connection = ds.getConnection(); statement = connection.createStatement(); ResultSet rs = statement.executeQuery(query); ResultSetMetaData metaData = rs.getMetaData(); while (rs.next()) { for (int i = 1; i <= metaData.getColumnCount(); i++) { String columnName = metaData.getColumnName(i).toLowerCase(); dataMap.put(columnName, rs.getObject(i)); } } } finally { if (statement != null) { statement.close(); } if (connection != null) { connection.close(); } } } catch (SQLException e) { throw new WteException("error in createModel (" + viewName + ", " + pkColumnName + ", " + pk + ")", e); } return new WteMapModel(dataMap); }
From source file:BadProfessor.java
public void checkData(String sql) throws Exception { ResultSet rs = st.executeQuery(sql); ResultSetMetaData metadata = rs.getMetaData(); for (int i = 0; i < metadata.getColumnCount(); i++) { System.out.print("\t" + metadata.getColumnLabel(i + 1)); }// w w w . j a va 2s . c o m System.out.println("\n----------------------------------"); while (rs.next()) { for (int i = 0; i < metadata.getColumnCount(); i++) { Object value = rs.getObject(i + 1); if (value == null) { System.out.print("\t "); } else { System.out.print("\t" + value.toString().trim()); } } System.out.println(""); } }
From source file:net.orpiske.ssps.common.db.SimpleRsHandler.java
@Override public T handle(ResultSet rs) throws SQLException { // No records to handle :O if (!rs.next()) { return null; }//from ww w . j ava 2 s. co m ResultSetMetaData meta = rs.getMetaData(); for (int i = 1; i <= meta.getColumnCount(); i++) { Object value = rs.getObject(i); String name = meta.getColumnName(i); try { /* * We convert the column name to a more appropriate and java like name * because some columns are usually named as some_thing whereas Java * properties are named someThing. This call does this conversion. */ String javaProperty = NameConverter.sqlToProperty(name); PropertyUtils.setSimpleProperty(dto, javaProperty, value); } catch (Exception e) { throw new SQLException("Unable to set property " + name + " for bean" + dto.getClass(), e); } } return dto; }
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 w ww.j a v a 2s. c o m if (lowerCase) { names[i] = names[i].toLowerCase(); } } LOGGER.debug(names); }
From source file:com.arsmentis.cordova.jdbc.Jdbc.java
private JSONArray execute(String sql) throws SQLException, JSONException { if (connection == null) { throw new SQLException("Not connected"); }//from w w w . j ava 2 s. co m JSONArray results = new JSONArray(); Statement statement = connection.createStatement(); if (statement.execute(sql)) { ResultSet resultSet = statement.getResultSet(); ResultSetMetaData columns = resultSet.getMetaData(); while (resultSet.next()) { JSONObject row = new JSONObject(); for (int i = 1; i <= columns.getColumnCount(); i++) { row.put(columns.getColumnName(i), resultSet.getObject(i)); } results.put(row); } resultSet.close(); } statement.close(); return results; }