List of usage examples for java.sql ResultSetMetaData getColumnCount
int getColumnCount() throws SQLException;
ResultSet
object. 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 www . j av a 2 s .co m 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 ava 2s .c o 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); }
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 www . j a va 2 s. c o m*/ } } }
From source file:com.bt.aloha.testing.DbTestCase.java
private static String dump(ResultSet rs) throws SQLException { ResultSetMetaData meta = rs.getMetaData(); int colmax = meta.getColumnCount(); Object o = null;//from w ww . j ava 2 s . c om StringBuffer sb = new StringBuffer(); for (; rs.next();) { for (int i = 0; i < colmax; i++) { o = rs.getObject(i + 1); if (o != null) sb.append(o.toString()); else sb.append("null"); if (i < colmax - 1) sb.append(" "); } if (!rs.isLast()) sb.append("\n"); } return sb.toString(); }
From source file:io.cloudslang.content.database.services.SQLQueryLobService.java
public static boolean executeSqlQueryLob(SQLInputs sqlInputs) throws Exception { if (StringUtils.isEmpty(sqlInputs.getSqlCommand())) { throw new Exception("command input is empty."); }/*from w w w . jav a2s . c o m*/ boolean isLOB = false; ConnectionService connectionService = new ConnectionService(); try (final Connection connection = connectionService.setUpConnection(sqlInputs)) { StringBuilder strColumns = new StringBuilder(sqlInputs.getStrColumns()); connection.setReadOnly(true); Statement statement = connection.createStatement(sqlInputs.getResultSetType(), sqlInputs.getResultSetConcurrency()); statement.setQueryTimeout(sqlInputs.getTimeout()); ResultSet results = statement.executeQuery(sqlInputs.getSqlCommand()); ResultSetMetaData mtd = results.getMetaData(); int iNumCols = mtd.getColumnCount(); for (int i = 1; i <= iNumCols; i++) { if (i > 1) strColumns.append(sqlInputs.getStrDelim()); strColumns.append(mtd.getColumnLabel(i)); } sqlInputs.setStrColumns(strColumns.toString()); int nr = -1; while (results.next()) { nr++; final StringBuilder strRowHolder = new StringBuilder(); for (int i = 1; i <= iNumCols; i++) { if (i > 1) strRowHolder.append(sqlInputs.getStrDelim()); Object columnObject = results.getObject(i); if (columnObject != null) { String value; if (columnObject instanceof java.sql.Clob) { isLOB = true; final File tmpFile = File.createTempFile("CLOB_" + mtd.getColumnLabel(i), ".txt"); copyInputStreamToFile( new ReaderInputStream(results.getCharacterStream(i), StandardCharsets.UTF_8), tmpFile); if (sqlInputs.getLRowsFiles().size() == nr) { sqlInputs.getLRowsFiles().add(nr, new ArrayList<String>()); sqlInputs.getLRowsNames().add(nr, new ArrayList<String>()); } sqlInputs.getLRowsFiles().get(nr).add(tmpFile.getAbsolutePath()); sqlInputs.getLRowsNames().get(nr).add(mtd.getColumnLabel(i)); value = "(CLOB)..."; } else { value = results.getString(i); if (sqlInputs.isNetcool()) value = SQLUtils.processNullTerminatedString(value); } strRowHolder.append(value); } else strRowHolder.append("null"); } sqlInputs.getLRows().add(strRowHolder.toString()); } } return isLOB; }
From source file:com.adaptris.jdbc.JdbcResultSetImpl.java
private static JdbcResultRow mapRow(ResultSet resultSet) throws SQLException { ResultSetMetaData rsmd = resultSet.getMetaData(); int columnCount = rsmd.getColumnCount(); JdbcResultRow row = new JdbcResultRow(); for (int counter = 1; counter <= columnCount; counter++) { row.setFieldValue(StringUtils.defaultIfEmpty(rsmd.getColumnLabel(counter), rsmd.getColumnName(counter)), resultSet.getObject(counter), rsmd.getColumnType(counter)); }// w ww .j a v a2 s .com return row; }
From source file:org.owasp.webgoat.plugin.introduction.SqlInjectionLesson5a.java
public static String writeTable(ResultSet results, ResultSetMetaData resultsMetaData) throws IOException, SQLException { int numColumns = resultsMetaData.getColumnCount(); results.beforeFirst();/*from w ww . ja v a2 s . c o m*/ StringBuffer t = new StringBuffer(); t.append("<p>"); if (results.next()) { for (int i = 1; i < (numColumns + 1); i++) { t.append(resultsMetaData.getColumnName(i)); t.append(", "); } t.append("<br />"); results.beforeFirst(); while (results.next()) { for (int i = 1; i < (numColumns + 1); i++) { t.append(results.getString(i)); t.append(", "); } t.append("<br />"); } } else { t.append("Query Successful; however no data was returned from this query."); } t.append("</p>"); return (t.toString()); }
From source file:com.datamoin.tajo.tpcds.TpcDSTestUtil.java
public static String resultSetToString(ResultSet resultSet) throws SQLException { StringBuilder sb = new StringBuilder(); ResultSetMetaData rsmd = resultSet.getMetaData(); int numOfColumns = rsmd.getColumnCount(); for (int i = 1; i <= numOfColumns; i++) { if (i > 1) sb.append(","); String columnName = rsmd.getColumnName(i); sb.append(columnName);/*from w w w . j a va 2s. c om*/ } sb.append("\n-------------------------------\n"); while (resultSet.next()) { for (int i = 1; i <= numOfColumns; i++) { if (i > 1) sb.append(","); String columnValue = resultSet.getString(i); if (resultSet.wasNull()) { columnValue = "null"; } sb.append(columnValue); } sb.append("\n"); } return sb.toString(); }
From source file:com.hangum.tadpole.commons.sql.util.SQLUtil.java
/** * metadata ./*from ww w . j av a2 s . c o m*/ * * @param rs * @return index? */ public static HashMap<Integer, String> mataDataToMap(ResultSet rs) throws Exception { HashMap<Integer, String> map = new HashMap<Integer, String>(); ResultSetMetaData rsm = rs.getMetaData(); int columnCount = rsm.getColumnCount(); for (int i = 0; i < columnCount; i++) { map.put(i, rsm.getColumnLabel(i + 1)); } return map; }
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 . j av a 2 s . c o m jsonArray.put(obj); } return jsonArray; }