Getting Data from a Result Set - Java JDBC

Java examples for JDBC:RowSet

Description

Getting Data from a Result Set

Demo Code


import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Main {
  public void m() {
    try {//from   ww w  . ja v  a  2  s  .co m
      Connection connection = null;
      // Create a result set containing all data from my_table
      Statement stmt = connection.createStatement();
      ResultSet rs = stmt.executeQuery("SELECT * FROM my_table");

      // Fetch each row from the result set
      while (rs.next()) {
        // Get the data from the row using the column index
        String s = rs.getString(1);

        // Get the data from the row using the column name
        s = rs.getString("col_string");
      }
    } catch (SQLException e) {
    }
  }
}

Related Tutorials