List of usage examples for java.sql ResultSet first
boolean first() throws SQLException;
ResultSet
object. From source file:net.mms_projects.copy_it.api.oauth.HeaderVerifier.java
/** * Used to verify that the provided nonce is not used earlier in the past 5 minutes, make sure you called * verifyOAuthToken first//www . j a va 2 s. co m * @throws OAuthException Thrown if it was used earlier */ public void verifyOAuthNonce(Database database) throws SQLException, OAuthException { if (user == null) /* Should NEVER happen! */ throw new OAuthException("user is null!"); final String oauth_nonce = oauth_params.get(OAuthParameters.OAUTH_NONCE); PreparedStatement statement = database.getConnection().prepareStatement(NONCE_CHECKING_QUERY); statement.setString(1, oauth_nonce); statement.setInt(2, user.getUserId()); ResultSet result = statement.executeQuery(); if (result.first()) { result.close(); throw new OAuthException(ErrorMessages.USED_NONCE); } result.close(); PreparedStatement insertStatement = database.getConnection().prepareStatement(NONCE_INSERT_QUERY); insertStatement.setInt(1, user.getUserId()); insertStatement.setString(2, oauth_nonce); insertStatement.execute(); database.getConnection().commit(); }
From source file:com.google.enterprise.connector.salesforce.storetype.DBStore.java
public void setDocList(String checkpoint, String str_store_entry) { DatabaseMetaData dbm = null;/*from w w w .j ava 2 s.c o m*/ Connection connection = null; logger.log(Level.FINEST, "Setting doclist " + checkpoint); logger.log(Level.FINEST, "Setting store_entry " + str_store_entry); try { connection = ds.getConnection(); connection.setAutoCommit(true); dbm = connection.getMetaData(); //logger.log(Level.FINE,"Base64 ENCODING..."); String encode_entry = new String( org.apache.commons.codec.binary.Base64.encodeBase64(str_store_entry.getBytes())); str_store_entry = encode_entry; //logger.log(Level.FINE,"Setting store_entry ENCODED " + str_store_entry); if (dbm.getDatabaseProductName().equals("MySQL")) { //get the most recent row Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); String update_stmt = "select crawl_set from " + this.instance_table + " where crawl_set=(select max(crawl_set) from " + this.instance_table + ")"; logger.log(Level.FINE, "Getting lastentryp in db: " + update_stmt); ResultSet rs = statement.executeQuery(update_stmt); boolean ret_rows = rs.first(); String last_entry_in_db = null; while (ret_rows) { BigDecimal crawl_set = rs.getBigDecimal("crawl_set"); last_entry_in_db = crawl_set.toPlainString(); ret_rows = rs.next(); } logger.log(Level.FINER, "Last_Entry_in_Database " + last_entry_in_db); if (last_entry_in_db != null) { if (last_entry_in_db.startsWith(checkpoint)) { //increment if in the same set BigDecimal bd = new BigDecimal(last_entry_in_db); bd = bd.add(new BigDecimal(".00001")); logger.log(Level.INFO, "Adding to DBStore. Index Value: " + bd.toPlainString()); update_stmt = "insert into " + this.instance_table + " (crawl_set,crawl_data) values (?,COMPRESS(?))"; PreparedStatement ps = connection.prepareStatement(update_stmt); ps.setString(1, bd.toPlainString()); ps.setString(2, str_store_entry); ps.executeUpdate(); ps.close(); } else { //otherwise add the the 0th row for this set logger.log(Level.INFO, "Adding to DBStore. Index Value: " + checkpoint + ".00000"); update_stmt = "insert into " + this.instance_table + " (crawl_set,crawl_data) values (?,COMPRESS(?))"; PreparedStatement ps = connection.prepareStatement(update_stmt); ps.setString(1, checkpoint + ".00000"); ps.setString(2, str_store_entry); ps.executeUpdate(); ps.close(); } } else { logger.log(Level.INFO, "Adding to DBStore. Index Value: " + checkpoint + ".00000"); update_stmt = "insert into " + this.instance_table + " (crawl_set,crawl_data) values (?,COMPRESS(?))"; PreparedStatement ps = connection.prepareStatement(update_stmt); ps.setString(1, checkpoint + ".00000"); ps.setString(2, str_store_entry); ps.executeUpdate(); ps.close(); } rs.close(); statement.close(); connection.close(); } } catch (Exception ex) { logger.log(Level.SEVERE, "Exception initializing context Datasource " + ex); return; } }
From source file:com.norconex.collector.core.data.store.impl.jdbc.JDBCCrawlDataStore.java
@Override public Iterator<ICrawlData> getCacheIterator() { try {/*from ww w . java 2s . c om*/ final Connection conn = datasource.getConnection(); final Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); final ResultSet rs = stmt.executeQuery(serializer.getSelectCrawlDataSQL(TABLE_CACHE)); if (rs == null || !rs.first()) { return null; } rs.beforeFirst(); return new CrawlDataIterator(TABLE_CACHE, rs, conn, stmt); } catch (SQLException e) { throw new CrawlDataStoreException("Problem getting database cache iterator.", e); } }
From source file:org.geowebcache.storage.jdbc.metastore.JDBCMBIdCache.java
/** * Ask the database for next auto increment * // ww w . ja va 2s. c om * @throws SQLException */ private Long doInsert(String table, String key) throws SQLException { Long res = null; final Connection connection = wrpr.getConnection(); PreparedStatement prep = null; ResultSet rs = null; try { String query = "INSERT INTO " + table + " (value) VALUES (?)"; prep = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS); prep.setString(1, key); prep.executeUpdate(); rs = prep.getGeneratedKeys(); rs.first(); res = Long.valueOf(rs.getLong(1)); } catch (SQLException se) { log.error(se.getMessage()); } finally { close(rs); close(prep); close(connection); } return res; }
From source file:org.geowebcache.storage.jdbc.metastore.JDBCMBIdCache.java
/** See whether the database knows anything */ private Long doSelect(String table, String key) throws SQLException { PreparedStatement prep = null; ResultSet rs = null; final Connection connection = wrpr.getConnection(); try {// w ww . j a v a 2 s . co m String query = "SELECT ID FROM " + table + " WHERE VALUE LIKE ? LIMIT 1"; prep = connection.prepareStatement(query); prep.setString(1, key); rs = prep.executeQuery(); if (rs.first()) { return Long.valueOf(rs.getLong(1)); } } catch (SQLException se) { log.error(se.getMessage()); } finally { close(rs); close(prep); close(connection); } return null; }
From source file:com.napkindrawing.dbversion.task.DbVersionCommand.java
public boolean canQueryRevisionTable() { Connection conn = getConnection(); Statement stmt = null;//from w w w. j a v a 2 s . c o m try { stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery( "SELECT count(*), '" + Thread.currentThread() + "' AS thread FROM __database_revision"); if (rs.first()) { return true; } } catch (SQLException e) { if (e.getMessage().matches("^Table.*doesn't exist.*$")) { // MySQL } else { throw new RuntimeException("Error querying database", e); } } finally { if (stmt != null) { try { stmt.close(); } catch (SQLException ignore) { } } } return false; }
From source file:siddur.solidtrust.bi.data.DataPump.java
private int pumpEach(WebSource webSource) { int maxId = maxId(webSource); ScrapeLog log = new ScrapeLog(); log.setTablename(webSource.getTablename()); log.setStartAt(new Date()); log.setStartId(maxId);//from w ww . j a v a 2s . co m persister.save(log); int total = 0; Connection conn = null; try { conn = getConnection(webSource.getDataSource()); log4j.info("[" + webSource.getTablename() + "]Last sync: " + maxId); String baseSql = buildBaseSql(webSource); log4j.info(baseSql); while (true) { Statement stmt = conn.createStatement(); ResultSet result = stmt.executeQuery(buildSql(baseSql, maxId, 1000)); if (result.last()) { maxId = result.getInt("ID"); result.first(); log4j.info("Fetch from " + result.getInt("ID")); result.beforeFirst(); } else { break; } List<CarInfo> list = getCars(result, webSource.getTablename()); stmt.close(); if (!list.isEmpty()) { total += list.size(); persister.saveBatch(list); } } log4j.info("[" + webSource.getTablename() + "]Finish sync: " + total); //the last past 24 hours List<CarInfo> list = getDateRemoved(webSource, conn); if (list != null) { int s = list.size(); int r = s % 1000; int x = (s - r) / 1000; for (int i = 0; i < x; i++) { persister.saveBatch(list.subList(i * 1000, (i + 1) * 1000)); log4j.info("Update removed records:" + 1000); } if (x * 1000 != list.size()) { persister.saveBatch(list.subList(x * 1000, s)); log4j.info("Update removed records:" + (s - x * 1000)); } log4j.info("[" + webSource.getTablename() + "]Updated removed records: " + s); } } catch (Exception e1) { log.setError(e1.getMessage()); log4j.warn(e1.getMessage(), e1); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { log4j.warn(e.getMessage(), e); } } } log.setEndAt(new Date()); log.setEndId(maxId); log.setAmount(total); persister.merge(log); try { sort(); } catch (Exception e) { log4j.warn(e.getMessage(), e); } return total; }
From source file:com.drevelopment.couponcodes.bukkit.coupon.BukkitCouponHandler.java
@Override public Coupon getBasicCoupon(String coupon) { try {//from www . j av a 2 s .c o m ResultSet rs = databaseHandler.query("SELECT * FROM couponcodes WHERE name='" + coupon + "'"); if (rs == null) return null; if (databaseHandler.getDatabaseOptions() instanceof MySQLOptions) rs.first(); int usetimes = rs.getInt("usetimes"); int time = rs.getInt("timeuse"); String type = rs.getString("ctype"); if (type.equalsIgnoreCase("Item")) return createNewItemCoupon(coupon, usetimes, time, null, null); else if (type.equalsIgnoreCase("Economy")) return createNewEconomyCoupon(coupon, usetimes, time, null, 0); else if (type.equalsIgnoreCase("Rank")) return createNewRankCoupon(coupon, null, usetimes, time, null); else if (type.equalsIgnoreCase("Xp")) return this.createNewXpCoupon(coupon, 0, usetimes, time, null); else if (type.equalsIgnoreCase("command")) return this.createNewCommandCoupon(coupon, null, usetimes, time, null); else return null; } catch (SQLException e) { e.printStackTrace(); return null; } }
From source file:com.tengel.time.MobControl.java
public boolean removeMonsterSpawn(Location loc, String type) { Connection con = plugin.getSql().getConnection(); Statement st;// w w w .ja v a2 s . c o m try { st = con.createStatement(); ResultSet rs = st.executeQuery("SELECT id FROM `spawns` WHERE type='" + type + "' AND x=" + loc.getBlockX() + " AND y=" + loc.getBlockY() + " AND z=" + loc.getBlockZ() + ";"); if (rs.first()) { st.executeUpdate("DELETE FROM `spawns` WHERE id=" + rs.getInt("id") + ";"); return true; } } catch (Exception ex) { plugin.sendConsole("Failed to remove spawn for '" + type + "' in MobControl class, " + ex); } return false; }
From source file:com.redoute.datamap.dao.impl.PictureDAO.java
@Override public Picture findPictureByKey(String id) { Picture result = null;//from w ww .ja v a 2 s .c om final String query = "SELECT * FROM picture WHERE id = ?"; Connection connection = this.databaseSpring.connect(); try { PreparedStatement preStat = connection.prepareStatement(query); try { preStat.setString(1, id); ResultSet resultSet = preStat.executeQuery(); try { if (resultSet.first()) { result = loadPictureFromResultSet(resultSet); } } catch (SQLException exception) { Logger.log(PictureDAO.class.getName(), Level.ERROR, exception.toString()); } finally { resultSet.close(); } } catch (SQLException exception) { Logger.log(PictureDAO.class.getName(), Level.ERROR, exception.toString()); } finally { preStat.close(); } } catch (SQLException exception) { Logger.log(PictureDAO.class.getName(), Level.ERROR, exception.toString()); } finally { try { if (connection != null) { connection.close(); } } catch (SQLException e) { Logger.log(PictureDAO.class.getName(), Level.WARN, e.toString()); } } return result; }