SQLJ Programs

Fetch data from a REF CURSOR

import sqlj.runtime.*; import sqlj.runtime.ref.*; import java.sql.*; public class SQLJTst6 { #sql public static iterator EmpIter (int empno, String ename); public static void main(String[] args) { Connection c = null; String URL = “jdbc:oracle:thin:@localhost:1521:orcl”; String userid = “scott”; String passwd = “tiger”; try { DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); c = DriverManager.getConnection(URL, userid, passwd); System.out.println(“Successfully conencted to Oracle…”); //…

Execute DML against a database

import sqlj.runtime.*; import sqlj.runtime.ref.*; import java.sql.*; public class SQLJTst5 { 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…

Execute a database procedure with known IN OUT arguments

import sqlj.runtime.*; import sqlj.runtime.ref.*; import java.sql.*; public class SQLJTst4 { 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…

Connect to more than one database to do work

import sqlj.runtime.*; import sqlj.runtime.ref.*; import java.sql.*; #sql context DB1Ctx; // Define conenction context #sql context DB2Ctx; public class SQLJTst3 { 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()); // Connect to multiple databases DB1Ctx dB1 = new…

Fetch multiple records from database

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());…

Select records from a database

import sqlj.runtime.*; import sqlj.runtime.ref.*; import java.sql.*; public class SQLJTst1 { public static void main(String[] args) { Connection c = null; String dbURL = “jdbc:oracle:thin:@oracle10:1521:acme”; String userid = “acme”; String passwd = “acme”; try { DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); c = DriverManager.getConnection(dbURL, userid, passwd); System.out.println(“Successfully conencted to Oracle…”); // You need to set a Context for SQLJ, otherwise…