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.oracle.tutorial.jdbc.FilteredRowSetSample.java

public static void viewTable(Connection con) throws SQLException {
    Statement stmt = null;/*www.  ja v a 2s.  c  o  m*/
    String query = "select * from COFFEE_HOUSES";
    try {
        stmt = con.createStatement();

        ResultSet rs = stmt.executeQuery(query);

        while (rs.next()) {
            System.out.println(rs.getInt("STORE_ID") + ", " + rs.getString("CITY") + ", " + rs.getInt("COFFEE")
                    + ", " + rs.getInt("MERCH") + ", " + rs.getInt("TOTAL"));
        }

    } catch (SQLException e) {
        JDBCTutorialUtilities.printSQLException(e);
    } finally {
        if (stmt != null) {
            stmt.close();
        }
    }
}

From source file:com.sql.EMail.java

/**
 * Inserts email message into email table.
 *
 * @param eml EmailMessageModel/*from  w  ww. j  a v a  2s  .  co  m*/
 * @return Integer - generated key of the email
 */
public static int InsertEmail(EmailMessageModel eml) {
    Connection conn = null;
    PreparedStatement ps = null;
    try {
        conn = DBConnection.connectToDB();
        String sql = "INSERT INTO EMail (" + "section, " + "emailFrom, " + "emailTo, " + "emailSubject, "
                + "sentDate, " + "receivedDate, " + "emailCC, " + "emailBCC, " + "emailBody, "
                + "emailBodyFileName, " + "readyToFile " + ") VALUES (" + "?, " //1
                + "?, " //2
                + "?, " //3
                + "?, " //4
                + "?, " //5
                + "?, " //6
                + "?, " //7
                + "?, " //8
                + "?, " //9
                + "?, " //10
                + "0)"; // Ready to File False
        ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
        ps.setString(1, StringUtils.left(eml.getSection(), 4));
        ps.setString(2, StringUtils.left(eml.getEmailFrom(), 200));
        ps.setString(3, eml.getEmailTo());
        ps.setString(4, eml.getEmailSubject());
        ps.setTimestamp(5, eml.getSentDate());
        ps.setTimestamp(6, eml.getReceivedDate());
        ps.setString(7, eml.getEmailCC());
        ps.setString(8, eml.getEmailBCC());
        ps.setString(9, eml.getEmailBody());
        ps.setString(10, eml.getEmailBodyFileName());
        ps.executeUpdate();
        ResultSet newRow = ps.getGeneratedKeys();
        if (newRow.next()) {
            return newRow.getInt(1);
        }
    } catch (SQLException ex) {
        ExceptionHandler.Handle(ex);
    } finally {
        DbUtils.closeQuietly(conn);
        DbUtils.closeQuietly(ps);
    }
    return 0;
}

From source file:PieChartCreate.PieChart_AWT.java

