0

I am trying to write a SQLJ application program to access Db2 data but I am facing some errors while creating the context for the connection as per the IBM manual.

I am receiving errors for the lines that starts with #sql.

#sql context TestSqljCtx;

I assume I need to include a JAR file in the build process to recognize this. But I could not identify what it is ?


import com.ibm.db2.jcc.DB2Connection;
import com.ibm.db2.jcc.DB2ConnectionPoolDataSource;
import com.ibm.db2.jcc.DB2PooledConnection;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;


#sql context TestSqljCtx;

public class StaticApp {

    private DB2Connection connection;
    private TestSqljCtx ctx;

    public static void main(String[] args) throws SQLException {
        new StaticApp().execute();
    }


    private void execute() throws SQLException {

        DB2PooledConnection pooledConnection;
        byte[] cookie;
        Connection con;
        Statement stmt;

        System.out.println("**** Enter class DataSourceMethod");
        //Create a DB2ConnectionPoolDataSource instance
        DB2ConnectionPoolDataSource dataSource = new DB2ConnectionPoolDataSource();


        //Set properties for this instance
        dataSource.setDatabaseName("");
        dataSource.setServerName("");
        dataSource.setDriverType(4);
        dataSource.setPortNumber();
        java.util.Properties properties = new java.util.Properties();

        //Set other properties using
        //properties.put("property", "value");
        //Supply the user ID and password for the connection
        String user = "";
        String password = "";

        //Call getDB2TrustedPooledConnection to get the trusted connection
        //instance and the cookie for the connection

        try {
            Object[] objects = dataSource.getDB2TrustedPooledConnection(user, password, properties);
            System.out.println("Data source creation Successful");

            pooledConnection = (DB2PooledConnection) objects[0];
            cookie = (byte[]) objects[1];

            con = (DB2Connection) pooledConnection.getDB2Connection(cookie, "", null, null, null, "", properties);

            //this.connection = null; // Change to obtain Db2 Connection
            this.ctx = new TestSqljCtx(con);

            try {
                deleteRows();
                getDiagnostics();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            } finally {
                this.connection.rollback();
                this.connection.close();
            }
        }catch (SQLException e) {
            System.out.println("Data source creation Error");
        }
    }

    private void getDiagnostics() throws SQLException {
        String diagnostics = null;
        #sql[ctx] {
            GET DIAGNOSTICS :diagnostics = ALL STATEMENT, CONDITION, CONNECTION
        } ;
        System.out.printf("Diagnostics: %s%n", diagnostics);
    }

    private void deleteRows() throws SQLException {
        String employeeID = "000010";
        #sql[ctx] {
            DELETE FROM ADHA01.EMP WHERE EMPNO = :employeeID
        } ;
    }
}


Please let me know if I am missing anything here.

Thanks.

  • Please edit your question (do not use comments) to specify the command-line(s) you are using to build your sqlj. When learning , consider getting the samples provided by IBM working in your environment using IBM's scripts to see how it works, the samples are in multiple places, for example, on github https://github.com/IBM/db2-samples/tree/master/java/sqlj , and in the online documentation at https://www.ibm.com/docs/en/db2/11.5?topic=api-sqlj-samples , as well as on any Db2-LUW server that has the samples installed . – mao May 30 '23 at 14:09

0 Answers0