Example usage for java.sql ResultSet getInt

List of usage examples for java.sql ResultSet getInt

Introduction

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

Prototype

int getInt(String columnLabel) throws SQLException;

Source Link

Document

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

Usage

From source file:edu.ku.brc.specify.conversion.ConvertMiscData.java

/**
 * //from w w  w.  java 2 s . c om
 */
public static void convertObservations(final Connection oldDBConn, final Connection newDBConn,
        final int disciplineID) {
    IdMapperMgr.getInstance().setDBs(oldDBConn, newDBConn);

    String sql = "SELECT cc.CollectionObjectCatalogID, o.ObservationID, o.Text1, o.Text2, o.Number1, o.Remarks ";
    String baseSQL = " FROM collectionobjectcatalog AS cc Inner Join observation AS o ON cc.CollectionObjectCatalogID = o.BiologicalObjectID";
    String ORDERBY = " ORDER BY cc.CollectionObjectCatalogID";

    Calendar cal = Calendar.getInstance();
    Timestamp tsCreated = new Timestamp(cal.getTimeInMillis());
    IdMapperIFace coMapper = IdMapperMgr.getInstance().get("collectionobjectcatalog",
            "CollectionObjectCatalogID");
    if (coMapper == null) {
        coMapper = IdMapperMgr.getInstance().addTableMapper("collectionobjectcatalog",
                "CollectionObjectCatalogID", false);
    }

    int totalCnt = BasicSQLUtils.getCountAsInt(oldDBConn, "SELECT COUNT(*) " + baseSQL);
    if (totalCnt < 1)
        return;

    Statement stmt = null;
    PreparedStatement pStmt = null;
    PreparedStatement updateStmt = null;
    PreparedStatement insertStmt = null;
    PreparedStatement updateCOStmt = null;
    try {
        pStmt = newDBConn.prepareStatement(
                "SELECT co.CollectionObjectAttributeID FROM collectionobject AS co WHERE co.CollectionObjectID = ? AND co.CollectionObjectAttributeID IS NOT NULL");
        updateStmt = newDBConn.prepareStatement(
                "UPDATE collectionobjectattribute SET Text1=?, Text2=?, Number1=?, Remarks=? WHERE CollectionObjectAttributeID = ?");
        insertStmt = newDBConn.prepareStatement(
                "INSERT INTO collectionobjectattribute (Version, TimestampCreated, CollectionMemberID, CreatedByAgentID, Text1, Text2, Number1, Remarks) VALUES(0, ?, ?, ?, ?, ?, ?, ?)",
                Statement.RETURN_GENERATED_KEYS);
        updateCOStmt = newDBConn.prepareStatement(
                "UPDATE collectionobject SET CollectionObjectAttributeID=? WHERE CollectionObjectID = ?");

        int cnt = 0;

        stmt = oldDBConn.createStatement();
        ResultSet rs = stmt.executeQuery(sql + baseSQL + ORDERBY);
        while (rs.next()) {
            int ccId = rs.getInt(1);
            String text1 = rs.getString(3);
            String text2 = rs.getString(4);
            Integer number1 = rs.getInt(5);
            String remarks = rs.getString(6);
            Integer newId = coMapper.get(ccId);
            if (newId == null) {
                log.error("Old Co Id [" + ccId + "] didn't map to new ID.");
                continue;
            }

            pStmt.setInt(1, newId);
            ResultSet rs2 = pStmt.executeQuery();
            if (rs2.next()) {
                updateStmt.setString(1, text1);
                updateStmt.setString(2, text2);
                updateStmt.setInt(3, number1);
                updateStmt.setString(4, remarks);
                updateStmt.setInt(5, rs2.getInt(1));
                if (updateStmt.executeUpdate() != 1) {
                    log.error("Error updating collectionobjectattribute");
                }
            } else {
                int memId = BasicSQLUtils.getCountAsInt(
                        "SELECT CollectionMemberID FROM collectionobject WHERE CollectionObjectID = " + newId);
                insertStmt.setTimestamp(1, tsCreated);
                insertStmt.setInt(2, memId);
                insertStmt.setInt(3, 1); // Created By Agent
                insertStmt.setString(4, text1);
                insertStmt.setString(5, text2);
                insertStmt.setInt(6, number1);
                insertStmt.setString(7, remarks);

                if (insertStmt.executeUpdate() != 1) {
                    log.error("Error inserting collectionobjectattribute");
                }

                int newCOAId = BasicSQLUtils.getInsertedId(insertStmt);

                updateCOStmt.setInt(1, newCOAId);
                updateCOStmt.setInt(2, newId);
                if (updateCOStmt.executeUpdate() != 1) {
                    log.error(
                            "Error updating collectionobject newCOAId[" + newCOAId + "] newId[" + newId + "]");
                }
            }
            rs2.close();

            cnt++;
            if (cnt % 1000 == 0) {
                System.out.println(String.format("%d / %d", cnt, totalCnt));
            }
        }
        rs.close();

    } catch (Exception e) {
        e.printStackTrace();

    } finally {
        try {
            if (stmt != null)
                stmt.close();
            if (pStmt != null)
                pStmt.close();
            if (updateStmt != null)
                updateStmt.close();
            if (insertStmt != null)
                insertStmt.close();
            if (updateCOStmt != null)
                updateCOStmt.close();

        } catch (SQLException ex) {
        }
    }
}

