Example usage for java.sql ResultSet next

List of usage examples for java.sql ResultSet next

Introduction

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

Prototype

boolean next() throws SQLException;

Source Link

Document

Moves the cursor forward one row from its current position.

Usage

From source file:application.gen.gen.java

public static List<Column> getColumns(Connection con, String catalog, String schema, String tableName)
        throws SQLException {
    List<Column> list = new ArrayList<Column>();
    ResultSet rs = con.getMetaData().getColumns(catalog, schema, tableName, "%");
    while (rs != null && rs.next()) {
        // System.out.println(rs.getString("COLUMN_NAME")+"
        // "+rs.getString("TYPE_NAME")+" "+rs.getString("COLUMN_SIZE")+"
        // "+rs.getString("NULLABLE"));
        Column column = new Column(rs.getString("COLUMN_NAME"), rs.getString("TYPE_NAME"),
                rs.getString("COLUMN_SIZE"), rs.getString("NULLABLE"));
        list.add(column);/*ww w.  j a  va  2 s  . com*/
    }

    return list;
}

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 a  v  a2s.  c  o  m
            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  w w. j  a  v  a2 s  . com
            return Optional.empty();
        }
    }
}

From source file:cn.edu.zju.acm.onlinejudge.persistence.sql.Database.java

/**
 * Gets the last id./*from  w  w  w  .  j  a  va2s .c  o  m*/
 * 
 * @param conn
 * @param ps
 * @param rs
 * @return the last id
 * @throws SQLException
 */
public static long getLastId(Connection conn) throws SQLException {
    PreparedStatement ps = null;
    try {
        ps = conn.prepareStatement(Database.GET_LAST_ID);
        ResultSet rs = ps.executeQuery();
        rs.next();
        return rs.getLong(1);
    } finally {
        Database.dispose(ps);
    }
}

From source file:dsd.dao.RawDataDAO.java

public static long GetCount() {
    long count = 0;
    try {/*from   w  ww . j  a v  a2s  . c  om*/
        Connection con = DAOProvider.getDataSource().getConnection();
        try {
            ResultSet results = DAOProvider.SelectTableSecure(tableName, " count(*) ", "", "", con, null);
            while (results.next()) {
                count = results.getLong(1);
            }
        } catch (Exception exc) {
            exc.printStackTrace();
        }
        con.close();
    } catch (Exception exc) {
        exc.printStackTrace();
    }
    return count;
}

From source file:org.springside.modules.test.data.H2Fixtures.java

/**
 * ,excludeTables.disable.// ww  w  . j av a  2s.c  om
 */
public static void deleteAllTable(DataSource dataSource, String... excludeTables) {

    List<String> tableNames = new ArrayList<String>();
    ResultSet rs = null;
    try {
        rs = dataSource.getConnection().getMetaData().getTables(null, null, null, new String[] { "TABLE" });
        while (rs.next()) {
            String tableName = rs.getString("TABLE_NAME");
            if (Arrays.binarySearch(excludeTables, tableName) < 0) {
                tableNames.add(tableName);
            }
        }

        deleteTable(dataSource, tableNames.toArray(new String[tableNames.size()]));
    } catch (SQLException e) {
        Exceptions.unchecked(e);
    } finally {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}

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

/**
 * returns profile based on id//from  w ww . ja v  a  2s .c  o m
 *
 * @param con db connection object
 * @param profileId profile id
 * @return profile
 */
public static Profile getProfile(Connection con, Long profileId) {

    Profile profile = null;
    try {
        PreparedStatement stmt = con.prepareStatement("select * from profiles where id=?");
        stmt.setLong(1, profileId);
        ResultSet rs = stmt.executeQuery();

        while (rs.next()) {
            profile = new Profile();
            profile.setId(rs.getLong("id"));
            profile.setNm(rs.getString("nm"));
            profile.setDesc(rs.getString("desc"));
            profile.setHostSystemList(ProfileSystemsDB.getSystemsByProfile(con, profileId));

        }
        DBUtils.closeRs(rs);
        DBUtils.closeStmt(stmt);

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

    return profile;
}

From source file:com.nokia.helium.metadata.DerbyFactoryManagerCreator.java

/**
 * Checks the database integrity.//from  www .  ja  va2 s .  co m
 * @param urlPath - database path to be connected to.
 * @return boolean - true if db is valid false otherwise.
 */
private static boolean checkDatabaseIntegrity(File database) throws MetadataException {
    boolean result = false;
    Connection connection = null;
    try {
        connection = DriverManager.getConnection("jdbc:derby:" + database);
        if (connection != null) {
            Statement stmt = connection.createStatement();
            ResultSet rs = stmt.executeQuery("select version from version");
            int version = -1;
            if (rs.next()) {
                version = rs.getInt(1);
            }
            rs.close();
            stmt.close();
            connection.close();
            connection = null;
            result = version == Version.DB_VERSION;
        }
    } catch (SQLException ex) {
        try {
            DriverManager.getConnection("jdbc:derby:;shutdown=true");
        } catch (java.sql.SQLException sex) {
            // normal exception during database shutdown
            connection = null;
        }
        return false;
    } finally {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (java.sql.SQLException sex) {
            // normal exception during database shutdown
            connection = null;
        }
        connection = null;
        if (!result) {
            try {
                DriverManager.getConnection("jdbc:derby:;shutdown=true");
            } catch (java.sql.SQLException sex) {
                // normal exception during database shutdown
                connection = null;
            }
        }
    }
    //shutdown unloads the driver, driver need to be loaded again.
    return result;
}

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

/**
 * returns all profile information/*from   www  .j  av  a2 s . c o m*/
 *
 * @return list of profiles
 */
public static List<Profile> getAllProfiles() {

    ArrayList<Profile> profileList = new ArrayList<>();
    Connection con = null;
    try {
        con = DBUtils.getConn();
        PreparedStatement stmt = con.prepareStatement("select * from  profiles order by nm asc");
        ResultSet rs = stmt.executeQuery();

        while (rs.next()) {
            Profile profile = new Profile();
            profile.setId(rs.getLong("id"));
            profile.setNm(rs.getString("nm"));
            profile.setDesc(rs.getString("desc"));
            profileList.add(profile);

        }
        DBUtils.closeRs(rs);
        DBUtils.closeStmt(stmt);

    } catch (Exception e) {
        log.error(e.toString(), e);
    } finally {
        DBUtils.closeConn(con);
    }

    return profileList;
}

From source file:dsd.dao.RawDataDAO.java

public static long GetMaxTimestamp() {
    long timestamp = 0;
    try {/*  w  ww .  ja  v  a  2s  .c om*/
        Connection con = DAOProvider.getDataSource().getConnection();
        try {
            ResultSet results = DAOProvider.SelectTableSecure(tableName, " max(timestamp) ", "", "", con, null);
            while (results.next()) {
                timestamp = results.getTimestamp(1).getTime();
            }
        } catch (Exception exc) {
            //            exc.printStackTrace();
            timestamp = 0;
        }
        con.close();
    } catch (Exception exc) {
        exc.printStackTrace();
    }
    return timestamp;
}