List of usage examples for java.sql ResultSetMetaData getColumnCount
int getColumnCount() throws SQLException;
ResultSet
object. From source file:net.orpiske.ssps.common.repository.search.cache.PackageCacheRsHandler.java
@Override public PackageInfo handle(ResultSet rs) throws SQLException { // No records to handle :O if (!rs.next()) { return null; }/*from w w w .j ava 2 s . com*/ 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); if (javaProperty.equals("version")) { Version version = Version.toVersion((String) value); PropertyUtils.setSimpleProperty(dto, javaProperty, version); } else { 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:edu.stanford.junction.sample.sql.QueryHandler.java
@Override public void onMessageReceived(MessageHeader header, JSONObject message) { //String query = q.getQueryText(); String query = message.optString("query"); query = query.toLowerCase();// w w w. ja va 2 s. co m if (!query.contains("select")) return; if (query.contains("drop") || query.contains("delete")) return; System.out.println("Got query: " + query); Connection connection = null; try { // Load the JDBC driver String driverName = "com.mysql.jdbc.Driver"; // MySQL MM JDBC driver Class.forName(driverName); // Create a connection to the database //String serverName = "192.168.1.122"; String serverName = "127.0.0.1"; String mydatabase = "jinzora3"; String url = "jdbc:mysql://" + serverName + "/" + mydatabase; // a JDBC url String username = "jinzora"; String password = "jinzora"; connection = DriverManager.getConnection(url, username, password); } catch (ClassNotFoundException e) { // Could not find the database driver e.printStackTrace(); } catch (SQLException e) { // Could not connect to the database e.printStackTrace(); } try { Statement stmt = connection.createStatement(); ResultSet rs = stmt.executeQuery(query); ResultSetMetaData rsMetaData = rs.getMetaData(); int cols = rsMetaData.getColumnCount(); while (rs.next()) { JSONObject row = new JSONObject(); try { for (int i = 1; i <= cols; i++) { // stupid indexing row.put(rsMetaData.getColumnName(i), rs.getObject(i)); } } catch (JSONException e) { e.printStackTrace(); } System.out.println("sending " + row); if (mActor != null) { //mActor.getJunction().sendMessageToTarget(header.getReplyTarget(),row); header.getReplyTarget().sendMessage(row); } } } catch (SQLException e) { e.printStackTrace(); } System.out.println("closing stream."); //results.close(); }
From source file:com.opensource.dbhelp.dbutils.CamelBeanProcessor.java
/** * /*from w w w .j ava 2 s .c o m*/ * * @param rsmd * * @param props * * @return ?? */ @Override protected int[] mapColumnsToProperties(ResultSetMetaData rsmd, PropertyDescriptor[] props) throws SQLException { int cols = rsmd.getColumnCount(); int columnToProperty[] = new int[cols + 1]; Arrays.fill(columnToProperty, PROPERTY_NOT_FOUND); for (int col = 1; col <= cols; col++) { String columnName = rsmd.getColumnLabel(col); if (null == columnName || 0 == columnName.length()) { columnName = rsmd.getColumnName(col); } for (int i = 0; i < props.length; i++) { if (formatColName(columnName).equalsIgnoreCase(props[i].getName())) { columnToProperty[col] = i; break; } if (columnName.equalsIgnoreCase(props[i].getName())) { columnToProperty[col] = i; break; } } } return columnToProperty; }
From source file:org.jasig.services.persondir.support.jdbc.ColumnMapParameterizedRowMapper.java
public final Map<String, Object> mapRow(ResultSet rs, int rowNum) throws SQLException { final ResultSetMetaData rsmd = rs.getMetaData(); final int columnCount = rsmd.getColumnCount(); final Map<String, Object> mapOfColValues = this.createColumnMap(columnCount); for (int i = 1; i <= columnCount; i++) { final String columnName = JdbcUtils.lookupColumnName(rsmd, i); final Object obj = this.getColumnValue(rs, i); if (!this.ignoreNull || obj != null) { final String key = this.getColumnKey(columnName); mapOfColValues.put(key, obj); }//from w w w .j a va2 s .co m } return mapOfColValues; }
From source file:jp.co.golorp.emarf.model.Models.java
/** * Model?????IO// w w w. j av a2s . c o m * * @param sql * sql * @param params * params * @return List */ private static List<Map<String, Object>> getDatas(final String sql, final Object... params) { // ?SQL? String rawSql = getRawSql(sql, params); // SQL?????? List<Map<String, Object>> datas = ModelsCache.get(rawSql); if (datas != null) { return datas; } // statementLog(rawSql); // ? PreparedStatement ps = null; try { ps = Connections.get().prepareStatement(sql); for (int i = 0; i < params.length; i++) { ps.setString(i + 1, String.valueOf(params[i])); } // ?? datas = new ArrayList<Map<String, Object>>(); ResultSet rs = null; try { // ??? rs = ps.executeQuery(); while (rs.next()) { // ? Map<String, Object> data = new LinkedHashMap<String, Object>(); // ResultSet?META? ResultSetMetaData meta = rs.getMetaData(); // META???? int columnCount = meta.getColumnCount(); for (int i = 1; i <= columnCount; i++) { // ??? String columnName = meta.getColumnName(i); // ??? String propertyName = StringUtil.toCamelCase(columnName); String key = propertyName; if (data.containsKey(propertyName)) { String modelName = StringUtil.toUpperCamelCase(meta.getTableName(i)); key = modelName + "." + propertyName; } // data.put(key, rs.getObject(columnName)); } // datas.add(data); } } catch (SQLException e) { throw new SystemError(e); } finally { IOUtil.closeQuietly(rs); } } catch (SQLException e) { throw new SystemError(e); } finally { IOUtil.closeQuietly(ps); } // SQL? // ModelsCache.set(rawSql, datas); return datas; }
From source file:org.apereo.services.persondir.support.jdbc.ColumnMapParameterizedRowMapper.java
@Override public final Map<String, Object> mapRow(final ResultSet rs, final int rowNum) throws SQLException { final ResultSetMetaData rsmd = rs.getMetaData(); final int columnCount = rsmd.getColumnCount(); final Map<String, Object> mapOfColValues = this.createColumnMap(columnCount); for (int i = 1; i <= columnCount; i++) { final String columnName = JdbcUtils.lookupColumnName(rsmd, i); final Object obj = this.getColumnValue(rs, i); if (!this.ignoreNull || obj != null) { final String key = this.getColumnKey(columnName); mapOfColValues.put(key, obj); }//ww w .ja v a 2s .c o m } return mapOfColValues; }
From source file:ResultsDecoratorText.java
public void write(ResultSet rs) throws IOException, SQLException { ResultSetMetaData md = rs.getMetaData(); int cols = md.getColumnCount(); for (int i = 1; i <= cols; i++) { print(md.getColumnName(i) + "\t"); }/*from w ww . j a va 2 s . co m*/ println(); while (rs.next()) { for (int i = 1; i <= cols; i++) { print(rs.getString(i) + "\t"); } println(); } }
From source file:io.stallion.dataAccess.BeanListHandler.java
private String[] makeColumnToProperty(ResultSetMetaData rsmd, List<String> propertyNames) throws SQLException { int cols = rsmd.getColumnCount(); String[] columnToProperty = new String[cols + 1]; Arrays.fill(columnToProperty, ""); for (int col = 1; col <= cols; ++col) { String columnName = rsmd.getColumnLabel(col); if (null == columnName || 0 == columnName.length()) { columnName = rsmd.getColumnName(col); }//from ww w . j a v a 2 s. c o m for (int i = 0; i < propertyNames.size(); ++i) { String propertyName = propertyNames.get(i); if (propertyName.equalsIgnoreCase(columnName)) { columnToProperty[col] = propertyName; break; } } } return columnToProperty; }
From source file:com.dangdang.ddframe.rdb.sharding.merger.component.other.WrapperResultSet.java
/** * ?./*w w w. ja va 2s . com*/ * * @return * @throws SQLException ?? */ public Map<String, Integer> getColumnLabelIndexMap() throws SQLException { ResultSetMetaData resultSetMetaData = getDelegate().getMetaData(); Map<String, Integer> result = new CaseInsensitiveMap<>(resultSetMetaData.getColumnCount()); for (int i = 1; i <= resultSetMetaData.getColumnCount(); i++) { result.put(resultSetMetaData.getColumnLabel(i), i); } return result; }
From source file:jongo.handler.ResultSetMetaDataHandler.java
@Override public List<Row> handle(ResultSet rs) throws SQLException { List<Row> results = new ArrayList<Row>(); int rowId = 0; ResultSetMetaData metaData = rs.getMetaData(); Map<String, String> map = null; for (int i = 1; i <= metaData.getColumnCount(); i++) { map = new HashMap<String, String>(2); map.put("tableName", metaData.getTableName(i)); map.put("columnName", metaData.getColumnName(i)); map.put("columnLabel", metaData.getColumnLabel(i)); map.put("columnType", metaData.getColumnTypeName(i)); map.put("columnSize", String.valueOf(metaData.getColumnDisplaySize(i))); map.put("precision", String.valueOf(metaData.getPrecision(i))); map.put("scale", String.valueOf(metaData.getScale(i))); // map.put("catalog_name", metaData.getCatalogName(i)); // map.put("column_class_name", metaData.getColumnClassName(i)); // map.put("schema_name", metaData.getSchemaName(i)); // map.put("column_type", String.valueOf(metaData.getColumnType(i))); if (map != null) results.add(new Row(rowId++, map)); }/*from ww w .jav a2s . c o m*/ return results; }