Example usage for java.sql ResultSet last

List of usage examples for java.sql ResultSet last

Introduction

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

Prototype

boolean last() throws SQLException;

Source Link

Document

Moves the cursor to the last row in this ResultSet object.

Usage

From source file:de.static_interface.reallifeplugin.module.contract.database.table.ContractUserOptionsTable.java

@Override
public ContractUserOptionsRow[] deserialize(ResultSet rs) throws SQLException {
    int rowcount = 0;
    if (rs.last()) {
        rowcount = rs.getRow();/*ww w.j  a va2  s.com*/
        rs.beforeFirst();
    }

    ContractUserOptionsRow[] rows = new ContractUserOptionsRow[rowcount];
    int i = 0;

    while (rs.next()) {
        ContractUserOptionsRow row = new ContractUserOptionsRow();
        if (hasColumn(rs, "id")) {
            row.id = rs.getInt("id");
        }
        if (hasColumn(rs, "user_id")) {
            row.userId = rs.getInt("userId");
        }
        if (hasColumn(rs, "contract_id")) {
            row.contractId = rs.getInt("contract_id");
        }
        if (hasColumn(rs, "isCreator")) {
            row.isCreator = rs.getBoolean("isCreator");
        }
        if (hasColumn(rs, "money")) {
            row.money = rs.getDouble("money");
            if (rs.wasNull()) {
                row.money = null;
            }
        }
        rows[i] = row;
        i++;
    }
    return rows;
}

From source file:de.static_interface.reallifeplugin.module.corporation.database.table.CorpsTable.java

@Override
public CorpRow[] deserialize(ResultSet rs) throws SQLException {
    int rowcount = 0;
    if (rs.last()) {
        rowcount = rs.getRow();/*from w w w. java2s. c o  m*/
        rs.beforeFirst();
    }

    CorpRow[] rows = new CorpRow[rowcount];
    int i = 0;

    while (rs.next()) {
        CorpRow row = new CorpRow();
        if (hasColumn(rs, "id")) {
            row.id = rs.getInt("id");
        }
        if (hasColumn(rs, "balance")) {
            row.balance = rs.getDouble("balance");
        }
        if (hasColumn(rs, "base_id")) {
            row.baseId = rs.getString("base_id");
        }
        if (hasColumn(rs, "base_world")) {
            row.baseWorld = rs.getString("base_world");
        }
        if (hasColumn(rs, "ceo_uuid")) {
            row.ceoUniqueId = UUID.fromString(rs.getString("ceo_uuid"));
        }
        if (hasColumn(rs, "corp_name")) {
            row.corpName = rs.getString("corp_name");
        }
        if (hasColumn(rs, "isdeleted")) {
            row.isDeleted = rs.getBoolean("isdeleted");
        }
        if (hasColumn(rs, "tag")) {
            row.tag = rs.getString("tag");
        }
        if (hasColumn(rs, "time")) {
            row.time = rs.getLong("time");
        }
        rows[i] = row;
        i++;
    }
    return rows;
}

From source file:com.autentia.tnt.bill.migration.support.OriginalInformationRecoverer.java

/**
 * Recupera la suma total de todos los conceptos de cada una de las facturas cuyo tipo se envia por parametro
 * @param billType tipo de factura//  ww w. ja v a 2 s.c  o  m
 */
