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:com.bluepandora.therap.donatelife.service.CheckService.java

public static int requestTracker(String mobileNumber, String date, DatabaseService dbService) {

    String query = GetQuery.getPersonRequestTrackerQuery(mobileNumber, date);
    // Debug.debugLog("GET REQUEST: ", query);
    ResultSet result = dbService.getResultSet(query);
    int totalRequestFound = 0;
    String dailyRequest = "0";
    try {//from   w ww.  ja v a  2 s  .  c  o  m
        while (result.next()) {
            dailyRequest = (String) result.getString("daily_request");
        }
        totalRequestFound = Integer.parseInt(dailyRequest);
    } catch (SQLException error) {
        Logger.getLogger(CheckService.class.getName()).log(Level.SEVERE, null, error);
    }

    return totalRequestFound;
}

From source file:com.nabla.dc.server.handler.fixed_asset.Asset.java

static private int getAssetCostBeforeDisposal(final Connection conn, int assetId) throws SQLException {
    final PreparedStatement stmt = StatementFormat.prepare(conn,
            "SELECT SUM(amount) FROM fa_transaction WHERE fa_asset_id=? AND class='COST';", assetId);
    try {//from   w w w.j  av a 2s  .  co  m
        final ResultSet rs = stmt.executeQuery();
        try {
            return rs.next() ? rs.getInt(1) : 0;
        } finally {
            rs.close();
        }
    } finally {
        stmt.close();
    }
}

From source file:com.nabla.dc.server.handler.fixed_asset.Asset.java

static private int getAssetDepreciationBeforeDisposal(final Connection conn, int assetId) throws SQLException {
    final PreparedStatement stmt = StatementFormat.prepare(conn,
            "SELECT SUM(amount) FROM fa_transaction WHERE fa_asset_id=? AND class='DEP';", assetId);
    try {/*  www  .  j  av a2 s .  c o  m*/
        final ResultSet rs = stmt.executeQuery();
        try {
            return rs.next() ? rs.getInt(1) : 0;
        } finally {
            rs.close();
        }
    } finally {
        stmt.close();
    }
}

From source file:libepg.util.db.AboutDB.java

/**
 * (???)//from  ww  w  . j ava  2 s  .c  om
 *
 * @param conn ??DB??
 * @throws java.sql.SQLException
 */
public static void debug_dump_table(Connection conn) throws SQLException {
    if (LOG.isDebugEnabled()) {
        final String DUMP = "SELECT * FROM " + TABLE_NAME + " ORDER BY number ASC";
        ResultSet rs = conn.createStatement().executeQuery(DUMP);
        while (rs.next()) {
            StringBuilder sb1 = new StringBuilder();
            sb1.append(NUMBER);
            sb1.append(" = ");
            sb1.append(rs.getInt(NUMBER));
            sb1.append("####");
            sb1.append(PID);
            sb1.append(" = ");
            sb1.append(rs.getInt(PID));
            sb1.append("####");
            sb1.append(CONTINUITY_CONTROL);
            sb1.append(" = ");
            sb1.append(rs.getInt(CONTINUITY_CONTROL));
            sb1.append("####");
            sb1.append(LACKFLAG);
            sb1.append(" = ");
            sb1.append(rs.getInt(LACKFLAG));
            sb1.append("####");
            sb1.append(PACKET);
            sb1.append(" = ");
            sb1.append(Hex.encodeHexString(rs.getBytes(PACKET)));
            LOG.debug(sb1.toString());
        }
    }
}

From source file:com.github.ibole.infrastructure.persistence.db.mybatis.pagination.SqlHelper.java

/**
 * //w w  w.  j  av  a 2s.  c o  m
 *
 * @param mappedStatement mapped
 * @param parameterObject ?
 * @param boundSql boundSql
 * @param dialect database dialect
 * @return 
 * @throws java.sql.SQLException sql
 */
public static int getCount(String sql, final MappedStatement mappedStatement, final Transaction transaction,
        final Object parameterObject, final BoundSql boundSql, Dialect dialect) throws SQLException {
    final String countSql = dialect.getCountString(sql);
    if (logger.isDebugEnabled()) {
        logger.debug("Total count SQL [{}] ", countSql);
        logger.debug("Total count Parameters: {} ", parameterObject);
    }
    Connection connection = transaction.getConnection();
    PreparedStatement countStmt = connection.prepareStatement(countSql);
    DefaultParameterHandler handler = new DefaultParameterHandler(mappedStatement, parameterObject, boundSql);
    handler.setParameters(countStmt);

    ResultSet rs = countStmt.executeQuery();
    int count = 0;
    if (rs.next()) {
        count = rs.getInt(1);
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Total count: {}", count);
    }
    return count;

}

