Java Code Examples for java.sql.ResultSet.getTimestamp()

The following are Jave code examples for showing how to use getTimestamp() of the java.sql.ResultSet class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: tangyuan2   File: DateTypeHandler.java   View Source Code Vote up 6 votes
@Override
public Date getNullableResult(ResultSet rs, String columnName) throws SQLException {
	Timestamp sqlTimestamp = rs.getTimestamp(columnName);
	if (sqlTimestamp != null) {
		return new Date(sqlTimestamp.getTime());
	}
	return null;
}
 
Example 2
Project: HTAPBench   File: TPCCUtil.java   View Source Code Vote up 6 votes
/**
    * Creates a Customer object from the current row in the given ResultSet.
    * The caller is responsible for closing the ResultSet.
    * @param rs an open ResultSet positioned to the desired row
    * @return the newly created Customer object
    * @throws SQLException for problems getting data from row
    */
public static Customer newCustomerFromResults(ResultSet rs)
		throws SQLException {
	Customer c = new Customer();
	// TODO: Use column indices: probably faster?
	c.c_first = rs.getString("c_first");
	c.c_middle = rs.getString("c_middle");
	c.c_street_1 = rs.getString("c_street_1");
	c.c_street_2 = rs.getString("c_street_2");
	c.c_city = rs.getString("c_city");
	c.c_state = rs.getString("c_state");
	c.c_zip = rs.getString("c_zip");
	c.c_phone = rs.getString("c_phone");
	c.c_credit = rs.getString("c_credit");
	c.c_credit_lim = rs.getFloat("c_credit_lim");
	c.c_discount = rs.getFloat("c_discount");
	c.c_balance = rs.getFloat("c_balance");
	c.c_ytd_payment = rs.getFloat("c_ytd_payment");
	c.c_payment_cnt = rs.getInt("c_payment_cnt");
	c.c_since = rs.getTimestamp("c_since");
	return c;
}
 
Example 3
Project: the-vigilantes   File: StatementRegressionTest.java   View Source Code Vote up 6 votes
/**
 * Tests fix for BUG#54095 - Unnecessary call in newSetTimestampInternal.
 *
 * This bug was fixed as a consequence of the patch for Bug#71084.
 *
 * @throws Exception
 *             if the test fails.
 */
public void testBug54095() throws Exception {
    Connection testConn = getConnectionWithProps("useLegacyDatetimeCode=false");

    Calendar testCal = Calendar.getInstance();
    java.util.Date origDate = testCal.getTime();

    PreparedStatement testPstmt = testConn.prepareStatement("SELECT ?");
    testPstmt.setTimestamp(1, new Timestamp(0), testCal);
    assertEquals("Calendar object shouldn't have changed after PreparedStatement.setTimestamp().", origDate, testCal.getTime());

    ResultSet testRs = testPstmt.executeQuery();
    testRs.next();
    assertEquals("Calendar object shouldn't have changed after PreparedStatement.executeQuery().", origDate, testCal.getTime());

    testRs.getTimestamp(1, testCal);
    assertEquals("Calendar object shouldn't have changed after ResultSet.getTimestamp().", origDate, testCal.getTime());

    testRs.close();
    testPstmt.close();
    testConn.close();
}
 
Example 4
Project: graphium   File: SubscriptionRowMapper.java   View Source Code Vote up 5 votes
@Override
public ISubscription mapRow(ResultSet rs, int rowNum) throws SQLException {
	Integer subscriptionGroupId = rs.getInt("group_id");
	String subscriptionGroupName = rs.getString("group_name");
	Long subscriptionGroupGraphId = rs.getLong("group_graph_id");
	String graphName = rs.getString("graph_name");
	ISubscriptionGroup subscriptionGroup = null;
	if (subscriptionGroupId != null && 
		subscriptionGroupName != null && 
		subscriptionGroupGraphId != null) {
		IWayGraph graph = new WayGraph(subscriptionGroupGraphId, graphName);
		subscriptionGroup = new SubscriptionGroup(subscriptionGroupId, subscriptionGroupName, graph, new ArrayList<>());
	}
	
	ISubscription subscription = new Subscription(
			rs.getString("servername"), 
			rs.getString("viewname"), 
			subscriptionGroup,
			rs.getString("url"),
			rs.getString("user"),
			rs.getString("password"),
			(Date) rs.getTimestamp("timestamp"));
	
	if (subscriptionGroup != null) {
		subscription.getSubscriptionGroup().getSubscriptions().add(subscription);
	}
	
	return subscription;
}
 
