Come eseguire un comando AS400 tramite JDBC/ODBC

In un precedente post abbiamo visto come eseguire un programma AS400 tramite JDBC/ODBC. Occupiamoci ora di come eseguire un comando tramite questa interfaccia.

Lo statement SQL CALL consente di eseguire esclusivamente dei programmi. Per lanciare un comando AS400 occorrerà quindi chiamare un programma ;-) A tale scopo esiste infatti il programma di sistema qcmdexc, che accetta due parametri: il primo è la stringa contenente il comando da eseguire, la seconda è un numero che indica la lunghezza del primo parametro. Ecco un esempio di codice Java che lo utilizza.

 
 try {
  Class.forName("com.ibm.as400.access.AS400JDBCDriver");
  Connection c = DriverManager.getConnection(
                 "jdbc:as400://AS400;user=PIPPO;password=X");
  CallableStatement cs =
                   c.prepareCall("CALL qsys.qcmdexc (?, ?)");   String cmd = "CHGJOB RUNPTY(15)";
  cs.setString(1, cmd);
	cs.setObject(2, new Integer(cmd.length()));
	cs.execute();
 } catch (Exception e) {}
 

Ed un esempio in Access

 
Function EseguiComandoAS(ByVal ComandoAS As String, ByVal NomeDSN As String)
Dim StrSQL As String
Dim L As Integer
Dim i As Integer
Dim SeparatoreDecimale As String
Dim wsODBC As Workspace
Dim connODBC As Connection
Dim QDirect As QueryDef
 
    SeparatoreDecimale = ","   'Attenzione: potrebbe essere .
 
    'Gestione doppi apici ('')
    L = Len(ComandoAS)
    i = 1
    Do While i < Len(ComandoAS) And i > 0
        i = InStr(i, ComandoAS, "''")
        If i <> 0 Then
            L = L - 1
            i = i + 2
        End If
    Loop
 
    StrSQL = "CALL qsys.qcmdexc('" & ComandoAS & "', " & Format$(L, "0000000000") & SeparatoreDecimale & "00000)"
 
    Set wsODBC = CreateWorkspace("AreaLavoroODBC", "", "", dbUseODBC)
    wsODBC.DefaultCursorDriver = dbUseClientBatchCursor
    Set connODBC = wsODBC.OpenConnection("ConnODBC", , False, "ODBC;DSN=" & NomeDSN)
 
    connODBC.Execute StrSQL
 
End Function
 

Lascia un commento