Hitachi

ノンストップデータベース HiRDB Version 10 UAP開発ガイド


18.4.4 ネイティブインタフェースを使用したコーディング例

〈この項の構成〉

(1) データの挿入と検索

データの挿入と検索をするコーディング例(sample1.sqlj)を次に示します。

import java.sql.*;
import JP.co.Hitachi.soft.HiRDB.pdjpp.runtime.*;
//反復子(カーソル)宣言
#sql iterator Pos(int,HiRDBCHAR(10),HiRDBNCHAR(5),HiRDBDECIMAL(10,5));
 
public class sample1{
  public static void main(String args[]){
 
    //接続及びテーブル作成
    try{
      #sql{CONNECT};    //クライアント環境変数を参照し接続します
      #sql{CREATE TABLE SAMPLE1(c1 int,c2 char(10),c3 nchar(5),c4 decimal(10,5))};
    }catch(SQLException e){System.out.println(e.getMessage());};
 
    //データのインサート
    try{
      int InInt = 100;
      HiRDBCHAR InChar = new HiRDBCHAR("CHAR");
      HiRDBNCHAR InNchar = new HiRDBNCHAR("NCHAR");
      HiRDBDECIMAL InDecimal = new HiRDBDECIMAL("12345.678");
 
      #sql{INSERT INTO SAMPLE1 VALUES(:InInt,:InChar,:InNchar,:InDecimal)};
      #sql{COMMIT};
    }catch(SQLException e){System.out.println(e.getMessage());};
 
    //データの検索(FETCH)
    try{
      Pos sampleCur = null;
      int OutInt = 0;
      HiRDBCHAR OutChar = null;
      HiRDBNCHAR OutNchar = null;
      HiRDBDECIMAL OutDecimal = null;
 
      #sql sampleCur  = {SELECT * FROM SAMPLE1};
      while(true){
        #sql {FETCH :sampleCur INTO :OutInt ,:OutChar ,:OutNchar ,:OutDecimal };
        if(sampleCur.endFetch()) break;
          System.out.println("c1="+ OutInt +" c2="+ OutChar.getString() + 
             " c3="+ OutNchar.getString() + " c4="+ OutDecimal.getString());
      }
    }catch(SQLException e){System.out.println(e.getMessage());};
    try{#sql{DISCONNECT};}catch(SQLException e){System.out.println(e.getMessage());}
  }
}

(2) データの挿入と1行検索

データの挿入と1行検索をするコーディング例(sample2.sqlj)を次に示します。

import java.sql.*;
import JP.co.Hitachi.soft.HiRDB.pdjpp.runtime.*;
 
public class sample2{
  public static void main(String args[]){
    String userid = "user1";
    String passwd = "user1";
 
    //接続及びテーブル作成
    try{
    //指定された認可識別子,パスワードで接続します
      #sql{CONNECT USER :userid USING :passwd};
      #sql{CREATE TABLE SAMPLE1(c1 int,c2 char(10),c3 nchar(5),c4 decimal(10,5))};
    }catch(SQLException e){System.out.println(e.getMessage());};
 
    //データのインサート
    try{
      int InInt = 100;
      HiRDBCHAR InChar = new HiRDBCHAR("CHAR");
      HiRDBNCHAR InNchar = new HiRDBNCHAR("NCHAR");
      HiRDBDECIMAL InDecimal = new HiRDBDECIMAL("12345.678");
 
      #sql{INSERT INTO SAMPLE1 VALUES(:InInt,:InChar,:InNchar,:InDecimal)};
      #sql{COMMIT};
    }catch(SQLException e){System.out.println(e.getMessage());};
 
    //データの検索(1行検索)
    try{
      //出力変数の宣言
      int OutInt = 0;
      HiRDBCHAR OutChar = new HiRDBCHAR(10);
      HiRDBNCHAR OutNchar = new HiRDBNCHAR(5);
      HiRDBDECIMAL OutDecimal = new HiRDBDECIMAL(10,5);
 
      #sql {SELECT * INTO :OutInt,:OutChar,:OutNchar,:OutDecimal FROM SAMPLE1};
      System.out.println("c1="+ OutInt +" c2="+ OutChar.getString() + 
        " c3="+ OutNchar.getString() + " c4="+ OutDecimal.getString());
    }catch(SQLException e){System.out.println(e.getMessage());};
    try{#sql{DISCONNECT};}catch(SQLException e){System.out.println(e.getMessage());}
  }
}

(3) CALL文の実行

CALL文を実行するコーディング例(sample3.sqlj)を次に示します。

import java.sql.*;
import JP.co.Hitachi.soft.HiRDB.pdjpp.runtime.*;
 
public class sample3{
  public static void main(String args[]){
 
    Integer PInteger1 = new Integer(99);
    Integer PInteger2 = new Integer(100);
    Integer PInteger3 = new Integer(101);try{
      #sql {CONNECT};
    }catch(SQLException e){System.out.println(e.getMessage());}
 
    try{
      #sql {DROP PROCEDURE PROCSQLJ};
      #sql {DROP TABLE PROCTABLE};
    }catch(SQLException e1){}
 
    try{
      #sql {CREATE TABLE PROCTABLE(c1 int, c2 int)};
      #sql {CREATE PROCEDURE PROC1(in p1 int,out p2 int,inout p3 int)
        begin 
        insert into PROCTABLE values(p1,p3); 
        select * into p2,p3 from PROCTABLE;
        end};
      #sql {COMMIT};
    }catch(SQLException e){System.out.println(e.getMessage());}
 