Example 5
Project: Java-Air-Reservation   File: FlightLeg.java   View Source Code Vote up 5 votes
/**
 * Constructor to auto load flight leg from db
 * @param leg_no : primary key of Flight leg
 */
public FlightLeg(int leg_no)
{
  this.conn = DBConnect.connect(); 
  PreparedStatement pst = null;
    try {
        String sql = "SELECT * FROM `flight_leg` WHERE `leg_no` = ?";
        pst=conn.prepareStatement(sql);
        pst.setInt(1,leg_no);
        ResultSet rs;
        rs = pst.executeQuery();
        if (!rs.isBeforeFirst() ) {     
            return;
        } 
        while (rs.next()) {
            this.leg_no = rs.getInt("leg_no");
            this.leg_type = rs.getString("leg_type");
            this.from_aID = rs.getString("from_aID");
            this.to_aID = rs.getString("to_aID");
            this.departure_time = rs.getTimestamp("departure_time");
            this.arival_time = rs.getTimestamp("arival_time");
            this.flight_no = rs.getString("flight_no");
            this.exist = true;
        }
    } catch (SQLException e) {
        System.out.println("Error : while excicuting prepared statement");
        System.out.println(e);
    }
}
 
Example 6
Project: ProyectoPacientes   File: StressRegressionTest.java   View Source Code Vote up 5 votes
/**
 * @param threadConn
 * @param threadStmt
 * @param threadNumber
 */
void contentiousWork(Connection threadConn, Statement threadStmt, int threadNumber) {
    Date now = new Date();

    try {
        for (int i = 0; i < 1000; i++) {
            ResultSet threadRs = threadStmt.executeQuery("SELECT 1, 2");

            while (threadRs.next()) {
                threadRs.getString(1);
                threadRs.getString(2);
            }

            threadRs.close();

            PreparedStatement pStmt = threadConn.prepareStatement("SELECT ?");
            pStmt.setTimestamp(1, new Timestamp(now.getTime()));

            threadRs = pStmt.executeQuery();

            while (threadRs.next()) {
                threadRs.getTimestamp(1);
            }

            threadRs.close();
            pStmt.close();
        }
    } catch (Exception ex) {
        throw new RuntimeException(ex.toString());
    }
}
 
Example 7
Project: Pxls   File: DBPixelPlacement.java   View Source Code Vote up 5 votes
@Override
public DBPixelPlacement map(int index, ResultSet r, StatementContext ctx) throws SQLException {
    Timestamp time = r.getTimestamp("time");
    Timestamp ban_expiry = r.getTimestamp("users.ban_expiry");
    boolean banned = Role.valueOf(r.getString("users.role")) == Role.BANNED;
    if (!banned && ban_expiry != null) {
        banned = ban_expiry.getTime() > System.currentTimeMillis();
    }

    return new DBPixelPlacement(
            r.getInt("id"),
            r.getInt("x"),
            r.getInt("y"),
            r.getInt("color"),
            r.getInt("secondary_id"),
            time == null ? 0 : time.getTime(),
            r.getInt("users.id"),
            r.getString("users.username"),
            r.getString("users.login"),
            Role.valueOf(r.getString("users.role")), // TODO: all users might not have valid roles
            ban_expiry == null ? 0 : ban_expiry.getTime(),
            r.getInt("pixel_count"),
            r.getInt("pixel_count_alltime"),
            r.getString("users.ban_reason"),
            banned,
            r.getBoolean("undo_action"),
            r.getString("user_agent")
    );
}
 
