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.khartec.waltz.jobs.InvolvementHarness.java

private static void viaJdbc(DataSource dataSource) {

    try (Connection conn = dataSource.getConnection();) {

        System.out.println("-- jdbc start");
        long start = System.currentTimeMillis();

        PreparedStatement pstmt = conn.prepareStatement(qry);
        ResultSet rs = pstmt.executeQuery();

        int c = 0;

        while (rs.next()) {
            c++;/* www .j a v  a  2 s.c  o m*/
        }

        System.out.println(c);
        long duration = System.currentTimeMillis() - start;
        System.out.println("-- jdbc end " + duration);
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

From source file:com.ec2box.manage.db.UserThemeDB.java

/**
 * get user theme//from  w w w .j a va  2 s . com
 *
 * @param userId object
 * @return user theme object
 */
public static UserSettings getTheme(Long userId) {

    UserSettings theme = null;
    Connection con = null;
    try {
        con = DBUtils.getConn();

        PreparedStatement stmt = con.prepareStatement("select * from user_theme where user_id=?");
        stmt.setLong(1, userId);
        ResultSet rs = stmt.executeQuery();
        if (rs.next()) {
            theme = new UserSettings();
            theme.setBg(rs.getString("bg"));
            theme.setFg(rs.getString("fg"));
            if (StringUtils.isNotEmpty(rs.getString("d1"))) {
                String[] colors = new String[16];
                colors[0] = rs.getString("d1");
                colors[1] = rs.getString("d2");
                colors[2] = rs.getString("d3");
                colors[3] = rs.getString("d4");
                colors[4] = rs.getString("d5");
                colors[5] = rs.getString("d6");
                colors[6] = rs.getString("d7");
                colors[7] = rs.getString("d8");
                colors[8] = rs.getString("b1");
                colors[9] = rs.getString("b2");
                colors[10] = rs.getString("b3");
                colors[11] = rs.getString("b4");
                colors[12] = rs.getString("b5");
                colors[13] = rs.getString("b6");
                colors[14] = rs.getString("b7");
                colors[15] = rs.getString("b8");
                theme.setColors(colors);
            }
        }
        DBUtils.closeRs(rs);
        DBUtils.closeStmt(stmt);

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        DBUtils.closeConn(con);
    }

    return theme;

}

From source file:com.oracle.tutorial.jdbc.DatalinkSample.java

public static void viewTable(Connection con, Proxy proxy) throws SQLException, IOException {
    Statement stmt = null;/*  w ww  . j  ava2  s . co  m*/
    String query = "SELECT document_name, url FROM data_repository";

    try {
        stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(query);

        if (rs.next()) {
            String documentName = null;
            java.net.URL url = null;

            documentName = rs.getString(1);

            // Retrieve the value as a URL object.
            url = rs.getURL(2);

            if (url != null) {

                // Retrieve the contents from the URL.
                URLConnection myURLConnection = url.openConnection(proxy);
                BufferedReader bReader = new BufferedReader(
                        new InputStreamReader(myURLConnection.getInputStream()));

                System.out.println("Document name: " + documentName);

                String pageContent = null;

                while ((pageContent = bReader.readLine()) != null) {
                    // Print the URL contents
                    System.out.println(pageContent);
                }
            } else {
                System.out.println("URL is null");
            }
        }
    } catch (SQLException e) {
        JDBCTutorialUtilities.printSQLException(e);
    } catch (IOException ioEx) {
        System.out.println("IOException caught: " + ioEx.toString());
    } catch (Exception ex) {
        System.out.println("Unexpected exception");
        ex.printStackTrace();
    } finally {
        if (stmt != null) {
            stmt.close();
        }
    }
}

From source file:com.oracle.tutorial.jdbc.SuppliersTable.java

public static void viewTable(Connection con) throws SQLException {
    Statement stmt = null;//from w w  w  .  j  a v  a  2 s.  co m
    String query = "select SUP_ID, SUP_NAME, STREET, CITY, STATE, ZIP from SUPPLIERS";
    try {
        stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(query);
        while (rs.next()) {
            int supplierID = rs.getInt("SUP_ID");
            String supplierName = rs.getString("SUP_NAME");
            String street = rs.getString("STREET");
            String city = rs.getString("CITY");
            String state = rs.getString("STATE");
            String zip = rs.getString("ZIP");
            System.out.println(
                    supplierName + "(" + supplierID + "): " + street + ", " + city + ", " + state + ", " + zip);
        }
    } catch (SQLException e) {
        JDBCTutorialUtilities.printSQLException(e);
    } finally {
        if (stmt != null) {
            stmt.close();
        }
    }
}

From source file:com.keybox.manage.db.UserThemeDB.java

/**
 * get user theme/*from ww w  .  j  av  a2 s .  com*/
 *
 * @param userId object
 * @return user theme object
 */
public static UserSettings getTheme(Long userId) {

    UserSettings theme = null;
    Connection con = null;
    try {
        con = DBUtils.getConn();

        PreparedStatement stmt = con.prepareStatement("select * from user_theme where user_id=?");
        stmt.setLong(1, userId);
        ResultSet rs = stmt.executeQuery();
        if (rs.next()) {
            theme = new UserSettings();
            theme.setBg(rs.getString("bg"));
            theme.setFg(rs.getString("fg"));
            if (StringUtils.isNotEmpty(rs.getString("d1"))) {
                String[] colors = new String[16];
                colors[0] = rs.getString("d1");
                colors[1] = rs.getString("d2");
                colors[2] = rs.getString("d3");
                colors[3] = rs.getString("d4");
                colors[4] = rs.getString("d5");
                colors[5] = rs.getString("d6");
                colors[6] = rs.getString("d7");
                colors[7] = rs.getString("d8");
                colors[8] = rs.getString("b1");
                colors[9] = rs.getString("b2");
                colors[10] = rs.getString("b3");
                colors[11] = rs.getString("b4");
                colors[12] = rs.getString("b5");
                colors[13] = rs.getString("b6");
                colors[14] = rs.getString("b7");
                colors[15] = rs.getString("b8");
                theme.setColors(colors);
            }
        }
        DBUtils.closeRs(rs);
        DBUtils.closeStmt(stmt);

    } catch (Exception e) {
        log.error(e.toString(), e);
    }
    DBUtils.closeConn(con);

    return theme;

}

From source file:br.ufmt.ic.pawii.util.Estados.java

public static String getEstados(String estado, String nome, String email) {
    try {//from  ww w  .  ja  va  2 s.  c  o m

        String strCidades;

        Statement stm;

        Connection con = ConnBD.getConnection();

        if (con == null) {
            throw new SQLException("Erro conectando");
        }

        stm = con.createStatement();

        String sql = "SELECT codigo,municipio FROM municipios" + " WHERE uf='" + estado
                + "' ORDER BY municipio ASC";

        ResultSet rs = stm.executeQuery(sql);

        JSONArray cidades = new JSONArray();

        while (rs.next()) {

            String codigo = rs.getString(1);
            String cidade = rs.getString(2);

            JSONObject jsonCidade = new JSONObject();
            jsonCidade.put("codigo", codigo);
            jsonCidade.put("nome", cidade);
            cidades.put(jsonCidade);
        }

        JSONObject jsonRetorno = new JSONObject();
        jsonRetorno.put("cidades", cidades);
        jsonRetorno.put("seuNome", nome);
        jsonRetorno.put("seuEmail", email);

        strCidades = jsonRetorno.toString();

        return strCidades;

    } catch (SQLException ex) {
        System.out.println("Error: " + ex.getMessage());
    } catch (Exception e) {
        System.out.println("Error: " + e.getMessage());
    }

    return null;
}

From source file:eu.databata.engine.util.PropagatorTableExport.java

private static void exportTablesData(JdbcTemplate jdbcTemplate, String delimiter, String tableNamePattern) {
    try {/*from  w ww  .  ja v  a 2 s .  co  m*/
        System.out.println("Trying to load tables from metadata");
        Connection connection = jdbcTemplate.getDataSource().getConnection();
        ResultSet tables = connection.getMetaData().getTables(null, null, tableNamePattern,
                new String[] { "TABLE" });
        while (tables.next()) {
            System.out.println("Exporting data from " + tables.getString("TABLE_NAME"));
            exportData(jdbcTemplate, tables.getString("TABLE_NAME"), delimiter);
        }
    } catch (SQLException e) {
        System.out.println("\nError when trying to get table names from DB.");
    }
}

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

public static JSONObject getUserProfileJson(ResultSet result) throws JSONException {
    JSONArray jsonArray = new JSONArray();
    JSONObject jsonObject;// www  .ja  va 2 s  .  c  om

    try {
        while (result.next()) {
            jsonObject = new JSONObject();
            jsonObject.put(FIRST_NAME, result.getString("first_name"));
            jsonObject.put(LAST_NAME, result.getString("last_name"));
            jsonObject.put(GROUP_ID, result.getString("group_id"));
            jsonObject.put(DIST_ID, result.getString("dist_id"));
            jsonArray.put(jsonObject);
        }
        if (jsonArray.length() != 0) {
            jsonObject = new JSONObject();
            jsonObject.put(PROFILE, jsonArray);
            jsonObject.put(DONE, 1);
        } else {
            jsonObject = new JSONObject();
            jsonObject.put("message", Enum.MESSAGE_INVALID_USER);
            jsonObject.put(DONE, 0);
        }
    } catch (SQLException error) {
        Debug.debugLog("USER PROFILE RESULT DATA : ", error);
        jsonObject = new JSONObject();
        jsonObject.put(DONE, 0);
    }
    return jsonObject;
}

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

public static JSONObject getJsonBloodGroup(ResultSet result) throws JSONException {

    JSONArray jsonArray = new JSONArray();
    JSONObject jsonObject;/*  ww w .java 2  s. c om*/

    try {

        while (result.next()) {
            jsonObject = new JSONObject();
            jsonObject.put(GROUP_ID, result.getString("group_id"));
            jsonObject.put(GROUP_NAME, result.getString("group_name"));
            jsonObject.put(GROUP_BNAME, result.getString("group_bname"));
            jsonArray.put(jsonObject);
        }

        jsonObject = new JSONObject();
        jsonObject.put(BLOOD_GROUP, jsonArray);
        jsonObject.put(DONE, 1);

    } catch (SQLException error) {
        Debug.debugLog("BLOOD_GROUP RESULT SET: ", error);
        jsonObject = new JSONObject();
        jsonObject.put(DONE, 0);

    }
    return jsonObject;
}

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

public static JSONObject getBloodRequestJson(ResultSet result) throws JSONException {
    JSONArray jsonArray = new JSONArray();
    JSONObject jsonObject;//w ww. j a v  a 2 s.c  o m

    try {
        while (result.next()) {
            jsonObject = new JSONObject();
            jsonObject.put(MOBILE_NUMBER, result.getString("mobile_number"));
            jsonObject.put(REQ_TIME, result.getString("req_time"));
            jsonObject.put(GROUP_ID, result.getString("group_id"));
            jsonObject.put(AMOUNT, result.getString("amount"));
            jsonObject.put(HOSP_ID, result.getString("hospital_id"));
            jsonObject.put(EMERGENCY, result.getString("emergency"));

            jsonArray.put(jsonObject);
        }
        jsonObject = new JSONObject();
        jsonObject.put(BLOOD_REQUEST, jsonArray);
        jsonObject.put(DONE, 1);

    } catch (SQLException error) {
        Debug.debugLog("BLOOD_GROUP RESULT SET: ", error);
        jsonObject = new JSONObject();
        jsonObject.put(DONE, 0);
    }
    return jsonObject;
}