    try{
      #sql {CALL PROC1(in :PInteger1 ,out :PInteger2 ,inout :PInteger3 )};
    }catch(SQLException e){System.out.println(e.getMessage());}
 
    System.out.println("INパラメタPInteger1 = " + PInteger1 );
    System.out.println("OUTパラメタPInteger2 = " + PInteger2 );
    System.out.println("INOUTパラメタPInteger3 = " + PInteger3 );
 
    try{#sql {DISCONNECT};}catch(SQLException e){System.out.println(e.getMessage());}
  }
}

(4) カーソルを使用した更新

カーソルを使用した更新をするコーディング例(sample4.sqlj)を次に示します。

import java.sql.*;
import JP.co.Hitachi.soft.HiRDB.pdjpp.runtime.*;
#sql iterator iterP implements JP.co.Hitachi.soft.HiRDB.pdjpp.runtime.ForUpdate(short);
 
public class sample4{
  public static void main(String args[]){
    iterP positer = null;
    iterP positer2 = null;
    short indata;
    short indata2 = 0;
    short indata3 = 999;
    try{
      #sql {CONNECT};
      #sql {DROP TABLE CURTABLE};
    }catch(SQLException e){System.out.println(e.getMessage());}
    //テーブル作成
    try{#sql {CREATE TABLE CURTABLE(c1 smallint)};
    }catch(SQLException e){System.out.println(e.getMessage());}
    //データのインサート
    for(short i = 0;i < 5;i++){
      indata = i;
      try{#sql{INSERT INTO CURTABLE VALUES(:indata)};}catch(SQLException e){}
    }
    //SELECTの実行とカーソルを使用した更新
    try{
      #sql positer = {SELECT * FROM CURTABLE};
    }catch(SQLException e){}
    try{
      while(true){
        #sql {FETCH :positer INTO :indata2};
        if(positer.endFetch()) break;
        System.out.println(indata2);
        #sql { UPDATE CURTABLE SET C1=:indata3 WHERE CURRENT OF :positer };
      }
    }catch(SQLException e){e.getMessage();}
    //更新結果の確認
    try{#sql positer2 = {SELECT * FROM CURTABLE};}catch(SQLException e){}
    try{
      while(true){
        #sql {FETCH :positer2 INTO :indata2};
        if(positer2.endFetch()) break;
        System.out.println(indata2);
      }
    }catch(SQLException e){System.out.println(e.getMessage());}
    try{#sql{DISCONNECT};}catch(SQLException e){}
  }
}