List of usage examples for java.sql ResultSet getLong
long getLong(String columnLabel) throws SQLException;
ResultSet
object as a long
in the Java programming language. From source file:net.sourceforge.msscodefactory.cfasterisk.v2_0.CFAstXMsgClient.CFAstXMsgClientSchema.java
public static Long getNullableUInt32(ResultSet reader, int colidx) { try {/*w w w.j av a2 s . co m*/ long val = reader.getLong(colidx); if (reader.wasNull()) { return (null); } else { return (new Long(val)); } } catch (SQLException e) { throw CFLib.getDefaultExceptionFactory().newDbException(CFAstXMsgClientSchema.class, "getNullableUInt32", e); } }
From source file:com.mtgi.analytics.sql.BehaviorTrackingDataSourceTest.java
@Test public void testPreparedStatement() throws Exception { //test tracking through the prepared statement API, which should also //log parameters in the events. PreparedStatement stmt = conn.prepareStatement("insert into TEST_TRACKING values (?, ?, ?)"); stmt.setLong(1, 1);/*w w w . j av a 2 s .com*/ stmt.setString(2, "hello"); stmt.setObject(3, null, Types.VARCHAR); assertEquals(1, stmt.executeUpdate()); //test support for batching. each batch should log 1 event. stmt.setLong(1, 3); stmt.setString(2, "batch"); stmt.setObject(3, "1", Types.VARCHAR); stmt.addBatch(); stmt.setLong(1, 4); stmt.setString(2, "batch"); stmt.setObject(3, "2", Types.VARCHAR); stmt.addBatch(); stmt.executeBatch(); //back to a regular old update. stmt.setLong(1, 2); stmt.setObject(2, "goodbye", Types.VARCHAR); stmt.setNull(3, Types.VARCHAR); assertEquals(1, stmt.executeUpdate()); stmt = conn.prepareStatement("update TEST_TRACKING set DESCRIPTION = 'world'"); assertEquals(4, stmt.executeUpdate()); stmt = conn.prepareStatement("select ID from TEST_TRACKING order by ID"); ResultSet rs = stmt.executeQuery(); int index = 0; long[] keys = { 1L, 2L, 3L, 4L }; while (rs.next()) assertEquals(keys[index++], rs.getLong(1)); rs.close(); assertEquals(4, index); manager.flush(); assertEventDataMatches("BehaviorTrackingDataSourceTest.testPreparedStatement-result.xml"); }
From source file:biopolis.headless.BiopolisSegmentationManagement.java
public void iterationPhase(ResultSet resultSet) throws SQLException, BiopolisGeneralException { java.util.Date date = this.updateTTL(); while (resultSet.next()) { long ll = resultSet.getLong(1); BasicDBObject place = new BasicDBObject(); place.put("biopolishandle", this.biopiolisid); place.put("biopolisdata", ll); place.put("biopolisttl", date); dbcol.insert(place);/*from w w w. j a va 2s . c om*/ } this.occurs++; }
From source file:com.sqewd.open.dal.core.persistence.db.H2DbPersister.java
@Override protected Object getSequenceValue(Entity entity, StructAttributeReflect attr, Connection conn) throws Exception { if (EnumPrimitives.isPrimitiveType(attr.Field.getType())) { EnumPrimitives prim = EnumPrimitives.type(attr.Field.getType()); if (prim == EnumPrimitives.ELong || prim == EnumPrimitives.EInteger) { String seqname = SimpleDbQuery.getSequenceName(entity, attr); String sql = "select NEXT VALUE FOR " + seqname; Statement stmnt = conn.createStatement(); try { ResultSet rs = stmnt.executeQuery(sql); while (rs.next()) { return rs.getLong(1); }/*from w ww . j ava2 s. c o m*/ if (rs != null) rs.close(); } finally { if (stmnt != null && !stmnt.isClosed()) stmnt.close(); } } } else if (attr.Field.getType().equals(String.class)) { UUID uuid = UUID.randomUUID(); return uuid.toString(); } throw new Exception("Cannot generate sequence for type [" + attr.Field.getType().getCanonicalName() + "]"); }
From source file:net.mindengine.oculus.frontend.service.customization.JdbcCustomizationDAO.java
@Override public long saveUnitCustomizationValue(UnitCustomizationValue unitCustomizationValue) throws Exception { UnitCustomizationValue value = null; if (unitCustomizationValue.getId() == null || unitCustomizationValue.getId() < 1) { value = getUnitCustomizationValue(unitCustomizationValue.getCustomizationId(), unitCustomizationValue.getUnitId()); } else//from w w w . ja va 2 s.co m value = unitCustomizationValue; if (value == null) { PreparedStatement ps = getConnection().prepareStatement( "insert into unit_customization_values (unit_id, customization_id, value) values (?,?,?)"); ps.setLong(1, unitCustomizationValue.getUnitId()); ps.setLong(2, unitCustomizationValue.getCustomizationId()); ps.setString(3, unitCustomizationValue.getValue()); logger.info(ps); ps.execute(); ResultSet rs = ps.getGeneratedKeys(); if (rs.next()) { return rs.getLong(1); } } else { update("update unit_customization_values set value = :value where id = :id", "value", unitCustomizationValue.getValue(), "id", value.getId()); return value.getId(); } return 0; }
From source file:dk.netarkivet.archive.arcrepositoryadmin.ReplicaCacheHelpers.java
/** * Method to create a new entry in the file table in the database. * The file_id is automatically created by the database, and the argument * is used for the filename for this new entry to the table. * This will also create a replicafileinfo entry for each replica. * * @param filename The filename for the new entry in the file table. * @param connection An open connection to the archive database * @throws IllegalState If the file cannot be inserted into the database. * @return created file_id for the new entry. *///from w ww . ja v a2 s . com protected static long insertFileIntoDB(String filename, Connection connection) throws IllegalState { log.debug("Insert file '" + filename + "' into database"); PreparedStatement statement = null; try { // Make the SQL statement for putting the replica into the database // and insert the variables for the entry to the replica table. statement = connection.prepareStatement("INSERT INTO file (filename) " + "VALUES ( ? )", Statement.RETURN_GENERATED_KEYS); statement.setString(1, filename); // execute the SQL statement statement.executeUpdate(); // Retrieve the fileId for the just inserted file. ResultSet resultset = statement.getGeneratedKeys(); resultset.next(); long fileId = resultset.getLong(1); connection.commit(); // Create replicafileinfo for each replica. createReplicaFileInfoEntriesInDB(fileId, connection); log.debug("Insert file '" + filename + "' into database completed. Assigned fileID=" + fileId); return fileId; } catch (SQLException e) { throw new IllegalState("Cannot add file '" + filename + "' to the database.", e); } finally { DBUtils.closeStatementIfOpen(statement); } }
From source file:javasnack.flywaydb.FlywaydbDemo1Test.java
@Test public void typicalMigrationFlow() throws Exception { Properties properties = new Properties(); properties.setProperty("flyway.user", "sa"); properties.setProperty("flyway.password", ""); properties.setProperty("flyway.url", "jdbc:h2:mem:flywaydb_demo1;DB_CLOSE_DELAY=-1"); properties.setProperty("flyway.driver", "org.h2.Driver"); final Flyway flyway = new Flyway(); flyway.configure(properties);//from www. j a va 2 s . c om assertNotNull(flyway.getDataSource()); flyway.setLocations("flywaydbdemos/demo1"); // not initialized -> no target schemas. assertEquals(flyway.getSchemas().length, 0); flyway.init(); assertEquals(flyway.getSchemas().length, 1); assertEquals(flyway.getSchemas()[0], "PUBLIC"); assertEquals(flyway.getTable(), "schema_version"); MigrationInfoService mis = flyway.info(); assertEquals(mis.all().length, 4); for (MigrationInfo mi : mis.all()) { System.out.println(mi.getVersion()); System.out.println(mi.getDescription()); System.out.println(mi.getState()); System.out.println(mi.getType()); } // 3 migrations (V1.1, V1.2, V1.3) must be pending status. assertEquals(mis.pending().length, 3); // initialized version (V1) applied. assertEquals(mis.applied().length, 1); // current version is initialized version (V1). MigrationInfo mi = mis.current(); assertEquals(mi.getVersion().getVersion(), "1"); assertEquals(mi.getDescription(), flyway.getInitDescription()); assertEquals(mi.getState(), MigrationState.SUCCESS); // migrate to V1.2 flyway.setTarget(MigrationVersion.fromVersion("1.2")); flyway.migrate(); mis = flyway.info(); assertEquals(mis.all().length, 4); // no pending, V1.3 -> "ABOVE_TARGET". assertEquals(mis.pending().length, 0); // V1, V1.1, V1.2 were applied. assertEquals(mis.applied().length, 3); for (MigrationInfo _mi : mis.all()) { System.out.println(_mi.getVersion()); System.out.println(_mi.getDescription()); System.out.println(_mi.getState()); System.out.println(_mi.getType()); } mi = mis.current(); assertEquals(mi.getVersion().getVersion(), "1.2"); assertEquals(mi.getDescription(), "add t1 hobby column"); assertEquals(mi.getState(), MigrationState.SUCCESS); // migrate to latest version flyway.setTarget(MigrationVersion.LATEST); flyway.migrate(); mis = flyway.info(); assertEquals(mis.all().length, 4); assertEquals(mis.pending().length, 0); assertEquals(mis.applied().length, 4); mi = mis.current(); assertEquals(mi.getVersion().getVersion(), "1.3"); assertEquals(mi.getDescription(), "insert t1 data2"); assertEquals(mi.getState(), MigrationState.SUCCESS); // select and validate records. QueryRunner run = new QueryRunner(); ResultSetHandler<Map<Long, T1>> h = new AbstractKeyedHandler<Long, T1>() { @Override protected T1 createRow(ResultSet rs) throws SQLException { T1 row = new T1(); row.id = rs.getLong("id"); row.name = rs.getString("name"); row.age = rs.getInt("age"); row.hobby = rs.getString("hobby"); return row; } @Override protected Long createKey(ResultSet rs) throws SQLException { return rs.getLong("id"); } }; Map<Long, T1> found = run.query(conn, "select id, name, age, hobby from t1", h); assertEquals(found.size(), 4); T1 jon = found.get(1L); assertEquals(jon.name, "jon"); assertEquals(jon.hobby, ""); T1 alice = found.get(3L); assertEquals(alice.name, "alice"); assertEquals(alice.hobby, "swimming"); }
From source file:javasnack.flywaydb.FlywaydbDemo3Test.java
@Test public void usingPlaceholderAndEncoding() throws Exception { Properties properties = new Properties(); properties.setProperty("flyway.user", "sa"); properties.setProperty("flyway.password", ""); properties.setProperty("flyway.url", "jdbc:h2:mem:flywaydb_demo3;DB_CLOSE_DELAY=-1"); properties.setProperty("flyway.driver", "org.h2.Driver"); final Flyway flyway = new Flyway(); flyway.configure(properties);/*ww w . ja va 2 s. c om*/ flyway.setLocations("flywaydbdemos/demo3"); flyway.setEncoding("UTF-8"); Map<String, String> placeholders = new HashMap<>(); placeholders.put("L1", "abc"); placeholders.put("L2", "def"); placeholders.put("L3", "abc"); placeholders.put("L4", "def"); flyway.setPlaceholders(placeholders); MigrationInfoService mis = flyway.info(); assertEquals(mis.all().length, 3); for (MigrationInfo mi : mis.all()) { System.out.println(mi.getVersion()); System.out.println(mi.getDescription()); System.out.println(mi.getState()); System.out.println(mi.getType()); } // 3 migrations (V1, V1.1, V1.2) must be pending status. assertEquals(mis.pending().length, 3); // no version applied. assertEquals(mis.applied().length, 0); // no current version. MigrationInfo mi = mis.current(); assertNull(mi); // migrate to V1.1 flyway.setTarget(MigrationVersion.fromVersion("1.1")); flyway.migrate(); mis = flyway.info(); assertEquals(mis.all().length, 3); // no pending, V1.2 -> "ABOVE_TARGET". assertEquals(mis.pending().length, 0); // V1, V1.1, V1.2 were applied. assertEquals(mis.applied().length, 2); for (MigrationInfo _mi : mis.all()) { System.out.println(_mi.getVersion()); System.out.println(_mi.getDescription()); System.out.println(_mi.getState()); System.out.println(_mi.getType()); } mi = mis.current(); assertEquals(mi.getVersion().getVersion(), "1.1"); assertEquals(mi.getDescription(), "add t1 hobby column"); assertEquals(mi.getState(), MigrationState.SUCCESS); // change placeholder prefix and suffix. flyway.setPlaceholderPrefix("%{%"); flyway.setPlaceholderSuffix("%}%"); // migrate to latest version flyway.setTarget(MigrationVersion.LATEST); flyway.migrate(); mis = flyway.info(); assertEquals(mis.all().length, 3); assertEquals(mis.pending().length, 0); assertEquals(mis.applied().length, 3); mi = mis.current(); assertEquals(mi.getVersion().getVersion(), "1.2"); assertEquals(mi.getDescription(), "insert t1 data2"); assertEquals(mi.getState(), MigrationState.SUCCESS); // select and validate records. QueryRunner run = new QueryRunner(); ResultSetHandler<Map<Long, T1>> h = new AbstractKeyedHandler<Long, T1>() { @Override protected T1 createRow(ResultSet rs) throws SQLException { T1 row = new T1(); row.id = rs.getLong("id"); row.name = rs.getString("name"); row.age = rs.getInt("age"); row.hobby = rs.getString("hobby"); return row; } @Override protected Long createKey(ResultSet rs) throws SQLException { return rs.getLong("id"); } }; Map<Long, T1> found = run.query(conn, "select id, name, age, hobby from abc_def_t1", h); assertEquals(found.size(), 4); T1 jon = found.get(1L); assertEquals(jon.name, ""); assertEquals(jon.hobby, ""); T1 alice = found.get(3L); assertEquals(alice.name, "alice"); assertEquals(alice.hobby, "swimming"); }
From source file:com.azaptree.services.domain.entity.dao.VersionedEntityRowMapperSupport.java
@Override public T mapRow(final ResultSet rs, final int rowNum) throws SQLException { final T entity = createEntity(rs, rowNum); final DomainVersionedEntity domainEntity = (DomainVersionedEntity) entity; domainEntity.setEntityId((UUID) rs.getObject("entity_id")); domainEntity.setEntityVersion(rs.getLong("entity_version")); domainEntity.setEntityCreatedOn(rs.getTimestamp("entity_created_on").getTime()); domainEntity.setCreatedBy((UUID) rs.getObject("entity_created_by")); domainEntity.setEntityUpdatedOn(rs.getTimestamp("entity_updated_on").getTime()); domainEntity.setUpdatedBy((UUID) rs.getObject("entity_updated_by")); return mapRow(entity, rs, rowNum); }
From source file:com.twitter.hdfsdu.TreeSizeByPathServlet.java
@Override public Iterable<String> getLines(HttpServletRequest request) { String paramPath = request.getParameter("path"); if (paramPath == null) { paramPath = "/"; }// w w w .j a va2 s . c o m List<String> lines = Lists.newLinkedList(); List<NodeData> elems = Lists.newArrayList(); Integer paramDepth = request.getParameter("depth") == null ? 2 : Integer.parseInt(request.getParameter("depth")); try { ResultSet resultSet = getSizeByPath(request); NodeData data; while (resultSet.next()) { data = new NodeData(); data.fileSize = resultSet.getString("size_in_bytes"); data.nChildren = resultSet.getLong("file_count"); data.path = resultSet.getString("path"); data.leaf = resultSet.getBoolean("leaf"); elems.add(data); } JSONObject jsonObject = DataTransformer.getJSONTree(paramPath, paramDepth, elems); String ans = null; if (jsonObject != null) { ans = jsonObject.toJSONString(); } if (ans == null) { lines.add("{ \"children\": [] }"); } else { lines.add(ans); } } catch (SQLException e) { e.printStackTrace(); } return lines; }