Java Examples

Reading a File

This example assumes a text file called dblist.txt exists in /u01/dba/java/source...

import java.io.*;

class filereadtest {

  public static void main (String args []) throws IOException

  {

     String file_name = "/u01/dba/java/source/dblist.txt";

     BufferedReader bufferedReader = new BufferedReader(new FileReader(file_name));

     String line = "x";

     while (line != null)

     {

       while ((line = bufferedReader.readLine()) != null)

       {

         System.out.println(line);

       }

     }

  }

}

Check for Empty Set

Scenario... Java code that runs SQL that should return a single row result set. If the SQL fails (due to something like a missing column) then run alternate SQL that uses a different query to populate the single row result set. If the SQL returns an empty set then force an exception so that the alternate SQL is run.

import java.sql.*class OraVersion{   public static void main (String args []) throws SQLException, IOException   {      DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());      Connection conn = DriverManager.getConnection (fileline);      Statement stmt = conn.createStatement();      String rversion = "";
      try      {         \\ 12c Syntax         ResultSet rset = stmt.executeQuery("SELECT r.version||'.'||r.bundle_id AS version \n"+                                            "FROM   sys.registry$sqlpatch r \n"+                                            "WHERE  action = 'APPLY' \n"+                                            "AND    flags != 'NJJ' \n"+                                            "AND    action_time = (SELECT MAX(action_time) \n"+                                            "                      FROM  sys.registry$sqlpatch \n"+                                            "                      WHERE action = 'APPLY' \n"+                                            "                      AND   flags != 'NJJ')");         while (rset.next())         {            rversion = rset.getString("VERSION");         }       }      catch(SQLException se1)       {         try         {            // 11.2 Syntax            ResultSet rset = stmt.executeQuery("SELECT SUBSTR(r.comments,5) AS version \n"+                                               "FROM   sys.registry$history r, \n"+                                               "WHERE  r.action = 'APPLY' \n"+                                               "AND    r.action_time = (SELECT MAX(action_time) \n"+                                               "                        FROM   sys.registry$history \n"+                                               "                        WHERE  action = 'APPLY')");            while (rset.next())            {               rversion = rset.getString("VERSION");            }            // This check forces us of 11.1 syntax if 11.2 query returns empty set            if (rhostnam.isEmpty())            {                throw new SQLException("No result");            }         }         catch(SQLException se2)         {            try            {               // 11.1 syntax               ResultSet rset = stmt.executeQuery("SELECT i.version AS version, \n"+                                                  "FROM   v$instance i");               while (rset.next())               {                  rversion = rset.getString("VERSION");               }            }            catch(SQLException se3)            {               System.out.println("ERROR: unable to retrieve version");            }            }         }      finally      {         if (stmt!=null)            stmt.close();      }   }}

Bibliography