Example 8
Project: scorekeeperfrontend   File: Payment.java   View Source Code Vote up 5 votes
public Payment(ResultSet rs) throws SQLException
{
    payid    = (UUID)rs.getObject("payid");
    eventid  = (UUID)rs.getObject("eventid");
    carid    = (UUID)rs.getObject("carid");
    refid    = rs.getString("refid");
    txtype   = rs.getString("txtype");
    txid     = rs.getString("txid");
    txtime   = rs.getTimestamp("txtime", Database.utc);
    itemname = rs.getString("itemname");
    amount   = rs.getDouble("amount");
}
 
Example 9
Project: airsonic   File: MediaFileDao.java   View Source Code Vote up 5 votes
public MediaFile mapRow(ResultSet rs, int rowNum) throws SQLException {
    return new MediaFile(
            rs.getInt(1),
            rs.getString(2),
            rs.getString(3),
            MediaFile.MediaType.valueOf(rs.getString(4)),
            rs.getString(5),
            rs.getString(6),
            rs.getString(7),
            rs.getString(8),
            rs.getString(9),
            rs.getInt(10) == 0 ? null : rs.getInt(10),
            rs.getInt(11) == 0 ? null : rs.getInt(11),
            rs.getInt(12) == 0 ? null : rs.getInt(12),
            rs.getString(13),
            rs.getInt(14) == 0 ? null : rs.getInt(14),
            rs.getBoolean(15),
            rs.getInt(16) == 0 ? null : rs.getInt(16),
            rs.getLong(17) == 0 ? null : rs.getLong(17),
            rs.getInt(18) == 0 ? null : rs.getInt(18),
            rs.getInt(19) == 0 ? null : rs.getInt(19),
            rs.getString(20),
            rs.getString(21),
            rs.getInt(22),
            rs.getTimestamp(23),
            rs.getString(24),
            rs.getTimestamp(25),
            rs.getTimestamp(26),
            rs.getTimestamp(27),
            rs.getTimestamp(28),
            rs.getBoolean(29),
            rs.getInt(30));
}
 
Example 10
Project: HueSense   File: PresenceSensor.java   View Source Code Vote up 5 votes
@Override
public Set<SensorValue<Boolean>> getValuesInRange(Date start, Date end) {
    NavigableSet<SensorValue<Boolean>> ret = new TreeSet<>();

    try (Connection conn = dbMan.getConnection()) {
        try (PreparedStatement stmt = conn.prepareStatement(SELECT_RANGE)) {
            stmt.setLong(1, dbId);
            stmt.setTimestamp(2, new Timestamp(start.getTime()));
            long endTime = end == null ? System.currentTimeMillis() : end.getTime();
            stmt.setTimestamp(3, new Timestamp(endTime));
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                SensorValue<Boolean> val = new SensorValue<>(rs.getTimestamp("CREATED"), rs.getBoolean("PRESENCE"));
                ret.add(val);
            }
        }
    } catch (SQLException ex) {
        LOG.error("Error querying light values", ex);
    }
    return ret;
}
 
Example 11
Project: morpheus-core   File: SQLExtractor.java   View Source Code Vote up 4 votes
@Override
@SuppressWarnings("unchecked")
public <V> V getValue(ResultSet rs, int colIndex) throws SQLException {
    return (V)rs.getTimestamp(colIndex);
}
 
Example 12
Project: airsonic   File: ShareDao.java   View Source Code Vote up 4 votes
public Share mapRow(ResultSet rs, int rowNum) throws SQLException {
    return new Share(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getTimestamp(5),
            rs.getTimestamp(6), rs.getTimestamp(7), rs.getInt(8));
}
 
