在cobol中读取和执行sql语句

时间:2012-02-23 15:19:53

标签: sql database cobol

首先,对不起,如果已经问到这一点,我彻底搜查了,但似乎无法找到答案。

这是一个针对学校的项目,我们确实意识到这可以通过更有效的方式解决,但这不是一种选择。

所以这就是问题所在:

  1. 我们必须使用Microsoft SQL Server 2008 R2,这里我们有一个空数据库。
  2. 使用Cobol,我们必须使用参数文件sqlstatements.sql创建所有表(a.i.读取每个语句,执行它们,依此类推。
  3. 我们遇到的问题(甚至我们的老师也无法解决)是:

    1. 正在以非常奇怪的方式读取sql语句(语句随机乱码)
    2. 我们似乎没有任何实际编辑数据库的权利。
    3. 这是cobol代码:

         IDENTIFICATION DIVISION.
         PROGRAM-ID. DB_connection.
      
         INPUT-OUTPUT SECTION.
         FILE-CONTROL.
          SELECT sqlscript assign to "databaseScript.sql"
          ORGANIZATION IS LINE SEQUENTIAL.
          SELECT logfile assign to "logfile.txt"
          ORGANIZATION IS LINE SEQUENTIAL.
         DATA DIVISION.
         FILE SECTION.
         FD sqlscript.
         01 sqlstatement.
         FD logfile.
         01 logrecord.
         03 logline pic x(100).
         WORKING-STORAGE SECTION.
         EXEC SQL 
           BEGIN DECLARE SECTION
         END-EXEC
      
        * SQLCODE is 0 for success, 100 for no data, -1 for failure
         01 SQLCODE PIC S9(3). 
      
        * SQLSTATE is a 5 character communication code; 00xxx is success.
         01 SQLSTATE PIC X(5). 
      
         01 JdbcString PIC X(255).
      
         EXEC SQL 
           END DECLARE SECTION
         END-EXEC
         01 einde         pic X value '0'.
          88 einde-bestand value '1'. 
      
         PROCEDURE DIVISION.
         MAIN-PARAGRAPH.
        * Initial code
         OPEN INPUT sqlscript
         OPEN EXTEND logfile
         PERFORM DO-CONNECT
      
         DISPLAY "After connecting to the database:"
      
         DISPLAY "SQLCODE= " + SQLCODE.
         DISPLAY "SQLSTATE= " + SQLSTATE
         DISPLAY "Connectie met java succes."
      
         IF sqlcode = 0
         MOVE "Connectie succesvol" to logline
         WRITE logrecord
         DISPLAY "Writing to logfile"
         END-IF
      
      
         PERFORM LEESSQLBESTAND
        * lees sql bestand, read entry'sz
        * Use the database       
         CLOSE sqlscript 
         CLOSE logfile
         PERFORM DO-DISCONNECT
         ACCEPT SQLSTATE
        * Terminate the program    
         GOBACK
      
        * The SQL connect statement must be completed with the information
        * appropriate to the actual JDBC driver in use.  JDBC stands for
        * Java DataBase Connectivity, and it is the method by which PERCobol
        * accesses databases and database-like data sources.
        *
        * The JDBC driver itself must be included in the Java library path
        * in order to successfully connect to the database.  The JDBC driver
        * is generally included with the database itself; see the database
        * documentation for more details.
        *
        * When connecting to a datasource, the jdbc:url may be 
        * ds:data-source-name.
        *
        * jdbc:url        The JDBC url to the database itself     
        * com.driver.name This is the classname of the driver
        *
         DO-CONNECT.
      
         STRING "jdbc:sqlserver://localhost\SQLEXPRESS;" 
           DELIMITED BY SIZE
           "databaseName=ProjectManagement;"
           DELIMITED BY SIZE
           "userName=admin;password=broforce"
           DELIMITED BY SIZE
           INTO JdbcString
      
         EXEC SQL
          CONNECT
            TO :JdbcString         
            DRIVER "com.microsoft.sqlserver.jdbc.SQLServerDriver"                 
         END-EXEC.
      
         LEESSQLBESTAND.
         READ sqlscript
          AT END DISPLAY 'LEEG SQL BESTAND' MOVE '1' TO einde
          MOVE "Leeg SQL-Bestand" to logline
          NOT AT END DISPLAY 'MINSTENS 1 SQL STATEMENT'
          MOVE "Queries found..." to logline
         END-READ
         WRITE logrecord
      
         PERFORM UNTIL einde = 1
                 MOVE sqlstatement to logline
                 EXEC SQL
                         EXECUTE sqlstatement
                 END-EXEC
                 IF sqlcode < 0
                 move "failed" to logline
                 WRITE logrecord
                 END-IF
                 IF sqlcode = 0
                 move "succes" to logline
                 WRITE logrecord
                 END-IF
      
                 READ sqlscript
                   AT END DISPLAY 'EINDE SQL BESTAND'
                   MOVE '1' TO einde
                   MOVE "Einde Bestand" to logline
                 END-READ
                 WRITE logrecord
         EXEC SQL COMMIT WORK END-EXEC       
         END-PERFORM
      
      
        * Disconnect from the SQL database connection.  This allows the
        * JDBC driver to free any resources required for the connection.
      
      
         DO-DISCONNECT.
         EXEC SQL
          DISCONNECT
         END-EXEC.
      

      我在这里做错了吗?我们不知所措。

2 个答案:

答案 0 :(得分:1)

COBOL适用于固定长度记录,但它可以轻松处理可变长度记录。

...
FD sqlscript
   record is varying in size
   depending on sqlstatement-length.
01 sqlstatement.
   05 filler pic x occurs 0 to 9999 times
      depending on sqlstatement-length.
...
working-storage
...
01 sqlstatement-length pic 9(4) binary.
...
procedure division.
...
read sqlscript
*then you can use sqlstatement-length to grab what you need
*you might want to add if sqlstatement-length = zero logic for blank lines
move sqlstatement(1:sqlstatement-length) to wherever

答案 1 :(得分:0)

随机加扰,假设我正在解释这意味着什么,通常意味着您需要初始化您正在读取的变量(剩余的数据使您的工作存储空间不足)或者您在不兼容之间移动数据类型或错误大小的字段。由于你的sqlstatement上没有图片条款(这怎么可以工作?)很难说出那里有什么问题。

就权限而言,我只是在IBM / DB2环境中经历过,但我总是必须为DB2中的个别程序设置权限,以允许它修改我的基础。