private static PieDataset createDataset(ResultSet resultSet) {
    try {//from  w ww .  j  av  a  2s .  c o m
        DefaultPieDataset dataset = new DefaultPieDataset();

        while (resultSet.next()) {
            dataset.setValue(resultSet.getString(1), new Double(resultSet.getString(2)));
        }
        return dataset;
    } catch (SQLException ex) {
        Logger.getLogger(PieChart_AWT.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

From source file:com.concursive.connect.web.modules.activity.utils.ProjectHistoryUtils.java

public static int findNextPosition(Connection db, int projectHistoryId) throws SQLException {
    int position = 0;
    PreparedStatement pst = db.prepareStatement("SELECT max(position) AS position " + "FROM project_history "
            + "WHERE history_id = ? OR top_id = ? ");
    pst.setInt(1, projectHistoryId);//  w w w .  jav a2s  .co m
    pst.setInt(2, projectHistoryId);
    ResultSet rs = pst.executeQuery();
    if (rs.next()) {
        position = rs.getInt("position");
    }
    rs.close();
    pst.close();
    return (position + 1);
}

From source file:com.concursive.connect.web.modules.activity.utils.ProjectHistoryUtils.java

public static int findNextThreadPosition(Connection db, ProjectHistory parentProjectHistory)
        throws SQLException {
    int count = 0;
    PreparedStatement pst = db/*w w  w . j a  v a 2s  .  c om*/
            .prepareStatement("SELECT count(*) AS ccount " + "FROM project_history " + "WHERE lineage LIKE ? ");
    pst.setString(1, parentProjectHistory.getLineage() + parentProjectHistory.getId() + "/%");
    ResultSet rs = pst.executeQuery();
    if (rs.next()) {
        count = rs.getInt("ccount");
    }
    rs.close();
    pst.close();
    return (parentProjectHistory.getThreadPosition() + count + 1);
}

From source file:com.concursive.connect.web.modules.activity.utils.ProjectHistoryUtils.java

public static int queryAdditionalCommentsCount(Connection db, ProjectHistory projectHistory)
        throws SQLException {
    int count = 0;
    int topId = projectHistory.getTopId();
    if (topId == -1) {
        topId = projectHistory.getId();//  ww w. j a  v a  2s . c o  m
    }
    PreparedStatement pst = db.prepareStatement("SELECT count(*) AS comment_count " + "FROM project_history "
            + "WHERE top_id = ? AND position > ? ");
    pst.setInt(1, topId);
    pst.setInt(2, projectHistory.getPosition());
    ResultSet rs = pst.executeQuery();
    if (rs.next()) {
        count = rs.getInt("comment_count");
    }
    rs.close();
    pst.close();
    return count;
}

From source file:com.firewallid.util.FISQL.java

public static void updateRowInsertIfNotExist(Connection conn, String tableName,
        Map<String, String> updateConditions, Map<String, String> fields) throws SQLException {
    /* Query *///w w  w  . j  a v a  2  s  . c  o  m
    String query = "SELECT " + Joiner.on(", ").join(updateConditions.keySet()) + " FROM " + tableName
            + " WHERE " + Joiner.on(" = ? AND ").join(updateConditions.keySet()) + " = ?";

    /* Execute */
    PreparedStatement pst = conn.prepareStatement(query);
    int i = 1;
    for (String value : updateConditions.values()) {
        pst.setString(i, value);
        i++;
    }
    ResultSet executeQuery = pst.executeQuery();
    if (executeQuery.next()) {
        /* Update */
        query = "UPDATE " + tableName + " SET " + Joiner.on(" = ?, ").join(fields.keySet()) + " = ? WHERE "
                + Joiner.on(" = ? AND ").join(updateConditions.keySet()) + " = ?";
        pst = conn.prepareStatement(query);
        i = 1;
        for (String value : fields.values()) {
            pst.setString(i, value);
            i++;
        }
        for (String value : updateConditions.values()) {
            pst.setString(i, value);
            i++;
        }
        pst.executeUpdate();
        return;
    }

    /* Row is not exists. Insert */
    query = "INSERT INTO " + tableName + " (" + Joiner.on(", ").join(fields.keySet()) + ", "
            + Joiner.on(", ").join(updateConditions.keySet()) + ") VALUES ("
            + StringUtils.repeat("?, ", fields.size() + updateConditions.size() - 1) + "?)";
    pst = conn.prepareStatement(query);
    i = 1;
    for (String value : fields.values()) {
        pst.setString(i, value);
        i++;
    }
    for (String value : updateConditions.values()) {
        pst.setString(i, value);
        i++;
    }
    pst.execute();
}

From source file:org.red5.webapps.admin.controllers.service.UserDAO.java

public static AdminUserDetails getUser(String username) {
    AdminUserDetails details = null;/*  w  w w. j av  a2s  . com*/
    Connection conn = null;
    PreparedStatement stmt = null;
    try {
        conn = UserDatabase.getConnection();
        //make a statement
        stmt = conn.prepareStatement("SELECT * FROM APPUSER WHERE username = ?");
        stmt.setString(1, username);
        ResultSet rs = stmt.executeQuery();
        if (rs.next()) {
            log.debug("User found");
            details = new AdminUserDetails();
            details.setEnabled("enabled".equals(rs.getString("enabled")));
            details.setPassword(rs.getString("password"));
            details.setUserid(rs.getInt("userid"));
            details.setUsername(rs.getString("username"));
            //
            rs.close();
            //get role            
            stmt = conn.prepareStatement("SELECT authority FROM APPROLE WHERE username = ?");
            stmt.setString(1, username);
            rs = stmt.executeQuery();
            if (rs.next()) {
                GrantedAuthority[] authorities = new GrantedAuthority[1];
                authorities[0] = new GrantedAuthorityImpl(rs.getString("authority"));
                details.setAuthorities(authorities);
                //
                //if (daoAuthenticationProvider != null) {
                //User usr = new User(username, details.getPassword(), true, true, true, true, authorities);
                //daoAuthenticationProvider.getUserCache().putUserInCache(usr);               
                //}
            }
        }
        rs.close();
    } catch (Exception e) {
        log.error("Error connecting to db", e);
    } finally {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
            }
        }
        if (conn != null) {
            UserDatabase.recycle(conn);
        }
    }
    return details;
}

From source file:com.bluepandora.therap.donatelife.jsonbuilder.DistrictJson.java

public static JSONObject getDistrictJson(ResultSet result) throws JSONException {
    JSONObject jsonObject;//from  w  w w. j  a  va2 s.c  o m
    JSONArray jsonArray = new JSONArray();
    try {

        while (result.next()) {
            jsonObject = new JSONObject();
            jsonObject.put(DIST_ID, result.getString("dist_id"));
            jsonObject.put(DIST_NAME, result.getString("dist_name"));
            jsonObject.put(DIST_BNAME, result.getString("dist_bname"));
            jsonArray.put(jsonObject);
        }
        jsonObject = new JSONObject();
        jsonObject.put(DISTRICT, jsonArray);
        jsonObject.put(DONE, 1);

    } catch (SQLException error) {

        Debug.debugLog("DISTRICT JSON ERROR: ", error);
        jsonObject = new JSONObject();
        jsonObject.put(DONE, 0);
    }
    return jsonObject;

}

From source file:com.bluepandora.therap.donatelife.jsonbuilder.HospitalJson.java

public static JSONObject getHospitalJson(ResultSet result) throws JSONException {
    JSONObject jsonObject;/*from w ww  . j a v a  2 s  . co  m*/
    JSONArray jsonArray = new JSONArray();
    try {

        while (result.next()) {
            jsonObject = new JSONObject();
            jsonObject.put(DIST_ID, result.getString("dist_id"));
            jsonObject.put(HOSP_ID, result.getString("hospital_id"));
            jsonObject.put(HOSP_NAME, result.getString("hospital_name"));
            jsonObject.put(HOSP_BNAME, result.getString("hospital_bname"));
            jsonArray.put(jsonObject);
        }
        jsonObject = new JSONObject();
        jsonObject.put(HOSPITAL, jsonArray);
        jsonObject.put(DONE, 1);

    } catch (SQLException error) {

        Debug.debugLog("HOSPITAL JSON ERROR: ", error);
        jsonObject = new JSONObject();
        jsonObject.put(DONE, 0);
    }
    return jsonObject;
}