Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click NOW to join Planet IT!
Click NOW to join Planet IT!
ITKnowledge
Search this book:
 
Search the site:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


This method attempts to find the index of the column name passed in a parameter. It returns the column index as an integer. Listing 5-26 shows how to examine the columns of a result set.

Listing 5-26: Examining the columns of a result set.

...
...
java.sql.Statement myStatement =
                                    myConnection.createStatement();
ResultSet rs = myStatement.executeQuery(“SELECT name, title, salary FROM employees”);
while (rs.next()) {
  // print the columns of the row that was retrieved
  String empName = rs.getString(“name”);
         if rs.wasNull() System.out.println(“Ooops, column “ +
                   rs.findColumn(“name”) + “ is NULL!”);
  String empTitle = rs.getString(“title”);
  if rs.wasNull() System.out.println(“Ooops, column “ +
         rs.findColumn(“title”) + “ is NULL!”);
  long empSalary = rs.getLong(“salary”);
  if rs.wasNull() System.out.println(“Ooops, column “ +
         rs.findColumn(“salary”) + “ is NULL!”);
  System.out.println(“Employee “ + empName + “ is “ +
                   empTitle + “ and earns $” + empSalary);
}
...
...

SQL cursor

Cursors are often used when programming database applications. They offer a practical way of scanning the result sets and perform positioned delete and updates.

Result Set:

String getCursorName();

String getCursorName();

When a result table is retrieved, a named cursor is created. This cursor is used when stepping through the result set rows and may be used to update or delete data pointed to by the cursor. It is called positioned update/positioned delete. JDBC supports this feature by giving the name of the SQL cursor used by a result set. Note that if positioned update/delete is not supported by the DBMS, a SQLException will be thrown.

Putting It All Together Again

Figure 5-11 summarizes all the steps we studied in this chapter. They are the essential steps for communicating with a database, sending queries and updates, and retrieving the results from Java. Listing 5-27 also summarizes this chapter.


Figure 5-11:  Overview of all the steps for communicating with a database.

Listing 5-27: Retrieving results.

// retrieving results
// the SQL statement is taken from the standard input
import java.sql.*;
import java.io.*;
class SimpleExample
{
  public static void main(String argv[])
  {
  String url = “jdbc:odbc:mysource”;
  try
  {
         Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
         DriverManager.setLogStream(java.lang.System.out);
         Connection myConnection =
         DriverManager.getConnection(url, “javauser”, “hotjava”);
         Statement myStatement = myConnection.createStatement();
         boolean resultSetIsAvailable;
         boolean moreResultsAvailable;
         int i = 0;
         int res=0;
         String sqlText = (new DataInputStream(System.in)).readLine();
         resultSetIsAvailable = myStatement.execute(sqlText);
         ResultSet rs = null;
         for (moreResultsAvailable = true; moreResultsAvailable; )
         {
                  if (resultSetIsAvailable)
                  {
                             if ((rs = myStatement.getResultSet()) != null)
                             {
                                        // we have a resultset
                                        ResultSetMetaData rsmd =
                                         rs.getMetaData();
                                        int numCols = rsmd.getColumnCount();
                                        // display column headers
                                        for (i = 1; i <= numCols; i++)
                                        {
                                                    if (i > 1) System.out.print(“,
                                                      “);
                                                     System.out.print(
                                                     rsmd.getColumnLabel(i));
                                        }
                                         System.out.println(“”);
                                        // step through the rows
                                        while (rs.next())
                                        {
                                                    // process the columns
                                                    for (i = 1; i <= numCols; i++)
                                                    {
                                                    if (i > 1)
                                                            System.out.print(“, “);
                                                         System.out.print(
                                                     rs.getString(i));
                                                    }
System.out.println(“”);
                                        }
                             }
                  }
                  else
                  {
                             if ((res = myStatement.getUpdateCount()) != -1)
                             {
                                        // we have an updatecount
                                        System.out.println(res +
                                                                             “ row(s) affected.”);
                             }
                             // else no more results
                             else
                             {
                                       moreResultsAvailable = false;
                             }
                  }if
                  (moreResultsAvailable)
                  {
                          resultSetIsAvailable =
                                             myStatement.getMoreResults();
                  }
         }
         if (rs != null) rs.close();
         myStatement.close();
         myConnection.close();
  }
  catch(java.lang.Exception ex)
  {
         ex.printStackTrace();
  }
}

Error and Warning Management

JDBC uses the exception mechanism to pass error or warning information to programs. This mechanism signals an abnormal condition that may be handled to prevent a program termination. The exception mechanism is used when a recoverable error occurs and it must be caught to correct the situation.


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.