Wednesday, 30 March 2016

How can you retrieve data from the ResultSet ?


// Load the driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

// Get the connection
Connection connection = DriverManager.getConnection(
                         “jdbc:odbc:myConn", “Shaan", "hello123");

// Create JDBC statements
Statement stmt = connection.createStatement();

// Retrieve data from the ResultSet
ResultSet rs = stmt.executeQuery("SELECT COF_NAME, PRICE FROM COFFEES");

while (rs.next()) {
  // use column name to retrieve data from column
  String name = rs.getString("COLUMN_1");
  float salary = rs.getFloat("COLUMN_2");
  // OR
  // use column index [ starts with 1 ]
  String name = rs.getString(1);
  float salary = rs.getFloat(2);
}

No comments:

Post a Comment

Note: only a member of this blog may post a comment.