Example usage for java.sql ResultSet getLong

List of usage examples for java.sql ResultSet getLong

Introduction

In this page you can find the example usage for java.sql ResultSet getLong.

Prototype

long getLong(String columnLabel) throws SQLException;

Source Link

Document

Retrieves the value of the designated column in the current row of this ResultSet object as a long in the Java programming language.

Usage

From source file:org.ulyssis.ipp.snapshot.Event.java

public static Optional<Event> loadUnique(Connection connection, Class<? extends Event> eventType)
        throws SQLException, IOException {
    String statement = "SELECT \"id\", \"data\" FROM \"events\" WHERE \"type\" = ? AND \"removed\" = false";
    try (PreparedStatement stmt = connection.prepareStatement(statement)) {
        stmt.setString(1, eventType.getSimpleName());
        ResultSet result = stmt.executeQuery();
        if (result.next()) {
            String evString = result.getString("data");
            Event event = Serialization.getJsonMapper().readValue(evString, Event.class);
            event.id = result.getLong("id");
            event.removed = false;//from ww  w.  j a  va 2s. co m
            return Optional.of(event);
        } else {
            return Optional.empty();
        }
    }
}

From source file:com.keybox.manage.db.AuthDB.java

/**
 * returns user id based on auth token//  ww  w .j  a v a  2 s  . com
 *
 * @param authToken auth token
 * @param con       DB connection
 * @return user
 */
public static User getUserByAuthToken(Connection con, String authToken) {

    User user = null;
    try {
        PreparedStatement stmt = con
                .prepareStatement("select * from users where enabled=true and auth_token like ?");
        stmt.setString(1, authToken);
        ResultSet rs = stmt.executeQuery();
        if (rs.next()) {
            Long userId = rs.getLong("id");

            user = UserDB.getUser(con, userId);
        }
        DBUtils.closeRs(rs);
        DBUtils.closeStmt(stmt);

    } catch (Exception e) {
        log.error(e.toString(), e);
    }

    return user;

}

From source file:com.cloudera.sqoop.manager.CubridManagerExportTest.java

public static void assertRowCount(long expected, String tableName, Connection connection) {
    Statement stmt = null;//from   w  ww . j av a2  s.co m
    ResultSet rs = null;
    try {
        stmt = connection.createStatement();
        rs = stmt.executeQuery("SELECT count(*) FROM " + tableName);
        rs.next();
        assertEquals(expected, rs.getLong(1));
    } catch (SQLException e) {
        LOG.error("Can't verify number of rows", e);
        fail();
    } finally {
        try {
            connection.commit();
            if (stmt != null) {
                stmt.close();
            }
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException ex) {
            LOG.info("Ignored exception in finally block.");
        }
    }
}

From source file:dbcount.DBCountPageView.java

private static boolean verify(final DBMapReduceJobConf jobConf, final long totalPageview) throws SQLException {
    //check total num pageview
    String dbUrl = jobConf.getReduceOutputDbUrl();
    final Connection conn;
    try {/*  www  .j a v a  2s  .  c  om*/
        conn = jobConf.getConnection(dbUrl, true);
    } catch (ClassNotFoundException e) {
        throw new IllegalStateException(e);
    }
    String sumPageviewQuery = "SELECT SUM(pageview) FROM Pageview";
    Statement st = null;
    ResultSet rs = null;
    try {
        st = conn.createStatement();
        rs = st.executeQuery(sumPageviewQuery);
        rs.next();
        long sumPageview = rs.getLong(1);

        LOG.info("totalPageview=" + totalPageview);
        LOG.info("sumPageview=" + sumPageview);

        return totalPageview == sumPageview && totalPageview != 0;
    } finally {
        if (st != null) {
            st.close();
        }
        if (rs != null) {
            rs.close();
        }
        conn.close();
    }
}

From source file:org.ulyssis.ipp.snapshot.Snapshot.java

public static Optional<Snapshot> loadForEvent(Connection connection, Event event)
        throws SQLException, IOException {
    String statement = "SELECT \"id\", \"data\" FROM \"snapshots\" WHERE \"event\" = ?";
    try (PreparedStatement stmt = connection.prepareStatement(statement)) {
        stmt.setLong(1, event.getId().get());
        LOG.debug("executing query: {}", stmt);
        ResultSet rs = stmt.executeQuery();
        if (rs.next()) {
            String data = rs.getString("data");
            Snapshot result = Serialization.getJsonMapper().readValue(data, Snapshot.class);
            result.id = rs.getLong("id");
            result.eventId = event.getId().get();
            return Optional.of(result);
        } else {//  w w w  .  j  ava2 s  .c  om
            return Optional.empty();
        }
    }
}

From source file:org.ulyssis.ipp.snapshot.Snapshot.java

