VMS Fun 2 - SQLJ

Content:

  1. What is SQLJ?
  2. Syntax
  3. Process
  4. Example
  5. SQLJ
  6. Client
  7. Test
  8. The future?

What is SQLJ?

Remember back in the late 80's and 90's - *the* way to access a database was using embedded SQL in Cobol or PL/I or C or whatever.

That way to access a database is getting rare today. Only a few databases ship with pre-compilers (including Oracle DB, IBM DB2, PostgreSQL and Mimer). And even those have reduced the number of programming languages they support.

But there is one exception unknown to most.

Back when Java was young in the late 1990's some big Java and database players (including IBM and Oracle) defined a standard for embedded SQL in Java. And it actually got ANSI/ISO standardized in SQL 99.

That standard is commonly known as SQLJ.

SQLJ never became a hit. People started using plain JDBC. And when people needed something more advanced they switched to ORM (entity EJB's, Hibernate, JPA etc.).

But the SQLJ software still exists and work fine today.

It works with any database that has a JDBC driver (which is almost all databases).

And even though the code with the embedded SQL has to be Java, then the result is usable from any JVM language (Java, Groovy, Kotlin, Scala, Jython etc.).

And it runs fine on VMS. VMS Alpha, VMS Itanium and VMS x86-64. Often VMS Alpha is a problem because latest Java is version 5 from 2004, but not a problem for SQLJ since it is even older than that!

Syntax:

The SQLj syntax deviate a little bit from Cobol/C/whatever embedded SQL syntax.

#sql context <name of context class>;

define that the context class - it has a one arg constructor that takes a java.sql.Connection as argument.

#sql [<variable with context>] { <INSERT/UPDATE/DELETE SQL statement with embedded variables> };

execute a database update.

#sql iterator <name of iterator class>(<list with types of columns>);
...
#sql [<variable with context>] <variable with iterator> = { <SELECT SQL statement with embedded variables> };
...
#sql { fetch :<variable with iterator> INTO <list with embedded variables> };

execute a database query. The iterator class has an endFetch method to detect end of result set.

Maybe a little cryptic but examples coming up.

We can start by comparing Java with Cobol and C.

Update (INSERT):

        ...
        int f1 = 999;
        String f2 = "XXX";
        #sql [ctx] { INSERT INTO t1 VALUES(:f1,:f2) };
        ...
...
01 F1 PIC 9(9) DISPLAY.
01 F2 PIC X(50).
...
    ...
    MOVE 999 TO F1
    MOVE "XXX" TO F2
    EXEC SQL INSERT INTO t1 VALUES(:F1,:F2) END-EXEC
    ...
    ...
    EXEC SQL BEGIN DECLARE SECTION;
    char f2[51];
    long int f1;
    EXEC SQL END DECLARE SECTION;
    ...
    f1 = 999;
    strcpy(f2, "XXX");
    EXEC SQL INSERT INTO t1 VALUES(:f1, :f2);
    ...

Query (SELECT):

    ...
    #sql private static iterator T1Iterator(int, String);
    ...
        ...
        T1Iterator it;
        #sql [ctx] it = { SELECT f1,f2 FROM t1 };
        while(true) {
            int f1 = 0;
            String f2 = null;
            #sql { fetch :it INTO :f1, :f2 };
            if(it.endFetch()) break;
            ...
        }
        ...
...
EXEC SQL INCLUDE SQLCA END-EXEC.
01 F1 PIC 9(9) DISPLAY.
01 F2 PIC X(50).
EXEC SQL DECLARE curs CURSOR FOR SELECT f1,f2 FROM t1 END-EXEC.
...
    ...
    EXEC SQL OPEN curs END-EXEC
    MOVE 0 TO SQLCODE
    PERFORM UNTIL NOT SQLCODE = 0
        EXEC SQL FETCH curs INTO :f1, :f2 END-EXEC
        ...
    END-PERFORM
    EXEC SQL CLOSE curs END-EXEC
...
EXEC SQL INCLUDE SQLCA;
...
    ...
    EXEC SQL BEGIN DECLARE SECTION;
    char f2[51];
    long int f1;
    EXEC SQL END DECLARE SECTION;
    ...
    EXEC SQL DECLARE curs CURSOR FOR SELECT f1,f2 FROM t1;
    EXEC SQL OPEN curs;
    for(;;)
    {
        EXEC SQL FETCH curs INTO :f1, :f2;
        if(sqlca.sqlcode != 0) break;
        ...
    }
    EXEC SQL CLOSE curs;

Process:

The process is the same as for any other embedded SQL implementation.

Xxx.sqlj--(translator)-->Xxx.java--(javac)-->Xxx.class

The required jar files are:

These can be found in any older Oracle installation.

If newer Java (like Java 8 on Itanium or x86-64, but not Java 5 on Alpha) one also some sun io classes from older Java.

$ jar tvf sunio.jar
     0 Fri Jul 04 19:41:42 EST 2025 META-INF/
    65 Fri Jul 04 19:41:42 EST 2025 META-INF/MANIFEST.MF
     0 Fri Jul 04 19:40:46 EST 2025 sun/
     0 Fri Jul 04 19:40:10 EST 2025 sun/io/
   900 Fri Oct 09 00:45:06 EST 2009 sun/io/ByteToCharASCII.class
  2431 Fri Oct 09 00:47:38 EST 2009 sun/io/ByteToCharConverter.class
   570 Fri Oct 09 00:45:06 EST 2009 sun/io/ByteToCharCp1250.class
   570 Fri Oct 09 00:45:06 EST 2009 sun/io/ByteToCharCp1251.class
   570 Fri Oct 09 00:45:06 EST 2009 sun/io/ByteToCharCp1252.class
   570 Fri Oct 09 00:45:06 EST 2009 sun/io/ByteToCharCp1253.class
   570 Fri Oct 09 00:45:06 EST 2009 sun/io/ByteToCharCp1254.class
   570 Fri Oct 09 00:45:06 EST 2009 sun/io/ByteToCharCp1257.class
   860 Fri Oct 09 00:45:06 EST 2009 sun/io/ByteToCharCp833.class
   814 Fri Oct 09 00:45:06 EST 2009 sun/io/ByteToCharISO8859_1.class
   592 Fri Oct 09 00:45:06 EST 2009 sun/io/ByteToCharISO8859_13.class
   592 Fri Oct 09 00:45:06 EST 2009 sun/io/ByteToCharISO8859_15.class
   587 Fri Oct 09 00:45:06 EST 2009 sun/io/ByteToCharISO8859_2.class
   587 Fri Oct 09 00:45:06 EST 2009 sun/io/ByteToCharISO8859_4.class
   587 Fri Oct 09 00:45:06 EST 2009 sun/io/ByteToCharISO8859_5.class
   587 Fri Oct 09 00:45:06 EST 2009 sun/io/ByteToCharISO8859_7.class
   587 Fri Oct 09 00:45:06 EST 2009 sun/io/ByteToCharISO8859_9.class
   570 Fri Oct 09 00:45:06 EST 2009 sun/io/ByteToCharKOI8_R.class
  1226 Fri Oct 09 00:45:06 EST 2009 sun/io/ByteToCharSingleByte.class
  2271 Fri Oct 09 00:45:06 EST 2009 sun/io/ByteToCharUnicode.class
   243 Fri Oct 09 00:45:06 EST 2009 sun/io/ByteToCharUnicodeBig.class
   259 Fri Oct 09 00:45:06 EST 2009 sun/io/ByteToCharUnicodeBigUnmarked.class
   249 Fri Oct 09 00:45:06 EST 2009 sun/io/ByteToCharUnicodeLittle.class
   265 Fri Oct 09 00:45:06 EST 2009 sun/io/ByteToCharUnicodeLittleUnmarked.class
   332 Fri Oct 09 00:45:06 EST 2009 sun/io/ByteToCharUTF16.class
  2216 Fri Oct 09 00:45:06 EST 2009 sun/io/ByteToCharUTF8.class
   785 Fri Oct 09 00:47:42 EST 2009 sun/io/CharacterEncoding$1.class
   713 Fri Oct 09 00:47:42 EST 2009 sun/io/CharacterEncoding$2.class
 19599 Fri Oct 09 00:47:42 EST 2009 sun/io/CharacterEncoding.class
  1825 Fri Oct 09 00:45:06 EST 2009 sun/io/CharToByteASCII.class
  3378 Fri Oct 09 00:47:36 EST 2009 sun/io/CharToByteConverter.class
   717 Fri Oct 09 00:45:06 EST 2009 sun/io/CharToByteCp1250.class
   717 Fri Oct 09 00:45:06 EST 2009 sun/io/CharToByteCp1251.class
   717 Fri Oct 09 00:45:06 EST 2009 sun/io/CharToByteCp1252.class
   717 Fri Oct 09 00:45:06 EST 2009 sun/io/CharToByteCp1253.class
   717 Fri Oct 09 00:45:06 EST 2009 sun/io/CharToByteCp1254.class
   717 Fri Oct 09 00:45:06 EST 2009 sun/io/CharToByteCp1257.class
  3243 Fri Oct 09 00:45:06 EST 2009 sun/io/CharToByteCp833.class
  1959 Fri Oct 09 00:45:06 EST 2009 sun/io/CharToByteISO8859_1.class
   739 Fri Oct 09 00:45:06 EST 2009 sun/io/CharToByteISO8859_13.class
   739 Fri Oct 09 00:45:06 EST 2009 sun/io/CharToByteISO8859_15.class
   734 Fri Oct 09 00:45:06 EST 2009 sun/io/CharToByteISO8859_2.class
   734 Fri Oct 09 00:45:06 EST 2009 sun/io/CharToByteISO8859_4.class
   734 Fri Oct 09 00:45:06 EST 2009 sun/io/CharToByteISO8859_5.class
   734 Fri Oct 09 00:45:06 EST 2009 sun/io/CharToByteISO8859_7.class
   734 Fri Oct 09 00:45:06 EST 2009 sun/io/CharToByteISO8859_9.class
   717 Fri Oct 09 00:45:06 EST 2009 sun/io/CharToByteKOI8_R.class
  2026 Fri Oct 09 00:45:06 EST 2009 sun/io/CharToByteSingleByte.class
  2182 Fri Oct 09 00:45:06 EST 2009 sun/io/CharToByteUnicode.class
   268 Fri Oct 09 00:45:06 EST 2009 sun/io/CharToByteUnicodeBig.class
   318 Fri Oct 09 00:45:06 EST 2009 sun/io/CharToByteUnicodeBigUnmarked.class
   274 Fri Oct 09 00:45:06 EST 2009 sun/io/CharToByteUnicodeLittle.class
   324 Fri Oct 09 00:45:06 EST 2009 sun/io/CharToByteUnicodeLittleUnmarked.class
   332 Fri Oct 09 00:45:06 EST 2009 sun/io/CharToByteUTF16.class
  1725 Fri Oct 09 00:45:06 EST 2009 sun/io/CharToByteUTF8.class
   424 Fri Oct 09 00:47:36 EST 2009 sun/io/ConversionBufferFullException.class
  4806 Fri Oct 09 00:47:38 EST 2009 sun/io/Converters.class
   412 Fri Oct 09 00:47:36 EST 2009 sun/io/MalformedInputException.class
   416 Fri Oct 09 00:47:36 EST 2009 sun/io/UnknownCharacterException.class

Command line to precompile:

$ java -cp translator.jar:runtime12.jar:ojdbc5.jar:sunio.jar:<db driver jar> "sqlj.tools.Sqlj" -codegen=iso -compile=false -driver="<db driver class>" -url="<db connection url>" -user="<db username>" -password="<db password>" "Xxx.sqlj"

Example:

SQLJ:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

#sql context ConCtx;

public class Test  {
    private String constr;
    private String usr;
    private String pwd;
    private ConCtx getContext() throws SQLException {
        return new ConCtx(DriverManager.getConnection(constr, usr, pwd));
    }
    public Test(String constr, String usr, String pwd) {
        this.constr = constr;
        this.usr = usr;
        this.pwd = pwd;
    }
    #sql private static iterator T1Iterator(int, String);
    public T1 getOne(int f1) throws SQLException {
        T1 res;
        ConCtx ctx = getContext();
        T1Iterator it;
        #sql [ctx] it = { SELECT f1,f2 FROM t1 WHERE f1 = :f1};
        int xf1 = 0;
        String xf2 = null;
        #sql { fetch :it INTO :xf1, :xf2 };
        if(!it.endFetch()) {
            res = new T1(xf1, xf2);
        } else {
            res = null;
        }
        ctx.close();
        return res;
    }
    public List<T1> getAll() throws SQLException {
        List<T1> res = new ArrayList<T1>();
        ConCtx ctx = getContext();
        T1Iterator it;
        #sql [ctx] it = { SELECT f1,f2 FROM t1 };
        while(true) {
            int xf1 = 0;
            String xf2 = null;
            #sql { fetch :it INTO :xf1, :xf2 };
            if(it.endFetch()) break;
            res.add(new T1(xf1, xf2));
        }
        ctx.close();
        return res;
    }
    public void save(T1 o) throws SQLException {
        ConCtx ctx = getContext();
        int f1 = o.getF1();
        String f2 = o.getF2();
        #sql [ctx] { INSERT INTO t1 VALUES(:f1,:f2) };
        ctx.close();
    }
    public void remove(int f1) throws SQLException {
        ConCtx ctx = getContext();
        #sql [ctx] { DELETE FROM t1 WHERE f1 = :f1 };
        ctx.close();
    }
}

Precompile:

P1 = JDBC driver jar name
P2 = JDBC driver class name
P3 = JDBC connection URL
P4 = username
P5 = password
$ java -cp translator.jar:runtime12.jar:ojdbc5.jar:sunio.jar:'p1' "sqlj.tools.Sqlj" -codegen=iso -compile=false -driver="''p2'" -url="''p3'" -user="''p4'" -password="''p5'" "Test.sqlj"

Client:

import java.sql.SQLException;

public class Main {
    private static void dump(Test tst) throws SQLException {
        for(T1 o : tst.getAll()) {
            System.out.println(o);
        }
    }
    public static void main(String[] args) throws Exception {
        System.out.printf("Test of Java with %s\n", args[1]);
        Class.forName(args[0]);
        Test tst = new Test(args[1], args.length > 2 ? args[2] : "", args.length > 3 ? args[3] : "");
        System.out.println(tst.getOne(2));
        dump(tst);
        tst.save(new T1(999, "XXX"));
        dump(tst);
        tst.remove(999);
        dump(tst);
    }
}

Compile and run:

P1 = JDBC driver jar name
P2 = JDBC driver class name
P3 = JDBC connection URL
P4 = username
P5 = password
$ javac -cp runtime12.jar Main.java Test.java T1.java
$ java "-Xmx512m" -cp .:translator.jar:runtime12.jar:ojdbc5.jar:sunio.jar:'p1' "Main" "''p2'" "''p3'" "''p4'" "''p5'"
def dump(tst) {
    for(o in tst.getAll()) {
        println(o)
    }
}

printf("Test of Groovy with %s\n", args[1])
Class.forName(args[0])
tst = new Test(args[1], args.length > 2 ? args[2] : "", args.length > 3 ? args[3] : "")
println(tst.getOne(2))
dump(tst)
tst.save(new T1(999, "XXX"))
dump(tst)
tst.remove(999)
dump(tst)

Compile and run:

P1 = JDBC driver jar name
P2 = JDBC driver class name
P3 = JDBC connection URL
P4 = username
P5 = password
$ javac -cp runtime12.jar Test.java T1.java
$ groovy_cp = ".:translator.jar:runtime12.jar:ojdbc5.jar:sunio.jar:''p1'"
$ groovy Main.groovy "''p2'" "''p3'" "''p4'" "''p5'"
import sys

import java.lang.Class as JavaClass

import Test
import T1

def dump(tst):
    for o in tst.getAll():
        print(o)

print("Test of Jython with %s" % (sys.argv[2]))
JavaClass.forName(sys.argv[1])
if len(sys.argv) > 3:
    tst = Test(sys.argv[2], sys.argv[3], sys.argv[4])
else:
    tst = Test(sys.argv[2], "", "")
print(tst.getOne(2))
dump(tst)
tst.save(T1(999, "XXX"))
dump(tst)
tst.remove(999)
dump(tst)

Compile and run:

P1 = JDBC driver jar name
P2 = JDBC driver class name
P3 = JDBC connection URL
P4 = username
P5 = password
$ javac -cp runtime12.jar Test.java T1.java
$ define/nolog jython_libs "translator.jar:runtime12.jar:ojdbc5.jar:sunio.jar:''p1'"
$ jython Main.py "''p2'" "''p3'" "''p4'" "''p5'"
import sys

exec("import " + sys.argv[1])

import Test
import T1

def dump(tst):
    for o in tst.getAll():
        print(o)

print("Test of Jython with %s" % (sys.argv[2]))
if len(sys.argv) > 3:
    tst = Test(sys.argv[2], sys.argv[3], sys.argv[4])
else:
    tst = Test(sys.argv[2], "", "")
print(tst.getOne(2))
dump(tst)
tst.save(T1(999, "XXX"))
dump(tst)
tst.remove(999)
dump(tst)

Compile and run:

P1 = JDBC driver jar name
P2 = JDBC driver class name
P3 = JDBC connection URL
P4 = username
P5 = password
$ javac -cp runtime12.jar Test.java T1.java
$ define/nolog jython_libs "translator.jar:runtime12.jar:ojdbc5.jar:sunio.jar:''p1'"
$ jython Main.py "''p2'" "''p3'" "''p4'" "''p5'"

Test:

The code above is tested with:

Application Database
VMS Alpha Java 5
Groovy 2.2
Jython 2.5
VMS Alpha MySQL 5.5
VMS Alpha Java 5
Groovy 2.2
Jython 2.5
VMS Alpha Rdb 7.3
VMS Alpha Java 5
Groovy 2.2
Jython 2.5
(embedded) SQLite 3.34
VMS Alpha Java 5
Groovy 2.2
Jython 2.5
(embedded) H2 1.2
VMS x64-64 Java 8
Groovy 4.0
Jython 2.7
Windows MySQL 8.0
VMS x64-64 Java 8
Groovy 4.0
Jython 2.7
Windows PostgreSQL 15
VMS x64-64 Java 8
Groovy 4.0
Jython 2.7
Windows SQLServer 2022
VMS x64-64 Java 8
Groovy 4.0
Jython 2.7
(embedded) SQLite 3.47
VMS x64-64 Java 8
Groovy 4.0
Jython 2.7
(embedded) H2 2.2

This is obviously not all possible databases, but it does cover a bunch of the most relevant databases.

The future?

I am pretty sure that SQLJ does not have a future on VMS.

Plain JDBC and JPA provide the needed database API's.

The Java crowd is happy with those.

The embedded SQL crowd will likely not be interested in Java.

Article history:

Version Date Description
1.0 November 19th 2026 Initial version

Other articles:

See list of all articles here

Comments:

Please send comments to Arne Vajhøj