Example 13
Project: real-time-risk   File: PositionDAOImpl.java   View Source Code Vote up 4 votes
public Position mapRow(ResultSet rs, int rowNum) throws SQLException {
    Position.Builder builder = Position.newBuilder();

    if (rs.getString("inst_id") != null)
        builder.setInstId(rs.getString("inst_id"));

    if (rs.getString("portf_id") != null)
        builder.setPortfId(rs.getString("portf_id"));

    builder.setTotalQty(rs.getDouble("total_qty"));

    builder.setTotalBuyQty(rs.getDouble("total_buy_qty"));

    builder.setTotalSellQty(rs.getDouble("total_sell_qty"));

    builder.setAvgSellPrice(rs.getDouble("avg_buy_price"));

    builder.setAvgSellPrice(rs.getDouble("avg_sell_price"));

    if (rs.getTimestamp("timestamp") != null)
        builder.setTimestamp(rs.getTimestamp("timestamp").getTime());

    return builder.build();
}
 
Example 14
Project: calcite-avatica   File: JdbcResultSet.java   View Source Code Vote up 4 votes
private static Object getValue(ResultSet resultSet, int type, int j,
    Calendar calendar) throws SQLException {
  switch (type) {
  case Types.BIGINT:
    final long aLong = resultSet.getLong(j + 1);
    return aLong == 0 && resultSet.wasNull() ? null : aLong;
  case Types.INTEGER:
    final int anInt = resultSet.getInt(j + 1);
    return anInt == 0 && resultSet.wasNull() ? null : anInt;
  case Types.SMALLINT:
    final short aShort = resultSet.getShort(j + 1);
    return aShort == 0 && resultSet.wasNull() ? null : aShort;
  case Types.TINYINT:
    final byte aByte = resultSet.getByte(j + 1);
    return aByte == 0 && resultSet.wasNull() ? null : aByte;
  case Types.DOUBLE:
  case Types.FLOAT:
    final double aDouble = resultSet.getDouble(j + 1);
    return aDouble == 0D && resultSet.wasNull() ? null : aDouble;
  case Types.REAL:
    final float aFloat = resultSet.getFloat(j + 1);
    return aFloat == 0D && resultSet.wasNull() ? null : aFloat;
  case Types.DATE:
    final Date aDate = resultSet.getDate(j + 1, calendar);
    return aDate == null
        ? null
        : (int) (aDate.getTime() / DateTimeUtils.MILLIS_PER_DAY);
  case Types.TIME:
    final Time aTime = resultSet.getTime(j + 1, calendar);
    return aTime == null
        ? null
        : (int) (aTime.getTime() % DateTimeUtils.MILLIS_PER_DAY);
  case Types.TIMESTAMP:
    final Timestamp aTimestamp = resultSet.getTimestamp(j + 1, calendar);
    return aTimestamp == null ? null : aTimestamp.getTime();
  case Types.ARRAY:
    final Array array = resultSet.getArray(j + 1);
    if (null == array) {
      return null;
    }
    try {
      // Recursively extracts an Array using its ResultSet-representation
      return extractUsingResultSet(array, calendar);
    } catch (UnsupportedOperationException | SQLFeatureNotSupportedException e) {
      // Not every database might implement Array.getResultSet(). This call
      // assumes a non-nested array (depends on the db if that's a valid assumption)
      return extractUsingArray(array, calendar);
    }
  case Types.STRUCT:
    Struct struct = resultSet.getObject(j + 1, Struct.class);
    Object[] attrs = struct.getAttributes();
    List<Object> list = new ArrayList<>(attrs.length);
    for (Object o : attrs) {
      list.add(o);
    }
    return list;
  default:
    return resultSet.getObject(j + 1);
  }
}
 
