Showing posts with label jdbc. Show all posts
Showing posts with label jdbc. Show all posts

Monday, February 14, 2011

Java SQL basics 2 : Working with ResultSet Objects in JDBC.


IF you come here directly- you are suggested to read the article http://codingbasics.blogspot.com/2011/02/java-sql-basics-1-processing-sql.html

Updating Rows in ResultSet Objects

You cannot update a default ResultSet object, and you can only move its cursor forward.
However, you can create ResultSet objects that can be scrolled (the cursor can move backwards or move to an absolute position) and updated.

The following method, multiplies the PRICE column of each row by the argument percentage:

  public void modifyPrices(float percentage) throws SQLException {
    Statement stmt = null;
    try {
      stmt = con.createStatement();
      stmt = con.createStatement(
        ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
      ResultSet uprs = stmt.executeQuery(
        "SELECT * FROM " + dbName + ".COFFEES");


      while (uprs.next()) {
        float f = uprs.getFloat("PRICE");
        uprs.updateFloat("PRICE", f * percentage);
        uprs.updateRow();
      }


    } catch (SQLException e ) {
      JDBCTutorialUtilities.printSQLException(e);
    } finally {
      stmt.close();
    }
  }
The field ResultSet.TYPE_SCROLL_SENSITIVE creates a ResultSet object whose cursor can move both forward and backward relative to the current position
 and to an absolute position. The field ResultSet.CONCUR_UPDATABLE creates a ResultSet object that can be updated.

The method ResultSet.updateFloat updates the specified column (in this example, PRICE with the specified float value in the row where the cursor is positioned.
ResultSet contains various updater methods that enable you to update column values of various data types.
However, none of these updater methods modifies the database; you must call the method ResultSet.updateRow to update the database.
Learn about Batch Updates using Statement Objects

Java SQL basics 1 : Processing SQL statements with JDBC


In general, to process any SQL statement with JDBC, you follow these steps:

  • Establishing a connection.
  • Create a statement.
  • Execute the query.
  • Process the ResultSet object.
  • Close the connection.

  public static void viewTable(Connection con, String dbName) throws SQLException {
    Statement stmt = null;
    String query = "select COF_NAME, SUP_ID, PRICE, SALES, TOTAL from " + dbName + ".COFFEES";
    try {
      stmt = con.createStatement();
      ResultSet rs = stmt.executeQuery(query);
      while (rs.next()) {
        String coffeeName = rs.getString("COF_NAME");
        int supplierID = rs.getInt("SUP_ID");
        float price = rs.getFloat("PRICE");
        int sales = rs.getInt("SALES");
        int total = rs.getInt("TOTAL");
        System.out.println(coffeeName + "\t" + supplierID + "\t" + price + "\t" + sales + "\t" + total);
      }
    } catch (SQLException e ) {
      JDBCTutorialUtilities.printSQLException(e);
    } finally {
      stmt.close();
    }


Lets break each step and understand the code

subversion video