cobol连接数据库

mysql/DB2:

       $SET SQL(dbman=ODBC, DB=ConnToMysql, INIT)
       IDENTIFICATION DIVISION.
       PROGRAM-ID.  cobol-mysql.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       EXEC SQL INCLUDE SQLCA END-EXEC.
       EXEC SQL BEGIN DECLARE SECTION END-EXEC.
       01  TBL-STUDENT.
           03 STU-ID        PIC 9(4) COMP-5.
           03 STU-NAME      PIC X(20).
       EXEC SQL END DECLARE SECTION END-EXEC.

       PROCEDURE DIVISION.
       MAIN      SECTION.
       Begin.
      *  EXEC SQL
      *  CONNECT WITH PROMPT
      *  END-EXEC.
      *  IF SQLCODE <> 0
      *     DISPLAY SQLCODE
      *     DISPLAY SQLERRMC
      *  END-IF.

      *  EXEC SQL
      *  CREATE TABLE TBLSTUDENT(
      *      STU_ID      INT(4) PRIMARY KEY,
      *      STU_NAME    CHAR(20),
      *      CLASS_ID    INT(2)
      *  )
      *  END-EXEC.

         EXEC SQL
         INSERT INTO TBLSTUDENT (
             STU_ID,STU_NAME,CLASS_ID)
           VALUES (
             1,'STUDENT1',1)
         END-EXEC.
         IF SQLCODE <> 0 THEN
            DISPLAY SQLCODE
            DISPLAY SQLERRMC
         ELSE
            EXEC SQL COMMIT        END-EXEC
         END-IF.

         MOVE 1 TO STU-ID.
         EXEC SQL
         SELECT STU_NAME INTO :STU-NAME
           FROM TBLSTUDENT WHERE STU_ID = :STU-ID
         END-EXEC.
         DISPLAY STU-NAME.

         STOP 0.

 

ConnToMysql为DSN名(配置ODBC时指定的数据源名)

 

Oracle:

CONNECT :USERNAME IDENTIFIED BY :PASSWD USING :DBNAME

 

SQL Server:

CONNECT TO "ConnToSQLServer" USER :USERNAME WITH NO PROMPT

你可能感兴趣的:(oracle,sql,mysql,SQL Server,cobol)