public static Optional<Snapshot> loadBefore(Connection connection, Instant time)
        throws SQLException, IOException {
    String statement = "SELECT \"id\", \"data\", \"event\" FROM \"snapshots\" "
            + "WHERE \"time\" < ? ORDER BY \"time\" DESC, \"event\" DESC FETCH FIRST ROW ONLY";
    try (PreparedStatement stmt = connection.prepareStatement(statement)) {
        stmt.setTimestamp(1, Timestamp.from(time));
        LOG.debug("Executing query: {}", stmt);
        ResultSet rs = stmt.executeQuery();
        if (rs.next()) {
            String data = rs.getString("data");
            Snapshot result = Serialization.getJsonMapper().readValue(data, Snapshot.class);
            result.id = rs.getLong("id");
            result.eventId = rs.getLong("event");
            return Optional.of(result);
        } else {//from   w  ww.  ja v  a2s . c o m
            return Optional.empty();
        }
    }
}

From source file:com.enonic.cms.upgrade.task.UpgradeModel0204.java

private static VirtualFileItem createVirtualFileItem(final ResultSet rs) throws SQLException {
    VirtualFileItem item = new VirtualFileItem();

    final String vf_skey = rs.getString("vf_skey");
    item.key = vf_skey;// ww  w.j  a v a 2s.c  o m
    item.name = rs.getString("vf_sname");
    item.parentKey = rs.getString("vf_sparentkey");
    item.length = rs.getLong("vf_llength");
    item.blobkey = rs.getString("vf_sblobkey");
    return item;
}

From source file:com.tethrnet.manage.db.UserDB.java

/**
 * inserts new user//from w  w  w.ja va  2s. c  o m
 * 
 * @param con DB connection 
 * @param user user object
 */
public static Long insertUser(Connection con, User user) {

    Long userId = null;

    try {
        PreparedStatement stmt = con.prepareStatement(
                "insert into users (email, username, auth_type, user_type, password, salt) values (?,?,?,?,?,?)",
                Statement.RETURN_GENERATED_KEYS);
        stmt.setString(1, user.getEmail());
        stmt.setString(2, user.getUsername());
        stmt.setString(3, user.getAuthType());
        stmt.setString(4, user.getUserType());
        if (StringUtils.isNotEmpty(user.getPassword())) {
            String salt = EncryptionUtil.generateSalt();
            stmt.setString(5, EncryptionUtil.hash(user.getPassword() + salt));
            stmt.setString(6, salt);
        } else {
            stmt.setString(5, null);
            stmt.setString(6, null);
        }
        stmt.execute();
        ResultSet rs = stmt.getGeneratedKeys();
        if (rs != null && rs.next()) {
            userId = rs.getLong(1);
        }
        DBUtils.closeStmt(stmt);

    } catch (Exception e) {
        log.error(e.toString(), e);
    }

    return userId;

}

From source file:com.tethrnet.manage.db.SessionAuditDB.java

/**
 * returns a list of terminal sessions for session id
 *
 * @param sessionId session id/*from   w w  w  .  j  a  v a  2  s .c  o  m*/
 * @return terminal sessions with host information
 */
public static SessionAudit getSessionsTerminals(Long sessionId) {
    //get db connection
    Connection con = null;
    SessionAudit sessionAudit = new SessionAudit();

    String sql = "select * from session_log, users where users.id= session_log.user_id and session_log.id = ? ";
    try {

        con = DBUtils.getConn();
        PreparedStatement stmt = con.prepareStatement(sql);
        stmt.setLong(1, sessionId);

        ResultSet rs = stmt.executeQuery();
        while (rs.next()) {
            sessionAudit.setId(rs.getLong("session_log.id"));
            sessionAudit.setSessionTm(rs.getTimestamp("session_tm"));
            sessionAudit.setUser(UserDB.getUser(con, rs.getLong("user_id")));
            sessionAudit.setHostSystemList(getHostSystemsForSession(con, sessionId));

        }

        DBUtils.closeStmt(stmt);

    } catch (Exception e) {
        log.error(e.toString(), e);
    }
    //close db connection
    DBUtils.closeConn(con);

    return sessionAudit;

}

From source file:com.keybox.manage.db.SessionAuditDB.java

/**
 * insert new session record for user//from www .  j ava 2s  .  c om
 *
 * @param con    DB connection
 * @param userId user id
 * @return session id
 */
public static Long createSessionLog(Connection con, Long userId) {
    Long sessionId = null;
    try {

        //insert
        PreparedStatement stmt = con.prepareStatement("insert into session_log (user_id) values(?)",
                Statement.RETURN_GENERATED_KEYS);
        stmt.setLong(1, userId);
        stmt.execute();
        ResultSet rs = stmt.getGeneratedKeys();
        if (rs != null && rs.next()) {
            sessionId = rs.getLong(1);
        }

        DBUtils.closeStmt(stmt);

    } catch (Exception e) {
        log.error(e.toString(), e);
    }
    return sessionId;

}