List of usage examples for java.sql ResultSet getObject
Object getObject(String columnLabel) throws SQLException;
Gets the value of the designated column in the current row of this ResultSet
object as an Object
in the Java programming language.
From source file:at.bestsolution.persistence.java.Util.java
public static Map<String, Object> mapResultSet(ResultSet set) throws SQLException { Map<String, Object> map = new HashMap<String, Object>(); ResultSetMetaData m = set.getMetaData(); int columnCount = m.getColumnCount(); for (int i = 0; i < columnCount; i++) { map.put(m.getColumnName(i), set.getObject(i)); }// w w w . j ava 2s .c o m return map; }
From source file:it.greenvulcano.gvesb.datahandling.dbo.utils.ResultSetTransformer.java
public static JSONArray toJSONArray(ResultSet resultSet) throws SQLException { JSONArray queryResult = new JSONArray(); int columns = resultSet.getMetaData().getColumnCount(); List<String> names = new ArrayList<>(columns); for (int i = 1; i <= columns; i++) { names.add(resultSet.getMetaData().getColumnLabel(i)); }// w w w. j a v a2 s . c o m while (resultSet.next()) { JSONObject obj = new JSONObject(); for (String key : names) { Object jsonValue = parseValue(resultSet.getObject(key)); if (key.contains(".")) { String[] hieararchy = key.split("\\."); JSONObject child = Optional.ofNullable(obj.optJSONObject(hieararchy[0])) .orElse(new JSONObject()); child.put(hieararchy[1], Optional.ofNullable(jsonValue).orElse(JSONObject.NULL)); obj.put(hieararchy[0], child); } else { obj.put(key, Optional.ofNullable(jsonValue).orElse(JSONObject.NULL)); } } queryResult.put(obj); } return queryResult; }
From source file:org.apache.drill.jdbc.test.JdbcAssert.java
static String toString(ResultSet resultSet) throws SQLException { StringBuilder buf = new StringBuilder(); final List<Ord<String>> columns = columnLabels(resultSet); while (resultSet.next()) { for (Ord<String> column : columns) { buf.append(column.i == 1 ? "" : "; ").append(column.e).append("=") .append(resultSet.getObject(column.i)); }//from w w w.j a v a 2 s . c o m buf.append("\n"); } return buf.toString(); }
From source file:desktop.olayinka.file.transfer.model.DerbyJDBCHelper.java
public static JSONArray convert(ResultSet rs) throws SQLException, JSONException { ResultSetMetaData rsmd = rs.getMetaData(); int numColumns = rsmd.getColumnCount(); JSONArray jsonArray = new JSONArray(); while (rs.next()) { JSONObject obj = new JSONObject(); for (int i = 1; i < numColumns + 1; i++) { String columnName = rsmd.getColumnName(i); obj.put(columnName, rs.getObject(i)); }//from w w w . jav a 2 s .c o m jsonArray.put(obj); } return jsonArray; }
From source file:com.thoughtworks.go.server.database.DatabaseFixture.java
public static Object[][] query(String query, H2Database h2Database) { BasicDataSource source = h2Database.createDataSource(); Connection con = null;/* w w w . j a va2 s. c om*/ Statement stmt = null; ResultSet rs = null; try { con = source.getConnection(); stmt = con.createStatement(); rs = stmt.executeQuery(query); int columnCount = rs.getMetaData().getColumnCount(); List<Object[]> objects = new ArrayList<>(); while (rs.next()) { Object[] values = new Object[columnCount]; for (int i = 0; i < values.length; i++) { values[i] = rs.getObject(i + 1); } objects.add(values); } return objects.toArray(new Object[0][0]); } catch (SQLException e) { throw new RuntimeException(e); } finally { try { assert stmt != null; stmt.close(); con.close(); assert rs != null; rs.close(); } catch (SQLException e) { throw new RuntimeException(e); } } }
From source file:ca.sqlpower.persistance.CatNap.java
public static void load(Connection con, String tableName, Object loadTo, String where) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, SQLException { BeanUtils.describe(loadTo);//from w ww .j av a2 s . c o m Statement stmt = null; StringBuffer sql = new StringBuffer(); try { sql.append("SELECT * FROM " + tableName + " WHERE " + where); sql.append("\n"); stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(sql.toString()); while (rs.next()) { ResultSetMetaData metaData = rs.getMetaData(); for (int i = 0; i < metaData.getColumnCount(); i++) { String beanPropertyName = underscoreToCamelCaps(metaData.getColumnName(i).toLowerCase()); BeanUtils.setProperty(loadTo, beanPropertyName, rs.getObject(i)); } } } catch (SQLException ex) { System.err.println("Catnap: Insert failed. Statement was:\n" + sql); throw ex; } finally { try { if (stmt != null) stmt.close(); } catch (SQLException ex) { System.err.println( "Catnap: Couldn't close the statement. Damn. But at least you won a stack trace:"); ex.printStackTrace(); } } }
From source file:net.antidot.sql.model.core.SQLConnector.java
/** * Tool : print in output meta data column names from a result set * // w ww. j a va 2s. com * @param tablesSet * @throws SQLException */ public static void printMetaColumnsFromTable(ResultSet tablesSet) throws SQLException { if (log.isInfoEnabled()) log.info("[printMetaColumnsFromTable] "); for (int i = 0; i < tablesSet.getMetaData().getColumnCount(); i++) { String columnName = tablesSet.getMetaData().getColumnName(i + 1); Object value = tablesSet.getObject(i + 1); System.out.println(columnName + " = " + value); } }
From source file:Student.java
public static void checkData() throws Exception { Class.forName("org.hsqldb.jdbcDriver"); Connection conn = DriverManager.getConnection("jdbc:hsqldb:data/tutorial", "sa", ""); Statement st = conn.createStatement(); ResultSet mrs = conn.getMetaData().getTables(null, null, null, new String[] { "TABLE" }); while (mrs.next()) { String tableName = mrs.getString(3); System.out.println("\n\n\n\nTable Name: " + tableName); ResultSet rs = st.executeQuery("select * from " + tableName); ResultSetMetaData metadata = rs.getMetaData(); while (rs.next()) { System.out.println(" Row:"); for (int i = 0; i < metadata.getColumnCount(); i++) { System.out.println(" Column Name: " + metadata.getColumnLabel(i + 1) + ", "); System.out.println(" Column Type: " + metadata.getColumnTypeName(i + 1) + ": "); Object value = rs.getObject(i + 1); System.out.println(" Column Value: " + value + "\n"); }/*from w w w . jav a 2s. c om*/ } } }
From source file:Main.java
public static List<Map<String, Object>> map(ResultSet rs) throws SQLException { List<Map<String, Object>> results = new ArrayList<Map<String, Object>>(); try {/*from ww w . jav a2s . com*/ if (rs != null) { ResultSetMetaData meta = rs.getMetaData(); int numColumns = meta.getColumnCount(); while (rs.next()) { Map<String, Object> row = new HashMap<String, Object>(); for (int i = 1; i <= numColumns; ++i) { String name = meta.getColumnName(i); Object value = rs.getObject(i); row.put(name, value); } results.add(row); } } } finally { close(rs); } return results; }
From source file:com.splout.db.engine.JDBCManager.java
public static QueryResult convertResultSetToQueryResult(ResultSet rs, int maxResults) throws SQLException { ResultSetMetaData md = rs.getMetaData(); int columns = md.getColumnCount(); String[] columnNames = new String[columns]; for (int i = 0; i < columns; i++) { columnNames[i] = md.getColumnName(i); }/*w w w. j a va 2 s .co m*/ List<Object[]> list = new ArrayList<Object[]>(); while (rs.next() && list.size() < maxResults) { Object[] row = new Object[columns]; for (int i = 1; i <= columns; ++i) { row[i] = rs.getObject(i); } list.add(row); } if (list.size() == maxResults) { throw new SQLException("Hard limit on number of results reached (" + maxResults + "), please use a LIMIT for this query."); } return new QueryResult(columnNames, list); }