このサンプル・プログラムでは、静的 SQL ステートメントおよび以下のサポートされる言語でのデータベース・マネージャー API の例を説明しています。
REXX 言語は静的 SQLをサポートしないため、サンプルはありません。
このサンプル・プログラムには、単一行を選択する照会が含まれます。このタイプの照会は SELECT INTO ステートメントを用いて実行することができます。
SELECT INTO ステートメントはデータベース内の複数の表から一行のデータを選択し、そしてその値をステートメントで指定したホスト変数に割り当てます。ホスト変数については、ホスト変数の使用で詳しく説明します。たとえば次のステートメントは、姓が 'HAAS' の従業員の給料をホスト変数 empsal に渡します。
SELECT SALARY INTO :empsal FROM EMPLOYEE WHERE LASTNAME='HAAS'
SELECT INTO ステートメントには 1 行だけ戻すように、または行を戻さないように指定する必要があります。行が複数検出されると、SQLCODE -811 (SQLSTATE 21000) エラーが起こります。照会の結果として複数行が検出されたら、カーソルを使用してそれらの行を処理する必要があります。詳細については、カーソルを用いた複数行の選択を参照してください。
SELECT INTO ステートメントの詳細については、 SQL 解説書 を参照してください。
SELECT ステートメントの記述方法の初歩的な説明については、 データの検索および操作のための SQL ステートメントのコーディングを参照してください。
このエラー検査ユーティリティーのソース・コードについては、 プログラム例での GET ERROR MESSAGE の使用を参照してください。
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "utilemb.h" EXEC SQL INCLUDE SQLCA; (1) int main(int argc, char *argv[]) { int rc = 0; char dbAlias[15] ; char user[15] ; char pswd[15] ; EXEC SQL BEGIN DECLARE SECTION; (2) char firstname[13]; EXEC SQL END DECLARE SECTION; /* checks the command line arguments */ rc = CmdLineArgsCheck1( argc, argv, dbAlias, user, pswd ); (3) if ( rc != 0 ) return( rc ) ; printf("\n\nSample C program: STATIC\n"); /* initialize the embedded application */ rc = EmbAppInit( dbAlias, user, pswd); if ( rc != 0 ) return( rc ) ; EXEC SQL SELECT FIRSTNME INTO :firstname (4) FROM employee WHERE LASTNAME = 'JOHNSON'; EMB_SQL_CHECK("SELECT statement"); (5) printf( "First name = %s\n", firstname ); /* terminate the embedded application */ rc = EmbAppTerm( dbAlias); return( rc ) ; } /* end of program : STATIC.SQC */
import java.sql.*; import sqlj.runtime.*; import sqlj.runtime.ref.*; class Static { static { try { Class.forName ("COM.ibm.db2.jdbc.app.DB2Driver").newInstance (); } catch (Exception e) { System.out.println ("\n Error loading DB2 Driver...\n"); System.out.println (e); System.exit(1); } } public static void main(String argv[]) { try { System.out.println (" Java Static Sample"); String url = "jdbc:db2:sample"; // URL is jdbc:db2:dbname Connection con = null; // Set the connection (3) if (argv.length == 0) { // connect with default id/password con = DriverManager.getConnection(url); } else if (argv.length == 2) { String userid = argv[0]; String passwd = argv[1]; // connect with user-provided username and password con = DriverManager.getConnection(url, userid, passwd); } else { throw new Exception("\nUsage: java Static [username password]\n"); } // Set the default context DefaultContext ctx = new DefaultContext(con); DefaultContext.setDefaultContext(ctx); String firstname = null; #sql { SELECT FIRSTNME INTO :firstname FROM employee WHERE LASTNAME = 'JOHNSON' } ; (4) System.out.println ("First name = " + firstname); } catch( Exception e ) (5) { System.out.println (e); } } }
Identification Division. Program-ID. "static". Data Division. Working-Storage Section. copy "sql.cbl". copy "sqlca.cbl". (1) EXEC SQL BEGIN DECLARE SECTION END-EXEC. (2) 01 firstname pic x(12). 01 userid pic x(8). 01 passwd. 49 passwd-length pic s9(4) comp-5 value 0. 49 passwd-name pic x(18). EXEC SQL END DECLARE SECTION END-EXEC. 77 errloc pic x(80). Procedure Division. Main Section. display "Sample COBOL program: STATIC". display "Enter your user id (default none): " with no advancing. accept userid. if userid = spaces EXEC SQL CONNECT TO sample END-EXEC else display "Enter your password : " with no advancing accept passwd-name. * Passwords in a CONNECT statement must be entered in a VARCHAR format * with the length of the input string. inspect passwd-name tallying passwd-length for characters before initial " ". EXEC SQL CONNECT TO sample USER :userid USING :passwd (3) END-EXEC. move "CONNECT TO" to errloc. call "checkerr" using SQLCA errloc. EXEC SQL SELECT FIRSTNME INTO :firstname (4) FROM EMPLOYEE WHERE LASTNAME = 'JOHNSON' END-EXEC. move "SELECT" to errloc. call "checkerr" using SQLCA errloc. (5) display "First name = ", firstname. EXEC SQL CONNECT RESET END-EXEC. (6) move "CONNECT RESET" to errloc. call "checkerr" using SQLCA errloc. End-Prog. stop run.