Nonstop Database, HiRDB Version 9 UAP Development Guide

[Contents][Index][Back][Next]

18.16 Example UAP that uses a JDBC driver

This section presents an example of a UAP that uses a JDBC driver.

This UAP executes SQL statements that display the rows that satisfy specified conditions. The UAP checks the SQL statements for errors and acquires error information when errors are detected.

 
import java.sql.*;
import java.io.*;
import java.util.Properties;
 
public class SAMPLE {
 
    public static void main(String[] args) {
 
        //Connection object variables
        String url = "jdbc:hitachi:hirdb://DBID=22200,DBHOST=host1";
        String user = "USER1";
        String passwd = "USER1";
        String driver = "JP.co.Hitachi.soft.HiRDB.JDBC.HiRDBDriver";
        try {
            Class.forName(driver);
        }catch(Exception e){e.printStackTrace();return;}
 
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
 
        // *** Environment variables ***
        java.util.Properties info = new java.util.Properties();
        info.setProperty("user",user);
        info.setProperty("password",passwd);
        info.setProperty("HiRDB_for_Java_ENV_VARIABLES",
                     "PDCLTPATH=C:\\tmp;PDSQLTRACE=0;PDPRMTRC=INOUT;");
        // ************
 
        // Register and load the driver
        System.setProperty("jdbc.drivers",
                           "JP.co.Hitachi.soft.HiRDB.JDBC.HiRDBDriver");
 
        // Configure an environment
        try {
            // Connect  ..................................................1
            con = DriverManager.getConnection(url,info);
 
 
            // *********************************************
       // Example of retrieval by SAMPLE1(C1 INT,C2 INT,C3 VARCHAR(30)).........2
            // *********************************************
            // Acquire PreparedStatement
            pstmt = con.prepareStatement
                            ("SELECT C2,C3 FROM SAMPLE1 WHERE C1 = ? ");
            // Set a ? parameter (C1 = 200)
            pstmt.setInt(1,200);
            // Acquire ResultSet
            rs = pstmt.executeQuery();
            int cnt=1;
            System.out.println("**** Retrieving ****");
            while(rs.next()){
                System.out.println("**** Retrieving row "+cnt+" ***");
                // Acquire and display C2 data
                int i_data = rs.getInt(1);
                System.out.println("C2="+i_data);
                // Acquire and display C3 data
                String c_data = rs.getString(2);
                System.out.println("C3="+c_data);
                cnt++;
            }
            // Release ResultSet
            rs.close();
            // Release PreparedStatement
            pstmt.close();
            // Disconnect..................................................3
            con.close();
            }
 
        catch(SQLException e){  .......................................4
            // Output error information
            e.printStackTrace();
            // Acquire individual information item by the following processing
            // Output SQLSTATE
            System.out.println("SQLSTATE=" + e.getSQLState());
            // Output SQLCODE
            System.out.println("SQLCODE=" + e.getErrorCode());
            // Output SQLERRM (SQL message)
            System.out.println("SQLERRM=" + e.getMessage());
            return;
        }
    }
}
 

Explanation:
  1. Uses the getConnection method to connect to HiRDB.
  2. Executes the SQL statement that displays the rows that satisfy the specified conditions.
  3. Uses the close method to disconnect from HiRDB.
  4. If an error occurs, the UAP returns SQLException and outputs error information.