Example 15
Project: calcite-avatica   File: ColumnMetaData.java   View Source Code Vote up 4 votes
/** Returns the value of a column of this type from a result set. */
public Object jdbcGet(ResultSet resultSet, int i) throws SQLException {
  switch (this) {
  case PRIMITIVE_BOOLEAN:
    return resultSet.getBoolean(i);
  case PRIMITIVE_BYTE:
    return resultSet.getByte(i);
  case PRIMITIVE_SHORT:
    return resultSet.getShort(i);
  case PRIMITIVE_INT:
    return resultSet.getInt(i);
  case PRIMITIVE_LONG:
    return resultSet.getLong(i);
  case PRIMITIVE_FLOAT:
    return resultSet.getFloat(i);
  case PRIMITIVE_DOUBLE:
    return resultSet.getDouble(i);
  case BOOLEAN:
    final boolean aBoolean = resultSet.getBoolean(i);
    return resultSet.wasNull() ? null : aBoolean;
  case BYTE:
    final byte aByte = resultSet.getByte(i);
    return resultSet.wasNull() ? null : aByte;
  case SHORT:
    final short aShort = resultSet.getShort(i);
    return resultSet.wasNull() ? null : aShort;
  case INTEGER:
    final int anInt = resultSet.getInt(i);
    return resultSet.wasNull() ? null : anInt;
  case LONG:
    final long aLong = resultSet.getLong(i);
    return resultSet.wasNull() ? null : aLong;
  case FLOAT:
    final float aFloat = resultSet.getFloat(i);
    return resultSet.wasNull() ? null : aFloat;
  case DOUBLE:
    final double aDouble = resultSet.getDouble(i);
    return resultSet.wasNull() ? null : aDouble;
  case JAVA_SQL_DATE:
    return resultSet.getDate(i);
  case JAVA_SQL_TIME:
    return resultSet.getTime(i);
  case JAVA_SQL_TIMESTAMP:
    return resultSet.getTimestamp(i);
  case ARRAY:
    return resultSet.getArray(i);
  case STRUCT:
    return resultSet.getObject(i, Struct.class);
  default:
    return resultSet.getObject(i);
  }
}
 
Example 16
Project: ralasafe   File: ResultSetReader.java   View Source Code Vote up 4 votes
public Object reader(ResultSet rs, int columnIndex) throws SQLException {
	return rs.getTimestamp(columnIndex);
}
 
Example 17
Project: ralasafe   File: ResultSetReader.java   View Source Code Vote up 4 votes
public Object reader(ResultSet rs, String columnName)
		throws SQLException {
	return rs.getTimestamp(columnName);
}
 
Example 18
Project: airsonic   File: AvatarDao.java   View Source Code Vote up 4 votes
public Avatar mapRow(ResultSet rs, int rowNum) throws SQLException {
    return new Avatar(rs.getInt(1), rs.getString(2), rs.getTimestamp(3), rs.getString(4),
                      rs.getInt(5), rs.getInt(6), rs.getBytes(7));
}
 
Example 19
Project: real-time-risk   File: MarketPriceDAOImpl.java   View Source Code Vote up 3 votes
public MarketPrice mapRow(ResultSet rs, int rowNum) throws SQLException {
    MarketPrice.Builder builder = MarketPrice.newBuilder();

    if (rs.getString("inst_id") != null)
        builder.setInstId(rs.getString("inst_id"));

    builder.setLastPrice(rs.getDouble("last_price"));

    if (rs.getTimestamp("timestamp") != null)
        builder.setTimestamp(rs.getTimestamp("timestamp").getTime());

    return builder.build();
}
 
Example 20
Project: jaffa-framework   File: TypeDefs.java   View Source Code Vote up 3 votes
/** Gets a parameter from the ResultSet.
 * @return the parameter.
 * @param engineType The engine type as defined in init.xml
 * @param rs The ResultSet.
 * @param columnName The name of the parameter.
 * @throws SQLException if a database access error occurs.
 * @throws IOException if any error occurs in reading the data from the database.
 */
public Object getAppObject(ResultSet rs, String columnName, String engineType) throws SQLException, IOException {
    Timestamp timestamp = rs.getTimestamp(columnName);
    if (timestamp != null)
        return new DateTime(timestamp);
    else
        return null;
}