public static double[] getImporteFacturaOriginal(String billType) throws Exception {
    Connection con = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    LineNumberReader file = null;
    double[] result = new double[0];

    try {
        log.info("RECOVERING IMPORTE FACTURAS " + billType + " ORIGINALES");

        // connect to database
        Class.forName(BillToBillPaymentMigration.DATABASE_DRIVER);
        con = DriverManager.getConnection(BillToBillPaymentMigration.DATABASE_CONNECTION,
                BillToBillPaymentMigration.DATABASE_USER, BillToBillPaymentMigration.DATABASE_PASS); //NOSONAR
        con.setAutoCommit(false); //DATABASE_PASS vacio.            

        String sql = "SELECT sum((bb.units*bb.amount)*(1+(bb.iva/100))) as total from Bill b left join BillBreakDown bb on b.id=bb.billId, Organization o, Project p where b.projectId = p.id and p.organizationId = o.id and b.billType= ? group by b.id order by total";

        pstmt = con.prepareStatement(sql);

        rs = pstmt.executeQuery();
        pstmt.setString(1, billType);

        rs.last();
        result = new double[rs.getRow()];
        rs.beforeFirst();
        int counter = 0;

        while (rs.next()) {
            result[counter] = rs.getDouble(1);
            log.info("\t" + result[counter]);
            counter++;
        }
        con.commit();
    } catch (Exception e) {
        log.error("FAILED: WILL BE ROLLED BACK: ", e);
        if (con != null) {
            con.rollback();
        }
    } finally {
        cierraFichero(file);
        liberaConexion(con, pstmt, rs);
    }
    return result;
}

From source file:database.DataBaseMySQL.java

public int getRowCount(ResultSet resultSet) {
    if (resultSet == null) {
        return 0;
    }/*from  ww  w.  j  a  v  a2 s  .  c om*/
    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:com.bluexml.side.Integration.alfresco.sql.synchronization.common.JdbcTransactionListener.java

public int executeSelectQuery(String sqlQuery) throws SQLException {
    Connection connection = getConnection();
    Statement st = null;/*  ww w . java  2 s  .  c om*/
    int rowCount = -1;
    try {
        if (logger.isDebugEnabled())
            logger.debug("[executeSQLQuery] " + sqlQuery);
        st = connection.createStatement();
        ResultSet results = st.executeQuery(sqlQuery);
        results.last();
        rowCount = results.getRow();
        if (logger.isDebugEnabled())
            logger.debug("[executeSQLQuery] Row count: " + rowCount);
    } catch (SQLException e) {
        logger.error("[executeSQLQuery]", e);
        throw (e);
    } finally {
        if (st != null) {
            try {
                st.close();
            } catch (SQLException e) {
                logger.error("[executeSQLQuery]", e);
            }
            st = null;
        }
    }

    return rowCount;
}

From source file:com.laxser.blitz.lama.provider.jdbc.PreparedStatementCallbackReturnId.java

@Override
public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {

    if (setter != null) {
        setter.setValues(ps);/*w w w . j av a 2s. co  m*/
    }

    int updated = ps.executeUpdate();
    if (updated == 0) {
        if (returnType.isArray()) {
            return Array.newInstance(wrappedIdType, 0);
        } else {
            return defaultValueOf(wrappedIdType);
        }
    }

    ResultSet keys = ps.getGeneratedKeys();
    if (keys != null) {
        try {
            Object ret = null;
            if (returnType.isArray()) {
                keys.last();
                int length = keys.getRow();
                keys.beforeFirst();
                ret = Array.newInstance(wrappedIdType, length);
            }

            for (int i = 0; keys.next(); i++) {
                Object value = mapper.mapRow(keys, i);
                if (value == null && idType.isPrimitive()) {
                    // ?primitive??null??
                    value = defaultValueOf(wrappedIdType);
                }
                if (ret != null) {
                    Array.set(ret, i + 1, value);
                } else {
                    ret = value;
                    break;
                }
            }
            return ret;
        } finally {
            JdbcUtils.closeResultSet(keys);
        }
    } else {
        if (returnType.isArray()) {
            return Array.newInstance(wrappedIdType, 0);
        } else {
            return defaultValueOf(wrappedIdType);
        }
    }
}

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

public int getResultSetCount(ResultSet rs) {
    int con = 0;/*from  w  w w. j  a  v  a2s .c om*/
    try {
        rs.last();
        con = rs.getRow();
        rs.beforeFirst();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return con;
}

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.ja  v  a 2s. 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 {//w  w  w . j  ava  2  s  .c om
        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  www.  ja v a  2  s . c om*/
        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;
}