List of usage examples for java.sql ResultSet getMetaData
ResultSetMetaData getMetaData() throws SQLException;
ResultSet
object's columns. From source file:com.mapd.utility.SQLImporter.java
void executeQuery() { Connection conn = null;//w ww . j av a 2 s.com Statement stmt = null; long totalTime = 0; try { //Open a connection LOGGER.info("Connecting to database url :" + cmd.getOptionValue("jdbcConnect")); conn = DriverManager.getConnection(cmd.getOptionValue("jdbcConnect"), cmd.getOptionValue("sourceUser"), cmd.getOptionValue("sourcePasswd")); long startTime = System.currentTimeMillis(); //Execute a query stmt = conn.createStatement(); long timer; ResultSet rs = stmt.executeQuery(cmd.getOptionValue("sqlStmt")); //check if table already exists and is compatible in MapD with the query metadata ResultSetMetaData md = rs.getMetaData(); checkMapDTable(md); timer = System.currentTimeMillis(); long resultCount = 0; int bufferCount = 0; long total = 0; int bufferSize = Integer.valueOf(cmd.getOptionValue("bufferSize", "10000")); List<TStringRow> rows = new ArrayList(bufferSize); while (rs.next()) { TStringRow tsr = new TStringRow(); for (int i = 1; i <= md.getColumnCount(); i++) { // place string in rows array TStringValue tsv = new TStringValue(); tsv.str_val = rs.getString(i); if (rs.wasNull()) { tsv.is_null = true; } else { tsv.is_null = false; } tsr.addToCols(tsv); } rows.add(tsr); resultCount++; bufferCount++; if (bufferCount == bufferSize) { bufferCount = 0; //send the buffer to mapD client.load_table(session, cmd.getOptionValue("targetTable"), rows); rows.clear(); if (resultCount % 100000 == 0) { LOGGER.info("Imported " + resultCount + " records"); } } } if (bufferCount > 0) { //send the LAST buffer to mapD client.load_table(session, cmd.getOptionValue("targetTable"), rows); rows.clear(); bufferCount = 0; } LOGGER.info("result set count is " + resultCount + " read time is " + (System.currentTimeMillis() - timer) + "ms"); //Clean-up environment rs.close(); stmt.close(); totalTime = System.currentTimeMillis() - startTime; conn.close(); } catch (SQLException se) { LOGGER.error("SQLException - " + se.toString()); se.printStackTrace(); } catch (TMapDException ex) { LOGGER.error("TMapDException - " + ex.toString()); ex.printStackTrace(); } catch (TException ex) { LOGGER.error("TException failed - " + ex.toString()); ex.printStackTrace(); } finally { //finally block used to close resources try { if (stmt != null) { stmt.close(); } } catch (SQLException se2) { } // nothing we can do try { if (conn != null) { conn.close(); } } catch (SQLException se) { LOGGER.error("SQlException in close - " + se.toString()); se.printStackTrace(); } //end finally try } //end try }
From source file:com.hexin.core.dao.BaseDaoSupport.java
@Override public <T> List<T> findListWithBlob(String sql, Class<T> dtoClass, Object... args) throws SQLException, InstantiationException, IllegalAccessException, SecurityException, IllegalArgumentException, NoSuchFieldException, IOException { long startTime = System.currentTimeMillis(); long endTime; long durTime; debugSql(sql, args);//ww w.j a v a 2 s .c o m PreparedStatement ps = jdbcTemplate.getDataSource().getConnection().prepareStatement(sql); setPreparedStatementParameter(ps, args); List<T> list = new ArrayList<T>(); ResultSet rs = ps.executeQuery(); while (rs.next()) { ResultSetMetaData rsmd = rs.getMetaData(); int colCount = rsmd.getColumnCount(); T obj = dtoClass.newInstance(); for (int i = 1; i <= colCount; i++) { String colName = rsmd.getColumnLabel(i); // ?? String colTypeName = rsmd.getColumnTypeName(i); String beanFiledName = IcpObjectUtil.underlineToCamel(colName); if ("blob".equalsIgnoreCase(colTypeName)) { InjectValueUtil.setFieldValue(obj, beanFiledName, rs.getBlob(i)); } else { InjectValueUtil.setFieldValue(obj, beanFiledName, rs.getObject(i)); } } list.add(obj); } endTime = System.currentTimeMillis(); durTime = endTime - startTime; logger.debug("This jdbc operation costs time: " + durTime); return list; }
From source file:com.nextep.datadesigner.sqlgen.impl.generator.DataSetGenerator.java
private ISQLScript buildScript(IDataSet set, IDataSet valuesSet, DeltaType type) { final ISQLScript script = CorePlugin.getTypedObjectFactory().create(ISQLScript.class); script.setScriptType(ScriptType.DATA); script.setName(type.name().toLowerCase() + "." + set.getName()); //$NON-NLS-1$ final StringBuilder buf = new StringBuilder(2000); final ISQLParser parser = SQLGenPlugin.getService(IGenerationService.class).getCurrentSQLParser(); // final IDataService dataService = DbgmPlugin.getService(IDataService.class); final IStorageService storageService = DbgmPlugin.getService(IStorageService.class); final IStorageHandle handle = valuesSet.getStorageHandle(); if (handle != null) { Connection conn = null;//from w ww. j a va 2s . c o m Statement stmt = null; ResultSet rset = null; try { conn = storageService.getLocalConnection(); stmt = conn.createStatement(); stmt.execute(handle.getSelectStatement()); rset = stmt.getResultSet(); final ResultSetMetaData md = rset.getMetaData(); while (rset.next()) { final List<Object> values = new LinkedList<Object>(); for (int i = 1; i <= md.getColumnCount(); i++) { values.add(rset.getObject(i)); } switch (type) { case INSERT: buf.append(buildInsert(parser, set, values)); break; case UPDATE: buf.append(buildUpdate(parser, set, values)); break; case DELETE: buf.append(buildDelete(parser, set, values)); break; } } } catch (SQLException e) { throw new ErrorException("Data generation problem: " + e.getMessage(), e); } finally { safeClose(rset, stmt, conn); } } if (buf.length() == 0) { return null; } else { script.appendSQL(buf.toString()); return script; } }
From source file:org.tradex.jdbc.JDBCHelper.java
/** * Executes the passed SQL and returns the results in a 2D object array * @param sql The SQL query to executer/*from w ww .ja v a 2s . co m*/ * @return the results of the query */ public Object[][] query(CharSequence sql) { Connection conn = null; PreparedStatement ps = null; ResultSet rset = null; Vector<Object[]> rows = new Vector<Object[]>(); try { conn = ds.getConnection(); ps = conn.prepareStatement(sql.toString()); rset = ps.executeQuery(); int colCount = rset.getMetaData().getColumnCount(); while (rset.next()) { Object[] row = new Object[colCount]; for (int i = 1; i <= colCount; i++) { row[i - 1] = rset.getObject(i); } rows.add(row); } Object[][] result = new Object[rows.size()][]; int cnt = 0; for (Object[] row : rows) { result[cnt] = row; cnt++; } return result; } catch (Exception e) { throw new RuntimeException("Query for [" + sql + "] failed", e); } finally { try { rset.close(); } catch (Exception e) { } try { ps.close(); } catch (Exception e) { } try { conn.close(); } catch (Exception e) { } } }
From source file:edu.ku.brc.specify.toycode.mexconabio.CopyPlantsFromGBIF.java
/** * // w w w . j a va 2 s.c o m */ public void processNonNullNonPlantKingdom() { PrintWriter pw = null; try { pw = new PrintWriter("gbif_plants_from_nonnull.log"); } catch (FileNotFoundException e) { e.printStackTrace(); } System.out.println("----------------------- Search non-Plantae ----------------------- "); String gbifWhereStr = "FROM raw WHERE kingdom = '%s'"; Vector<String> nonPlantKingdoms = new Vector<String>(); String sqlStr = "SELECT * FROM (select kingdom, count(kingdom) as cnt from plants.raw WHERE kingdom is not null AND NOT (lower(kingdom) like '%plant%') group by kingdom) T1 ORDER BY cnt desc;"; for (Object[] obj : BasicSQLUtils.query(sqlStr)) { String kingdom = (String) obj[0]; Integer count = (Integer) obj[1]; System.out.println(kingdom + " " + count); pw.println(kingdom + " " + count); if (!StringUtils.contains(kingdom.toLowerCase(), "plant")) { nonPlantKingdoms.add(kingdom); } } long startTime = System.currentTimeMillis(); for (String kingdom : nonPlantKingdoms) { String where = String.format(gbifWhereStr, kingdom); String cntGBIFSQL = "SELECT COUNT(*) " + where; String gbifSQL = gbifSQLBase + where; System.out.println(cntGBIFSQL); long totalRecs = BasicSQLUtils.getCount(srcConn, cntGBIFSQL); long procRecs = 0; int secsThreshold = 0; String msg = String.format("Query: %8.2f secs", (double) (System.currentTimeMillis() - startTime) / 1000.0); System.out.println(msg); pw.println(msg); pw.flush(); startTime = System.currentTimeMillis(); Statement gStmt = null; PreparedStatement pStmt = null; try { pStmt = dstConn.prepareStatement(pSQL); System.out.println("Total Records: " + totalRecs); pw.println("Total Records: " + totalRecs); pw.flush(); gStmt = srcConn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); gStmt.setFetchSize(Integer.MIN_VALUE); ResultSet rs = gStmt.executeQuery(gbifSQL); ResultSetMetaData rsmd = rs.getMetaData(); while (rs.next()) { String genus = rs.getString(16); if (genus == null) continue; String species = rs.getString(17); if (isPlant(colStmtGN, colStmtGNSP, genus, species) || isPlant(colDstStmtGN, colDstStmtGNSP, genus, species)) { for (int i = 1; i <= rsmd.getColumnCount(); i++) { Object obj = rs.getObject(i); pStmt.setObject(i, obj); } try { pStmt.executeUpdate(); } catch (Exception ex) { System.err.println("For Old ID[" + rs.getObject(1) + "]"); ex.printStackTrace(); pw.print("For Old ID[" + rs.getObject(1) + "] " + ex.getMessage()); pw.flush(); } procRecs++; if (procRecs % 10000 == 0) { long endTime = System.currentTimeMillis(); long elapsedTime = endTime - startTime; double avergeTime = (double) elapsedTime / (double) procRecs; double hrsLeft = (((double) elapsedTime / (double) procRecs) * (double) totalRecs - procRecs) / HRS; int seconds = (int) (elapsedTime / 60000.0); if (secsThreshold != seconds) { secsThreshold = seconds; msg = String.format( "Elapsed %8.2f hr.mn Ave Time: %5.2f Percent: %6.3f Hours Left: %8.2f ", ((double) (elapsedTime)) / HRS, avergeTime, 100.0 * ((double) procRecs / (double) totalRecs), hrsLeft); System.out.println(msg); pw.println(msg); pw.flush(); } } } } } catch (Exception ex) { ex.printStackTrace(); } finally { try { if (gStmt != null) { gStmt.close(); } if (pStmt != null) { pStmt.close(); } pw.close(); } catch (Exception ex) { } } } System.out.println("Done transferring."); pw.println("Done transferring."); }
From source file:com.diversityarrays.dal.server.SqlDialog.java
public int doSqlQuery(ResultSet rs, List<String> headings, List<String[]> rows) throws SQLException { int nColumns = -1; while (rs.next()) { if (nColumns < 0) { ResultSetMetaData rsmd = rs.getMetaData(); nColumns = rsmd.getColumnCount(); for (int i = 1; i <= nColumns; ++i) { String hdg = rsmd.getColumnLabel(i); headings.add(hdg);//from w w w. j a v a2 s . co m } } String[] values = new String[nColumns]; rows.add(values); for (int i = 1; i <= nColumns; ++i) { values[i - 1] = rs.getString(i); } } return nColumns; }
From source file:org.syncope.core.util.ImportExport.java
private void setParameters(final String tableName, final Attributes atts, final Query query) { Map<String, Integer> colTypes = new HashMap<String, Integer>(); Connection conn = DataSourceUtils.getConnection(dataSource); ResultSet rs = null; Statement stmt = null;//from w w w. j a v a 2s. c o m try { stmt = conn.createStatement(); rs = stmt.executeQuery("SELECT * FROM " + tableName); for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) { colTypes.put(rs.getMetaData().getColumnName(i + 1).toUpperCase(), rs.getMetaData().getColumnType(i + 1)); } } catch (SQLException e) { LOG.error("While", e); } finally { if (stmt != null) { try { stmt.close(); } catch (SQLException e) { LOG.error("While closing statement", e); } } if (rs != null) { try { rs.close(); } catch (SQLException e) { LOG.error("While closing result set", e); } } DataSourceUtils.releaseConnection(conn, dataSource); } for (int i = 0; i < atts.getLength(); i++) { Integer colType = colTypes.get(atts.getQName(i).toUpperCase()); if (colType == null) { LOG.warn("No column type found for {}", atts.getQName(i).toUpperCase()); colType = Types.VARCHAR; } switch (colType) { case Types.NUMERIC: case Types.REAL: case Types.INTEGER: case Types.TINYINT: try { query.setParameter(i + 1, Integer.valueOf(atts.getValue(i))); } catch (NumberFormatException e) { LOG.error("Unparsable Integer '{}'", atts.getValue(i)); query.setParameter(i + 1, atts.getValue(i)); } break; case Types.DECIMAL: case Types.BIGINT: try { query.setParameter(i + 1, Long.valueOf(atts.getValue(i))); } catch (NumberFormatException e) { LOG.error("Unparsable Long '{}'", atts.getValue(i)); query.setParameter(i + 1, atts.getValue(i)); } break; case Types.DOUBLE: try { query.setParameter(i + 1, Double.valueOf(atts.getValue(i))); } catch (NumberFormatException e) { LOG.error("Unparsable Double '{}'", atts.getValue(i)); query.setParameter(i + 1, atts.getValue(i)); } break; case Types.FLOAT: try { query.setParameter(i + 1, Float.valueOf(atts.getValue(i))); } catch (NumberFormatException e) { LOG.error("Unparsable Float '{}'", atts.getValue(i)); query.setParameter(i + 1, atts.getValue(i)); } break; case Types.DATE: case Types.TIME: case Types.TIMESTAMP: try { query.setParameter(i + 1, DateUtils.parseDate(atts.getValue(i), SyncopeConstants.DATE_PATTERNS), TemporalType.TIMESTAMP); } catch (ParseException e) { LOG.error("Unparsable Date '{}'", atts.getValue(i)); query.setParameter(i + 1, atts.getValue(i)); } break; case Types.BIT: case Types.BOOLEAN: query.setParameter(i + 1, "1".equals(atts.getValue(i)) ? Boolean.TRUE : Boolean.FALSE); break; default: query.setParameter(i + 1, atts.getValue(i)); } } }
From source file:com.tascape.reactor.report.MySqlBaseBean.java
public List<Map<String, Object>> dumpResultSetToList(ResultSet rs) throws SQLException { List<Map<String, Object>> rsml = new ArrayList<>(); ResultSetMetaData rsmd = rs.getMetaData(); while (rs.next()) { Map<String, Object> d = new LinkedHashMap<>(); for (int col = 1; col <= rsmd.getColumnCount(); col++) { d.put(rsmd.getColumnLabel(col), rs.getObject(col)); }/*www.ja v a 2 s.c om*/ rsml.add(d); } LOG.trace("{} rows loaded", rsml.size()); return rsml; }
From source file:com.mx.core.dao.BeanPropRowMap.java
/** * Extract the values for all columns in the current row. * <p>Utilizes public setters and result set metadata. * @see java.sql.ResultSetMetaData// www. j a v a2 s . com */ public T mapRow(ResultSet rs, int rowNumber) throws SQLException { Assert.state(this.mappedClass != null, "Mapped class was not specified"); T mappedObject = BeanUtils.instantiate(this.mappedClass); BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(mappedObject); initBeanWrapper(bw); ResultSetMetaData rsmd = rs.getMetaData(); int columnCount = rsmd.getColumnCount(); Set<String> populatedProperties = (isCheckFullyPopulated() ? new HashSet<String>() : null); for (int index = 1; index <= columnCount; index++) { String column = JdbcUtils.lookupColumnName(rsmd, index); PropertyDescriptor pd = this.mappedFields.get(column.replaceAll(" ", "").toLowerCase()); if (pd != null) { try { Object value = getColumnValue(rs, index, pd); if (logger.isDebugEnabled() && rowNumber == 0) { //logger.debug("Mapping column '" + column + "' to property '" + // pd.getName() + "' of type " + pd.getPropertyType()); } try { bw.setPropertyValue(pd.getName(), value); } catch (TypeMismatchException e) { if (value == null && primitivesDefaultedForNullValue) { logger.debug("Intercepted TypeMismatchException for row " + rowNumber + " and column '" + column + "' with value " + value + " when setting property '" + pd.getName() + "' of type " + pd.getPropertyType() + " on object: " + mappedObject); } else { throw e; } } if (populatedProperties != null) { populatedProperties.add(pd.getName()); } } catch (NotWritablePropertyException ex) { throw new DataRetrievalFailureException( "Unable to map column " + column + " to property " + pd.getName(), ex); } } } if (populatedProperties != null && !populatedProperties.equals(this.mappedProperties)) { throw new InvalidDataAccessApiUsageException("Given ResultSet does not contain all fields " + "necessary to populate object of class [" + this.mappedClass + "]: " + this.mappedProperties); } return mappedObject; }
From source file:com.erbjuder.logger.server.rest.util.ResultSetConverter.java
private List<LogMessageData> toLogMessageDataInternal(ResultSet rs, List<LogMessageData> logMessageData) { try {//ww w. j a va2 s. c o m // we will need the column names. java.sql.ResultSetMetaData rsmd = rs.getMetaData(); //loop through the ResultSet while (rs.next()) { //figure out how many columns there are int numColumns = rsmd.getColumnCount(); String tableName = rsmd.getTableName(1); LogMessageData obj = null; if (tableName.equals(DataBase.LOGMESSAGEDATA_PARTITION_01_NAME)) { obj = new LogMessageData_Partition_01(); } else if (tableName.equals(DataBase.LOGMESSAGEDATA_PARTITION_02_NAME)) { obj = new LogMessageData_Partition_02(); } else if (tableName.equals(DataBase.LOGMESSAGEDATA_PARTITION_03_NAME)) { obj = new LogMessageData_Partition_03(); } else if (tableName.equals(DataBase.LOGMESSAGEDATA_PARTITION_04_NAME)) { obj = new LogMessageData_Partition_04(); } else if (tableName.equals(DataBase.LOGMESSAGEDATA_PARTITION_05_NAME)) { obj = new LogMessageData_Partition_05(); } else if (tableName.equals(DataBase.LOGMESSAGEDATA_PARTITION_06_NAME)) { obj = new LogMessageData_Partition_06(); } else if (tableName.equals(DataBase.LOGMESSAGEDATA_PARTITION_07_NAME)) { obj = new LogMessageData_Partition_07(); } else if (tableName.equals(DataBase.LOGMESSAGEDATA_PARTITION_08_NAME)) { obj = new LogMessageData_Partition_08(); } else if (tableName.equals(DataBase.LOGMESSAGEDATA_PARTITION_09_NAME)) { obj = new LogMessageData_Partition_09(); } else if (tableName.equals(DataBase.LOGMESSAGEDATA_PARTITION_10_NAME)) { obj = new LogMessageData_Partition_10(); } else if (tableName.equals(DataBase.LOGMESSAGEDATA_PARTITION_11_NAME)) { obj = new LogMessageData_Partition_11(); } else if (tableName.equals(DataBase.LOGMESSAGEDATA_PARTITION_12_NAME)) { obj = new LogMessageData_Partition_12(); } else if (tableName.equals(DataBase.LOGMESSAGEDATA_PARTITION_13_NAME)) { obj = new LogMessageData_Partition_13(); } else if (tableName.equals(DataBase.LOGMESSAGEDATA_PARTITION_14_NAME)) { obj = new LogMessageData_Partition_14(); } else if (tableName.equals(DataBase.LOGMESSAGEDATA_PARTITION_15_NAME)) { obj = new LogMessageData_Partition_15(); } else if (tableName.equals(DataBase.LOGMESSAGEDATA_PARTITION_16_NAME)) { obj = new LogMessageData_Partition_16(); } else if (tableName.equals(DataBase.LOGMESSAGEDATA_PARTITION_17_NAME)) { obj = new LogMessageData_Partition_17(); } // loop through all the columns for (int i = 1; i < numColumns + 1; i++) { String column_name = rsmd.getColumnName(i); if (column_name.equals("ID")) { obj.setId(rs.getBigDecimal(column_name).longValueExact()); } if (column_name.equals("CONTENT")) { obj.setContent(rs.getNString(column_name)); } if (column_name.equals("LABEL")) { obj.setLabel(rs.getNString(column_name)); } if (column_name.equals("MIMETYPE")) { obj.setMimeType(rs.getNString(column_name)); } if (column_name.equals("MODIFIED")) { obj.setModified(rs.getBoolean(column_name)); } if (column_name.equals("CONTENTSIZE")) { obj.setContentSize(rs.getBigDecimal(column_name).longValueExact()); } if (column_name.equals("SEARCHABLE")) { obj.setSearchable(rs.getBoolean(column_name)); } if (column_name.equals("UTCLOCALTIMESTAMP")) { obj.setUtcLocalTimeStamp(rs.getTimestamp(column_name)); } if (column_name.equals("UTCSERVERTIMESTAMP")) { obj.setUtcServerTimeStamp(rs.getTimestamp(column_name)); } // if (column_name.equals("LOGMESSAGE_ID")) { // obj.setUtcServerTimeStamp(rs.getTimestamp(column_name)); // } } //end foreach logMessageData.add(obj); } //end while } catch (Exception e) { e.printStackTrace(); } return logMessageData; }