From source file:com.nabla.wapp.server.database.Database.java

/**
 * Test if a table has any data//from  w ww.  jav  a  2  s .c  o m
 * @param conn      - database connection
 * @param tableName   - table name
 * @return success
 * @throws SQLException
 */
public static boolean isTableEmpty(final Connection conn, final String tableName) throws SQLException {
    final Statement stmt = conn.createStatement();
    try {
        final ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM " + tableName + ";");
        try {
            return rs.next() && rs.getInt(1) == 0;
        } finally {
            rs.close();
        }
    } finally {
        stmt.close();
    }
}

From source file:moderation.Moderate.java

public static List getSavedList(String album_id) throws SQLException {

    List saved = new ArrayList();
    String sql = "select post_id from albumpost where album_id=" + album_id + ";";
    ResultSet rs = testing.Database.getResultset(sql, new setting.Conn().getConnection());
    while (rs.next()) {
        saved.add(rs.getString("post_id"));

    }/* www  . j a v  a 2s  .  c  om*/

    return saved;

}

From source file:modelo.ApiManager.java

public static List resultSetToArrayList(ResultSet rs) throws SQLException {
    ResultSetMetaData md = rs.getMetaData();
    int columns = md.getColumnCount();
    ArrayList list = new ArrayList();

    while (rs.next()) {
        HashMap row = new HashMap(columns);
        for (int i = 1; i <= columns; ++i) {
            row.put(md.getColumnName(i), rs.getObject(i));
        }//from w  w w . j a va2 s .  c o m
        list.add(row);
    }

    return list;
}

From source file:com.thoughtworks.go.server.database.DatabaseFixture.java

public static void assertColumnType(BasicDataSource dataSource, String tableName, String columnName,
        String expected) throws SQLException {
    Connection connection = dataSource.getConnection();
    try {//w w  w .  j  a v a 2s .c o  m
        ResultSet set = connection.getMetaData().getColumns(null, null, null, null);
        while (set.next()) {
            if (set.getString("TABLE_NAME").equalsIgnoreCase(tableName)
                    && set.getString("COLUMN_NAME").equalsIgnoreCase(columnName)) {
                String typeName = set.getString("TYPE_NAME");
                int typeWidth = set.getInt("COLUMN_SIZE");
                String type = typeName + "(" + typeWidth + ")";
                assertThat("Expected " + columnName + " to be " + expected + " type but was " + type, type,
                        is(expected));
                return;
            }
        }
        Assert.fail("Column " + columnName + " does not exist");
    } finally {
        try {
            connection.close();
        } catch (Exception ignored) {
        }
    }
}

From source file:com.jagornet.dhcp.db.DbSchemaManager.java

/**
 * Validate schema.//w ww.  j a v  a2  s  .c  om
 * 
 * @param dataSource the data source
 * 
 * @throws SQLException if there is a problem with the database
 * @throws IOExcpetion if there is a problem reading the schema file
 * 
 * returns true if database was created, false otherwise
 */
public static boolean validateSchema(DataSource dataSource, String schemaFilename, int schemaVersion)
        throws SQLException, IOException {
    boolean schemaCreated = false;

    List<String> tableNames = new ArrayList<String>();

    Connection conn = dataSource.getConnection();
    DatabaseMetaData dbMetaData = conn.getMetaData();

    log.info("JDBC Connection Info:\n" + "url = " + dbMetaData.getURL() + "\n" + "database = "
            + dbMetaData.getDatabaseProductName() + " " + dbMetaData.getDatabaseProductVersion() + "\n"
            + "driver = " + dbMetaData.getDriverName() + " " + dbMetaData.getDriverVersion());

    String[] types = { "TABLE" };
    ResultSet rs = dbMetaData.getTables(null, null, "%", types);
    if (rs.next()) {
        tableNames.add(rs.getString("TABLE_NAME"));
    } else {
        createSchema(dataSource, schemaFilename);
        dbMetaData = conn.getMetaData();
        rs = dbMetaData.getTables(null, null, "%", types);
        schemaCreated = true;
    }
    while (rs.next()) {
        tableNames.add(rs.getString("TABLE_NAME"));
    }

    String[] schemaTableNames;
    if (schemaVersion <= 1) {
        schemaTableNames = TABLE_NAMES;
    } else {
        schemaTableNames = TABLE_NAMES_V2;
    }

    if (tableNames.size() == schemaTableNames.length) {
        for (int i = 0; i < schemaTableNames.length; i++) {
            if (!tableNames.contains(schemaTableNames[i])) {
                throw new IllegalStateException("Invalid database schema: unknown tables");
            }
        }
    } else {
        throw new IllegalStateException("Invalid database schema: wrong number of tables");
    }

    return schemaCreated;
}