From source file:eu.sisob.uma.restserver.AuthorizationManager.java

private static boolean DBAuthorizeUserIn(String user, String pass, UserAttributes out_attributes) {
    boolean success = false;
    Connection conn = null;/*from www  .j  ava2  s  .  c  o  m*/
    PreparedStatement statement = null;
    ResultSet rs = null;
    try {
        String query = "SELECT `user_tasks_allow` as n_tasks_allow, `user_type` as account_type FROM USERS WHERE user_email = ? and user_pass = ?";
        conn = SystemManager.getInstance().getSystemDbPool().getConnection();
        statement = conn.prepareStatement(query);
        statement.setString(1, user);
        statement.setString(2, pass);

        rs = statement.executeQuery();
        if (rs.next()) {

            out_attributes.setAccountType(rs.getString("account_type"));
            out_attributes.setNTasksAllow((Integer) rs.getInt("n_tasks_allow"));

            success = true;
        } else
            success = false;
    } catch (SQLException ex) {
        ProjectLogger.LOGGER.error("", ex);
        success = false;
    } catch (Exception ex) {
        ProjectLogger.LOGGER.error("", ex);
        success = false;
    } finally {
        if (rs != null)
            try {
                rs.close();
            } catch (SQLException ex) {
                ProjectLogger.LOGGER.error("", ex);
            }

        if (statement != null)
            try {
                statement.close();
            } catch (SQLException ex) {
                ProjectLogger.LOGGER.error("", ex);
            }

        if (conn != null)
            try {
                conn.close();
            } catch (SQLException ex) {
                ProjectLogger.LOGGER.error("", ex);
            }

        statement = null;
        rs = null;
    }

    return success;
}

From source file:net.freechoice.model.orm.Map_Transaction.java

@Override
public FC_Transaction mapRow(final ResultSet rs, int rowNum) throws SQLException {

    FC_Transaction transaction = new FC_Transaction();

    transaction.id = rs.getInt(1);
    transaction.id_account_ = rs.getInt(2);
    transaction.time_committed = rs.getTimestamp(3);
    transaction.amount = rs.getBigDecimal(4);
    transaction.comment = rs.getString(5);

    return transaction;
}

From source file:com.leapfrog.inventorymanagementsystem.dao.impl.SalesDAOImpl.java

@Override
public List<Sales> getALL(boolean status) throws SQLException, ClassNotFoundException {
    String sql = "SELECT * FROM tbl_sales WHERE 1=1";

    if (status) {
        sql += " AND status=1 ";
    }/*from  w w  w.  j  a v  a2 s .c o m*/
    return jdbcTemplate.query(sql, new RowMapper<Sales>() {

        @Override
        public Sales mapRow(ResultSet rs, int i) throws SQLException {
            Sales s = new Sales();
            s.setId(rs.getInt("sales_id"));
            s.setProductId(rs.getInt("product_id"));
            s.setSellingPrice(rs.getInt("selling_price"));
            s.setQuantity(rs.getInt("quantity"));
            s.setDiscount(rs.getBigDecimal("discount"));
            s.setTotalCost(rs.getInt("total_cost"));
            s.setSalesDate(rs.getDate("sales_date"));
            s.setPaymentMethod(rs.getString("payment_method"));
            s.setStatus(rs.getBoolean("status"));

            return s;
        }
    });
}

