Nonstop Database, HiRDB Version 9 UAP Development Guide
This section presents an example of a stored procedure that uses the SELECT statement to retrieve BLOB data stored in the pics table, zips the data (compresses the data in the ZIP format), then returns it to the calling program.
CREATE PROCEDURE get_pic .............................................1 (IN pic_num INTEGER, OUT pic_data BLOB) ............................1 LANGUAGE JAVA ......................................................2 EXTERNAL NAME 'mypack.jar:JStrPics.getZippedPic(int, byte[][])' ....3 PARAMETER STYLE JAVA; ..............................................4 |
import java.sql.*; import java.io.*; import java.util.zip.*; public class JStrPics{ ...............................................1 public static void getZippedPic(int jpic_num, byte[][] jpic_data) ..2 throws SQLException, IOException{ ................................3 Connection con = DriverManager.getConnection( ....................4 "jdbc:hitachi:hirdb"); ....................4 PreparedStatement pstmt = con.prepareStatement ...................5 ("select p_name,p_data from pics where p_num = ?"); ...........5 pstmt.setInt(1, jpic_num); .......................................5 ResultSet rs = pstmt.executeQuery(); .............................6 String name; .....................................................7 byte[] srcPic; ...................................................7 while(rs.next()){ name = rs.getString("p_name"); .................................8 srcPic = rs.getBytes("p_data"); ................................9 } ByteArrayOutputStream baos = new ByteArrayOutputStream(); ........10 ZipOutputStream zos = new ZipOutputStream(baos); .................10 ByteArrayInputStream bais = new ByteArrayInputStream(srcPic); ....10 ZipEntry ze = new ZipEntry(name); ................................10 zos.putNextEntry(ze); ............................................10 int len = 0; .....................................................10 byte[] buff = new byte[1024]; ....................................10 while((len = bais.read(buff)) != -1){ ............................10 zos.write(buff, 0, len); .........................................10 } ................................................................10 zos.closeEntry(); ................................................11 bais.close(); ....................................................11 zos.close(); .....................................................11 jpic_data[0] = baos.toByteArray(); ...............................12 baos.close(); ....................................................12 return; ..........................................................13 } } |
import java.sql.*; import java.io.* ; public class Caller{ ...................................................1 public static void main(String[] args) ...............................2 throws SQLException, IOException{ ..................................3 Connection con = DriverManager.getConnection( ......................4 "jdbc:hitachi:hirdb","USER1","PASS1"); ............4 CallableStatement cstmt = con.prepareCall("{call get_pic(?,?)}"); ..5 cstmt.setInt(1, 10); ...............................................5 cstmt.registerOutParameter(2, java.sql.Types.LONGVARBINARY); .......5 cstmt.executeUpdate(); .............................................6 byte[] getPic = cstmt.getBytes(2); .................................7 ...... } } |
All Rights Reserved. Copyright (C) 2011, Hitachi, Ltd.