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.github.mgramin.liwide_rest.util.Convertor.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));
        }/*  w  ww  . j  av  a 2s  .  com*/
        jsonArray.put(obj);
    }
    return jsonArray;
}

From source file:org.works.integration.storedproc.derby.DerbyStoredProcedures.java

public static void findCoffee(int coffeeId, String[] coffeeDescription) throws SQLException {
    Connection connection = null;
    PreparedStatement statement = null;

    try {//from  ww  w  .ja va 2s  . com
        connection = DriverManager.getConnection("jdbc:default:connection");
        String sql = "SELECT * FROM COFFEE_BEVERAGES WHERE ID = ? ";
        statement = connection.prepareStatement(sql);
        statement.setLong(1, coffeeId);

        ResultSet resultset = statement.executeQuery();
        resultset.next();
        coffeeDescription[0] = resultset.getString("COFFEE_DESCRIPTION");

    } finally {
        JdbcUtils.closeStatement(statement);
        JdbcUtils.closeConnection(connection);
    }

}

From source file:TestDB.java

 /**
 * Runs a test by creating a table, adding a value, showing the table contents, and removing the
 * table./*from  www  . ja  v  a 2 s  .  c  o  m*/
 */
public static void runTest() throws SQLException, IOException
{
   Connection conn = getConnection();
   try
   {
      Statement stat = conn.createStatement();

      stat.executeUpdate("CREATE TABLE Greetings (Message CHAR(20))");
      stat.executeUpdate("INSERT INTO Greetings VALUES ('Hello, World!')");

      ResultSet result = stat.executeQuery("SELECT * FROM Greetings");
      if (result.next())
         System.out.println(result.getString(1));
      result.close();
      stat.executeUpdate("DROP TABLE Greetings");
   }
   finally
   {
      conn.close();
   }
}

From source file:common.utility.ChartHelper.java

public static CategoryDataset createDataset() throws SQLException {

    // create the dataset...
    final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    ResultSet rs = new PersonDetailsDAO().selectAllPersonofArea();
    while (rs.next()) {
        dataset.setValue(rs.getDouble("NumOfCitizens"), "Area Code : " + rs.getString("AreaCode"), "Areas");
    }// ww  w.ja v a2 s.com

    return dataset;

}

From source file:de.thejeterlp.bukkit.login.SQLAccount.java

protected static Account convert(UUID uuid) throws SQLException {
    checkReflection();//from w  w  w . j  av a 2  s.c om
    Validate.notNull(uuid, "uuid cannot be null!");
    PreparedStatement st = Login.getInstance().getDB()
            .getPreparedStatement("SELECT * FROM `" + Statics.USER_TABLE + "` WHERE `uuid` = ? LIMIT 1;");
    st.setString(1, uuid.toString());
    ResultSet rs = st.executeQuery();
    while (rs.next()) {
        int id = rs.getInt("id");
        Login.getInstance().getDB().closeResultSet(rs);
        Login.getInstance().getDB().closeStatement(st);
        PreparedStatement sta = Login.getInstance().getDB().getPreparedStatement(
                "SELECT * FROM `" + Statics.PASSWORD_TABLE + "` WHERE `userID` = ? LIMIT 1;");
        sta.setInt(1, id);
        ResultSet rset = sta.executeQuery();
        while (rset.next()) {
            String hash = rset.getString("password");
            Login.getInstance().getDB().closeResultSet(rset);
            Login.getInstance().getDB().closeStatement(sta);
            return new Account(id, uuid, hash);
        }
    }
    return null;
}

From source file:Main.java

private static List<Person> findAll(Connection conn) throws SQLException {
    List<Person> rows = new ArrayList<Person>();
    Statement stat = conn.createStatement();
    ResultSet rs = stat.executeQuery("select * from people;");
    while (rs.next()) {
        rows.add(new Person(rs.getString("name"), rs.getString("occupation")));
    }/* w  ww  .j  a v  a  2s.co m*/
    close(stat);
    close(rs);
    return rows;
}

From source file:com.afforess.nsdump.Test.java

public static void testNationDump() throws IOException, SQLException {
    NationsDump dump = new NationsDump();
    dump.parse();/*from w  w w .ja  va 2s .  co m*/

    Connection conn = dump.getDatabaseConnection();
    PreparedStatement statement = conn.prepareStatement("SELECT (name) FROM nations");
    ResultSet result = statement.executeQuery();
    int total = 0;
    while (result.next()) {
        total++;
    }
    result.close();

    System.out.println("Total nations: " + total);

    statement = conn.prepareStatement("SELECT * FROM nations WHERE name = 'sakhovelo'");
    result = statement.executeQuery();
    result.next();
    for (int i = 1; i <= 10; i++) {
        if (i == 4) {
            Clob clob = result.getClob(i);
            String motto = clob.getSubString(1, (int) clob.length());
            String mottoEscaped = StringEscapeUtils.unescapeHtml(motto);
            System.out.println("Raw: " + motto + " Escaped: " + mottoEscaped);
        } else {
            System.out.println(result.getString(i));
        }
    }

    File db = new File("./ns-db.h2.db");
    db.delete();
}

From source file:com.kaidad.utilities.MBTilesBase64Converter.java

private static void encodeTileData(final JdbcTemplate c) {
    c.query("SELECT tile_id, tile_data FROM images", new ResultSetExtractor<Object>() {
        @Override/*from ww w  .ja  va  2s .  c  om*/
        public Object extractData(ResultSet rs) throws SQLException, DataAccessException {
            while (rs.next()) {
                String tileId = rs.getString("tile_id");
                byte[] imageData = rs.getBytes("tile_data");
                addTileDataToStringTable(c, tileId, BaseEncoding.base64().encode(imageData));
            }
            System.out.println("Operation done successfully");
            return null;
        }
    });
}

From source file:com.afforess.nsdump.Test.java

public static void testRegionDump() throws IOException, SQLException {
    RegionsDump dump = new RegionsDump();
    dump.parse();/*from  w  w w .j a  va  2  s.  co  m*/

    Connection conn = dump.getDatabaseConnection();
    PreparedStatement statement = conn.prepareStatement("SELECT (numnations) FROM regions");
    ResultSet result = statement.executeQuery();
    int total = 0, regions = 0;
    while (result.next()) {
        total += result.getInt(1);
        regions++;
    }
    System.out.println("Total nations: " + total);
    System.out.println("Total regions: " + regions);
    result.close();
    conn.close();

    File db = new File("./ns-db.h2.db");
    db.delete();
}

From source file:com.dangdang.ddframe.rdb.sharding.example.jdbc.Main.java

private static void printGroupBy(final DataSource dataSource) throws SQLException {
    String sql = "SELECT o.user_id, COUNT(*) FROM t_order o JOIN t_order_item i ON o.order_id=i.order_id GROUP BY o.user_id";
    try (Connection conn = dataSource.getConnection();
            PreparedStatement preparedStatement = conn.prepareStatement(sql)) {
        ResultSet rs = preparedStatement.executeQuery();
        while (rs.next()) {
            System.out.println("user_id: " + rs.getInt(1) + ", count: " + rs.getInt(2));
        }//from w  w  w  .  ja v  a 2s .c om
    }
}