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.us.test.H2Helper.java

public static void main2(String... args) throws Exception {
    createOrUpdate("jdbc:h2:~/data/iph2db", "sa", "sa");
    laodData2H2Db(new File("C:\\Documents and Settings\\Administrator\\data\\ips.txt"), "jdbc:h2:~/data/iph2db",
            "sa", "sa");
    JdbcConnectionPool cp = JdbcConnectionPool.create("jdbc:h2:~/data/iph2db", "sa", "sa");
    Connection conn = cp.getConnection();
    ResultSet rs = conn.createStatement().executeQuery("select * from ips");
    while (rs.next()) {
        System.out.println(//www.  j  a  va  2 s. c  om
                rs.getString(1) + ":" + rs.getString(2) + ":" + rs.getString(3) + ":" + rs.getString(4));
    }

}

From source file:com.mum.edu.cs472.dictServlet.java

public static JSONArray convertToJSON(ResultSet resultSet) throws Exception {
    JSONArray jsonArray = new JSONArray();
    while (resultSet.next()) {
        int total_rows = resultSet.getMetaData().getColumnCount();
        JSONObject obj = new JSONObject();
        for (int i = 0; i < total_rows; i++) {
            obj.put(resultSet.getMetaData().getColumnLabel(i + 1).toLowerCase(), resultSet.getObject(i + 1));
            jsonArray.add(obj);//ww w . j  a v a  2s.  c  o m
        }
    }
    return jsonArray;
}

From source file:Main.java

public static int storedProcWithResultSet() throws Exception {
    Connection conn = null;//from   w  ww  . j ava2s. c  o  m
    CallableStatement cs = conn.prepareCall("{? = call proc (?,?,?,?,?,?,?)}");

    // register input parameters
    cs.setString(2, "");
    cs.setString(3, "");
    cs.setString(4, "123");
    // regsiter ouput parameters
    cs.registerOutParameter(5, java.sql.Types.CHAR);
    cs.registerOutParameter(6, java.sql.Types.CHAR);
    cs.registerOutParameter(7, java.sql.Types.CHAR);

    // Procedure execution
    ResultSet rs = cs.executeQuery();

    ResultSetMetaData rsmd = rs.getMetaData();
    int nbCol = rsmd.getColumnCount();
    while (rs.next()) {
        for (int i = 1; i <= nbCol; i++) {
            System.out.println(rs.getString(i));
            System.out.println(rs.getString(i));
        }
    }
    // OUTPUT parameters
    System.out.println("return code of Stored procedure = : " + cs.getInt(1));
    for (int i = 5; i <= 7; i++)
        System.out.println("parameter " + i + " : " + cs.getString(i));
    return cs.getInt(1);
}

From source file:com.splicemachine.derby.test.framework.SpliceRoleWatcher.java

public static void executeDrop(String roleName) {
    LOG.trace("ExecuteDrop");
    Connection connection = null;
    PreparedStatement statement = null;
    try {//from   ww w  .  j av a  2s  . c o m
        connection = SpliceNetConnection.getConnection();
        statement = connection.prepareStatement("select roleid from sys.sysroles where roleid = ?");
        statement.setString(1, roleName);
        ResultSet rs = statement.executeQuery();
        if (rs.next())
            connection.createStatement().execute(String.format("drop role %s", roleName));
        connection.commit();
    } catch (Exception e) {
        LOG.error("error Dropping " + e.getMessage());
        e.printStackTrace();
        throw new RuntimeException(e);
    } finally {
        DbUtils.closeQuietly(statement);
        DbUtils.commitAndCloseQuietly(connection);
    }
}

From source file:com.buddycloud.friendfinder.HashUtils.java

public static String retrieveJid(String hash, ComponentDataSource dataSource) {
    PreparedStatement statement = null;
    try {/*from  w ww  .  j  a va2 s  .c  om*/
        statement = dataSource
                .prepareStatement("SELECT \"jid\" FROM \"contact-matches\" WHERE \"credential-hash\"=?", hash);
        ResultSet resultSet = statement.executeQuery();
        if (resultSet.next()) {
            return resultSet.getString(1);
        }
        return null;
    } catch (SQLException e) {
        LOGGER.error("Could not retrieve jid from hash " + hash, e);
        throw new RuntimeException(e);
    } finally {
        ComponentDataSource.close(statement);
    }
}

From source file:Main.java

public static String QueryVcTable2(Statement stmt, String app_name) {
    String sql_1 = "SELECT app_versioncode FROM app_info.`msp_table_8.12_copy` WHERE app_name='" + app_name
            + "'";
    ResultSet result = null;
    String v = null;/*ww w  . j ava 2 s  . co m*/
    try {
        result = stmt.executeQuery(sql_1);
        while (result.next()) {
            v = result.getString(1);
        }
        if (v != null)
            return v;
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.pinterest.deployservice.db.SingleResultSetHandlerFactory.java

public static <T> ResultSetHandler<List<T>> newListObjectHandler() {
    return new ResultSetHandler<List<T>>() {
        @Override/*from   w ww.jav a 2  s  .  co  m*/
        public List<T> handle(ResultSet resultSet) throws SQLException {
            List<T> ret = new ArrayList<T>();
            while (resultSet.next()) {
                ret.add((T) resultSet.getObject(1));
            }
            return ret;
        }
    };
}

From source file:org.jw.service.factory.StatisticsChartFactory.java

public static CategoryDataset createCategoryDataset(Connection conn, String query) throws SQLException {
    DefaultCategoryDataset dataset = new DefaultCategoryDataset();

    Statement statement = conn.createStatement();
    ResultSet resultSet = statement.executeQuery(query);

    while (resultSet.next()) {
        Double value = resultSet.getDouble("value");
        String series = resultSet.getString("series");
        String category = resultSet.getString("category");
        dataset.addValue(value, series, category);
    }//from  w  w  w . j  av a  2 s  .  co  m

    return dataset;
}

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

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

    /* Execute */
    PreparedStatement pst = conn.prepareStatement(query);
    int i = 1;
    for (String value : fields.values()) {
        pst.setString(i, value);
        i++;
    }
    ResultSet executeQuery = pst.executeQuery();
    if (executeQuery.next()) {
        return executeQuery.getString(field);
    }

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

From source file:Student.java

  public static void checkData() throws Exception {
  Class.forName("org.hsqldb.jdbcDriver");
  Connection conn = DriverManager.getConnection("jdbc:hsqldb:data/tutorial", "sa", "");
  Statement st = conn.createStatement();

  ResultSet mrs = conn.getMetaData().getTables(null, null, null, new String[] { "TABLE" });
  while (mrs.next()) {
    String tableName = mrs.getString(3);
    System.out.println("\n\n\n\nTable Name: "+ tableName);

    ResultSet rs = st.executeQuery("select * from " + tableName);
    ResultSetMetaData metadata = rs.getMetaData();
    while (rs.next()) {
      System.out.println(" Row:");
      for (int i = 0; i < metadata.getColumnCount(); i++) {
        System.out.println("    Column Name: "+ metadata.getColumnLabel(i + 1)+ ",  ");
        System.out.println("    Column Type: "+ metadata.getColumnTypeName(i + 1)+ ":  ");
        Object value = rs.getObject(i + 1);
        System.out.println("    Column Value: "+value+"\n");
      }//from  www  . j  av a2  s .  c  o m
    }
  }
}