From source file:com.mohit.program.DAO.impl.ProductDAOImpl.java

@Override
public List<Product> getAll(boolean availability) throws SQLException, ClassNotFoundException {
    String sql = "SELECT * FROM tbl_product WHERE 1=1 ";

    if (availability) {
        sql += " AND status=1 ";
    }//  w  w w.  j a  v a2  s .  c o m

    return jdbcTemplate.query(sql, new RowMapper<Product>() {

        @Override
        public Product mapRow(ResultSet rs, int i) throws SQLException {
            Product p = new Product();
            p.setId(rs.getInt("product_id"));
            p.setProductName(rs.getString("product_name"));
            p.setCostPrice(rs.getString("cost_price"));
            p.setSellingPrice(rs.getString("selling_price"));
            p.setQuantityAvailable(rs.getInt("quantity_available"));
            p.setAddedDate(rs.getDate("added_date"));
            p.setModifiedDate(rs.getDate("modified_date"));
            p.setStatus(rs.getBoolean("status"));
            return p;
        }
    });
}

From source file:Phnbk.java

private void printOutNumber(ResultSet result) throws SQLException {
    while (result.next()) {
        System.out.print(result.getInt("number") + ", ");
    }//from  w w w .  j  a  v  a 2 s .c  o m
}

From source file:tianci.pinao.dts.dao.impl.ConfigDaoImpl.java

@Override
public Config mapRow(ResultSet rs, int index) throws SQLException {
    Config config = new Config();

    config.setId(rs.getInt("id"));
    config.setType(rs.getInt("type"));
    config.setValue(rs.getLong("value"));
    Timestamp ts = rs.getTimestamp("lastmod_time");
    if (ts != null)
        config.setLastModTime(new Date(ts.getTime()));
    config.setLastModUserid(rs.getInt("lastmod_userid"));

    return config;
}

From source file:com.expedia.seiso.domain.repo.impl.ServiceInstanceRepoImpl.java

@Override
public NodeSummary getServiceInstanceNodeSummary(@NonNull Long id) {
    val mapper = new RowMapper<NodeSummary>() {
        @Override/*from w  w  w  .j  a  v a2 s  .c  o  m*/
        public NodeSummary mapRow(ResultSet rs, int rowNum) throws SQLException {
            return new NodeSummary(rs.getInt(1), rs.getInt(2), rs.getInt(3), rs.getInt(4));
        }
    };
    return jdbcTemplate.query(NODE_SUMMARY_SQL, args(id), mapper).get(0);
}

From source file:com.karki.spring.dao.impl.FacilitatorDaoImpl.java

private Facilitator mapData(ResultSet rs) throws ClassNotFoundException, SQLException {
    Facilitator facilitator = new Facilitator();
    facilitator.setId(rs.getInt("id"));
    facilitator.setName(rs.getString("facilitator_name"));

    return facilitator;
}

From source file:org.chimi.s4s.metainfo.mysql.MysqlMetaInfoDaoTest.java

private void assertData(String id, FileMetadata metadata) throws Throwable {
    Connection conn = dataSource.getConnection();
    PreparedStatement pstmt = conn.prepareStatement("select * from FILE_METADATA where FILE_METADATA_ID = ?");
    int idValue = Integer.parseInt(id);
    pstmt.setInt(1, idValue);//from   w ww .  java2s  .  c o  m
    ResultSet rs = pstmt.executeQuery();

    assertTrue(rs.next());
    assertEquals(idValue, rs.getInt("FILE_METADATA_ID"));
    assertEquals(metadata.getServiceId(), rs.getString("SERVICE_ID"));
    assertEquals(metadata.getFileName(), rs.getString("FILE_NAME"));
    assertEquals(metadata.getLength(), rs.getLong("FILE_LENGTH"));
    assertEquals(metadata.getMimetype(), rs.getString("MIMETYPE"));
    SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
    assertEquals(format.format(metadata.getUploadTime()), format.format(rs.getTimestamp("UPLOAD_TIME")));
    assertEquals(metadata.getFileId(), rs.getString("FILE_ID"));

    rs.close();
    pstmt.close();
    conn.close();
}