Example usage for java.sql ResultSet beforeFirst

List of usage examples for java.sql ResultSet beforeFirst

Introduction

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

Prototype

void beforeFirst() throws SQLException;

Source Link

Document

Moves the cursor to the front of this ResultSet object, just before the first row.

Usage

From source file:com.imagelake.control.PaymentPreferenceDAOImp.java

public int getResultSetCount(ResultSet rs) {
    int con = 0;//from  ww w. ja  v a  2s.c o m
    try {
        rs.last();
        con = rs.getRow();
        rs.beforeFirst();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return con;
}

From source file:orange.save.export.sql.DocBuilder.java

private void getMultiValuedEntity(ResultSet resultSet, Entity entity, Map<String, Object> rootEntityMap)
        throws SQLException {
    List<Object> fieldArray = new ArrayList<Object>();
    resultSet.beforeFirst();
    while (resultSet.next()) {
        if (entity.fields.size() > 1) {
            Map<String, Object> entityFieldsMap = new HashMap<String, Object>();
            for (Iterator<Field> iterator = entity.fields.iterator(); iterator.hasNext();) {
                Field field = iterator.next();
                FieldType fieldType = FieldType.valueOf(field.allAttributes.get("type").toUpperCase());
                entityFieldsMap.put(field.name,
                        convertFieldType(fieldType, resultSet.getObject(field.column)).get(0));
            }//from   w  w  w .  jav  a 2s .  c  o  m
            fieldArray.add(entityFieldsMap);
        } else if (entity.fields.size() == 1) {
            fieldArray.add(resultSet.getObject(entity.fields.get(0).column));
        }
    }
    rootEntityMap.put(entity.name, fieldArray);
}

From source file:database.DataBaseMySQL.java

public int getRowCount(ResultSet resultSet) {
    if (resultSet == null) {
        return 0;
    }//from   ww  w .  j a va  2  s  .com
    try {
        resultSet.last();
        return resultSet.getRow();
    } catch (SQLException exp) {
        exp.printStackTrace();
    } finally {
        try {
            resultSet.beforeFirst();
        } catch (SQLException exp) {
            exp.printStackTrace();
        }
    }
    return 0;
}

From source file:org.giswater.dao.MainDao.java

public static int getNumberOfRows(ResultSet rs) {

    if (rs == null) {
        return 0;
    }/*  w ww . jav  a  2  s. com*/
    try {
        if (rs.getType() == ResultSet.TYPE_FORWARD_ONLY) {
            System.out.println("FORWARD");
            return 0;
        }
        rs.last();
        return rs.getRow();
    } catch (SQLException e) {
        Utils.logError(e);
    } finally {
        try {
            rs.beforeFirst();
        } catch (SQLException e) {
            Utils.logError(e);
        }
    }
    return 0;

}

From source file:com.tera.common.database.query.CQueryService.java

@Override
public int[] getUsedIds(String query, String idColumn) {
    PreparedStatement statement = prepareStatement(query, ResultSet.TYPE_SCROLL_INSENSITIVE,
            ResultSet.CONCUR_READ_ONLY);
    ResultSet resultSet = null;
    try {/*from   w  w  w  .j  av  a2 s.  c  o m*/
        resultSet = statement.executeQuery();
        resultSet.last();
        int count = resultSet.getRow();
        resultSet.beforeFirst();
        int[] ids = new int[count];
        for (int i = 0; i < count; i++) {
            resultSet.next();
            ids[i] = resultSet.getInt(idColumn);
        }
        return ids;
    } catch (SQLException e) {
        log.error("Can't get id's using query {}", query, e);
    } finally {
        close(resultSet, statement);
    }

    return new int[0];
}

From source file:com.vigglet.util.ModelUtilBase.java

public Collection<T> getPkMap() {
    Collection<T> result = null;

    try {//from w  w  w .ja  v a2 s . co m
        PreparedStatement stmt = getSelectAllQuery();
        ResultSet rs = stmt.executeQuery();

        rs.last();
        result = new ArrayList<>(rs.getRow());
        rs.beforeFirst();

        while (rs.next()) {
            result.add(readModel(rs));
        }

        rs.close();
        stmt.close();

    } catch (SQLException ex) {
        logger.log(Level.SEVERE, null, ex);
    }

    return result;
}

From source file:com.vigglet.util.ModelUtilBase.java

public Collection<T> findByCompany(int company) {
    Collection<T> result = null;

    try {/*from  ww  w  .  j  a  va  2s  .co m*/
        PreparedStatement stmt = getFindByCompanyQuery();
        stmt.setInt(1, company);
        ResultSet rs = stmt.executeQuery();

        rs.last();
        result = new ArrayList<>(rs.getRow());
        rs.beforeFirst();

        while (rs.next()) {
            result.add(readModel(rs));
        }

        rs.close();
        stmt.close();

    } catch (SQLException ex) {
        logger.log(Level.SEVERE, null, ex);
    }

    return result;
}

From source file:com.vigglet.util.ModelUtilBase.java

public List<T> getLimitedList(int company, int limit) {
    long startMs = System.currentTimeMillis();
    logger.log(Level.INFO, "Fetching limited (" + limit + ") list...");
    List<T> result = null;/*from w ww . j a v  a 2s . co  m*/

    try {
        PreparedStatement stmt = getLimitedQuery();
        stmt.setInt(1, company);
        stmt.setInt(2, limit);
        ResultSet rs = stmt.executeQuery();

        rs.last();
        result = new ArrayList<>(rs.getRow());
        rs.beforeFirst();

        while (rs.next()) {
            result.add(readModel(rs));
        }

        rs.close();
        stmt.close();

    } catch (SQLException ex) {
        logger.log(Level.SEVERE, null, ex);
    }

    long endMs = System.currentTimeMillis();
    logger.log(Level.INFO, "Done in " + (endMs - startMs) + " ms (" + (endMs - startMs) / 1000 + " s)");
    return result;
}

From source file:com.vigglet.util.ModelUtilBase.java

public List<T> getOrderdList(int company, String orderField, String orderDirection) {
    List<T> result = null;/*from   ww  w .  ja va 2  s.c  o m*/

    try {
        PreparedStatement stmt = null;
        if (orderDirection != null && orderDirection.length() > 0) {
            stmt = getOrderByQuery(orderField, orderDirection);
        } else {
            stmt = getOrderByQuery(orderField);
        }
        stmt.setInt(1, company);
        ResultSet rs = stmt.executeQuery();

        rs.last();
        result = new ArrayList<>(rs.getRow());
        rs.beforeFirst();

        while (rs.next()) {
            result.add(readModel(rs));
        }

        rs.close();
        stmt.close();

    } catch (SQLException ex) {
        logger.log(Level.SEVERE, null, ex);
    }

    return result;
}

From source file:eu.optimis_project.monitoring.storage.MySQLStorageManager.java

@Override
public Set<Measurement> getAllData() throws IOException {

    String query = "SELECT * FROM " + tableName;

    Connection connection = null;
    PreparedStatement statement = null;

    try {//  w  w  w .j  a  v  a 2s  .c o  m
        connection = getConnection();
        statement = connection.prepareStatement(query);

        log.info("Using statement: " + statement);

        ResultSet rs = statement.executeQuery();

        Set<Measurement> entryList = new HashSet<Measurement>();
        for (rs.beforeFirst(); rs.next();) {
            try {
                String serviceID = rs.getString(ENTRIES_COLUMNINDEX_SERVICEID);
                String instanceID = rs.getString(ENTRIES_COLUMNINDEX_INSTANCEID);
                String name = rs.getString(ENTRIES_COLUMNINDEX_NAME);
                String data = rs.getString(ENTRIES_COLUMNINDEX_DATA);
                long timestamp = rs.getLong(ENTRIES_COLUMNINDEX_TIMESTAMP);
                entryList.add(new Measurement(serviceID, instanceID, name, data, timestamp));
            } catch (Exception e) {
                throw new IOException("Failed to read measurement from database.", e);
            }
        }

        log.debug("Found " + entryList.size() + " measurements in database");
        return entryList;

    } catch (SQLException e) {

        throw new IOException(e);
    } finally {

        try {
            if (statement != null) {
                statement.close();
            }
        } catch (SQLException e) {
        }

        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
        }
    }
}