// -------------------------------------------------------------------- // Very simple example SQLJ program to fetch multiple records // from database // Frank Naude - Oct 2000 // -------------------------------------------------------------------- import sqlj.runtime.*; import sqlj.runtime.ref.*; import java.sql.*; // Iterators must be delared on top - sqlj will create classes for them #sql iterator TabIter (String, String); public class SQLJTst2 { public static void main(String[] args) { Connection c = null; String URL = "jdbc:oracle:thin:@oracle10:1521:acme"; String userid = "acme"; String passwd = "acme"; try { DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); c = DriverManager.getConnection(URL, userid, passwd); System.out.println("Successfully conencted to Oracle..."); // You need to set a Context for SQLJ, otherwise - // SQLException: found null connection context DefaultContext.setDefaultContext( new DefaultContext(c)); // Fetch multiple values TabIter tabIter; #sql tabIter = { select tname, tabtype from tab }; while (true) { String dBTabName = null; String dBTabType = null; #sql { fetch :tabIter into :dBTabName, :dBTabType }; if ( tabIter.endFetch() ) break; System.out.println("Name: " + dBTabType + "." + dBTabName); } // Close the database connection #sql { commit work }; #sql { rollback work }; c.close(); System.out.println("Disconected..."); } catch (SQLException e) { e.printStackTrace(); } } }