List of usage examples for javax.sql.rowset CachedRowSet beforeFirst
void beforeFirst() throws SQLException;
ResultSet
object, just before the first row. From source file:com.telefonica.iot.cygnus.backends.mysql.MySQLBackendImpl.java
@Override public void capRecords(String dbName, String tableName, long maxRecords) throws CygnusRuntimeError, CygnusPersistenceError { // Get the records within the table CachedRowSet records = select(dbName, tableName, "*"); // Get the number of records int numRecords = 0; try {/*from ww w. j av a 2s.com*/ if (records.last()) { numRecords = records.getRow(); records.beforeFirst(); } // if } catch (SQLException e) { throw new CygnusRuntimeError("Data capping error", "SQLException", e.getMessage()); } // try catch // Get the reception times (they work as IDs) for future deletion // to-do: refactor after implementing // https://github.com/telefonicaid/fiware-cygnus/issues/1371 String filters = ""; try { if (numRecords > maxRecords) { for (int i = 0; i < (numRecords - maxRecords); i++) { records.next(); String recvTime = records.getString("recvTime"); if (filters.isEmpty()) { filters += "recvTime='" + recvTime + "'"; } else { filters += " or recvTime='" + recvTime + "'"; } // if else } // for } // if records.close(); } catch (SQLException e) { throw new CygnusRuntimeError("Data capping error", "SQLException", e.getMessage()); } // try catch if (filters.isEmpty()) { LOGGER.debug("No records to be deleted"); } else { LOGGER.debug("Records must be deleted (dbName=" + dbName + ",tableName=" + tableName + ", filters=" + filters + ")"); delete(dbName, tableName, filters); } // if else }
From source file:com.telefonica.iot.cygnus.backends.mysql.MySQLBackendImpl.java
@Override public void expirateRecordsCache(long expirationTime) throws CygnusRuntimeError, CygnusPersistenceError { // Iterate on the cached resource IDs cache.startDbIterator();//from w w w .j a va2 s. c o m while (cache.hasNextDb()) { String dbName = cache.nextDb(); cache.startTableIterator(dbName); while (cache.hasNextTable(dbName)) { String tableName = cache.nextTable(dbName); // Get the records within the table CachedRowSet records = select(dbName, tableName, "*"); // Get the number of records int numRecords = 0; try { if (records.last()) { numRecords = records.getRow(); records.beforeFirst(); } // if } catch (SQLException e) { try { records.close(); } catch (SQLException e1) { LOGGER.debug("Can't close CachedRowSet."); } throw new CygnusRuntimeError("Data expiration error", "SQLException", e.getMessage()); } // try catch // Get the reception times (they work as IDs) for future // deletion // to-do: refactor after implementing // https://github.com/telefonicaid/fiware-cygnus/issues/1371 String filters = ""; try { for (int i = 0; i < numRecords; i++) { records.next(); String recvTime = records.getString("recvTime"); long recordTime = CommonUtils.getMilliseconds(recvTime); long currentTime = new Date().getTime(); if (recordTime < (currentTime - (expirationTime * 1000))) { if (filters.isEmpty()) { filters += "recvTime='" + recvTime + "'"; } else { filters += " or recvTime='" + recvTime + "'"; } // if else } else { break; } // if else } // for } catch (SQLException e) { throw new CygnusRuntimeError("Data expiration error", "SQLException", e.getMessage()); } catch (ParseException e) { throw new CygnusRuntimeError("Data expiration error", "ParseException", e.getMessage()); } // try catch if (filters.isEmpty()) { LOGGER.debug("No records to be deleted"); } else { LOGGER.debug("Records must be deleted (dbName=" + dbName + ",tableName=" + tableName + ", filters=" + filters + ")"); delete(dbName, tableName, filters); } // if else } // while } // while }
From source file:CSVTools.CsvToolsApi.java
public void writeResultSetIntoCSVFile(CachedRowSet resultSet, String path) { PrintWriter csvWriter = null; try {/* w ww . java2 s . co m*/ csvWriter = new PrintWriter(new File(path)); ResultSetMetaData meta = resultSet.getMetaData(); int numberOfColumns = meta.getColumnCount(); String dataHeaders = "\"" + meta.getColumnName(1) + "\""; for (int i = 2; i < numberOfColumns + 1; i++) { dataHeaders += ",\"" + meta.getColumnName(i) + "\""; } csvWriter.println(dataHeaders); resultSet.beforeFirst(); while (resultSet.next()) { String row = "\"" + resultSet.getString(1) + "\""; for (int i = 2; i < numberOfColumns + 1; i++) { row += ",\"" + resultSet.getString(i) + "\""; } csvWriter.println(row); } csvWriter.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } }
From source file:Main.java
public void readData(RowSetInternal caller) throws SQLException { System.out.println("--- CustomRowSetReader: begin. ---"); if (caller == null) { System.out.println("CustomRowSetReader: caller is null."); return;/* w w w. j a v a 2 s . c om*/ } CachedRowSet crs = (CachedRowSet) caller; // CachedRowSet crs = (CachedRowSet) caller.getOriginal(); RowSetMetaData rsmd = new RowSetMetaDataImpl(); rsmd.setColumnCount(3); rsmd.setColumnType(1, Types.VARCHAR); rsmd.setColumnType(2, Types.INTEGER); rsmd.setColumnType(3, Types.VARCHAR); rsmd.setColumnName(1, "col1"); rsmd.setColumnName(2, "col2"); rsmd.setColumnName(3, "col3"); crs.setMetaData(rsmd); System.out.println("CustomRowSetReader: crs.setMetaData( rsmd );"); crs.moveToInsertRow(); crs.updateString(1, "StringCol11"); crs.updateInt(2, 1); crs.updateString(3, "StringCol31"); crs.insertRow(); System.out.println("CustomRowSetReader: crs.insertRow() 1"); crs.updateString(1, "StringCol12"); crs.updateInt(2, 2); crs.updateString(3, "StringCol32"); crs.insertRow(); System.out.println("CustomRowSetReader: crs.insertRow() 2"); crs.moveToCurrentRow(); crs.beforeFirst(); displayRowSet(crs); crs.beforeFirst(); // crs.acceptChanges(); System.out.println("CustomRowSetReader: end."); }
From source file:org.aludratest.cloud.impl.app.LogDatabase.java
/** * Runs and populates the given query against the internal Derby database. * //from w w w . ja v a2 s .c om * @param query * SQL query to execute, usually starts with <code>SELECT</code>. * * @return A cached row set containing the full results of the query. * * @throws SQLException * If a database exception occurs, e.g. invalid query. */ public CachedRowSet populateQuery(String query) throws SQLException { Connection connection = getConnection(); Statement stmt = null; try { stmt = connection.createStatement(); LOG.debug("Executing QUERY: " + query); ResultSet rs = stmt.executeQuery(query); LOG.debug("Query execution complete."); try { CachedRowSet rowSet = (CachedRowSet) Class.forName("com.sun.rowset.CachedRowSetImpl").newInstance(); rowSet.populate(rs); rowSet.beforeFirst(); return rowSet; } catch (SQLException se) { throw se; } catch (Exception e) { throw new SQLException(e); } } finally { closeQuietly(stmt); closeQuietly(connection); } }