Example: Selecting data
After you select from a table into a ResultSet object, you can use the ResultSet.getBinaryStream() method to retrieve a stream of binary or ASCII data from the columns of type BYTE. You can also use the ResultSet.getAsciiStream() method to retrieve a stream of binary or ASCII data from the columns of type TEXT. Both methods return an InputStream object, which can be used to read the data in chunks.
All the data in the returned stream in the current row must be read before you call the next() method to retrieve the next row.
try { stmt = conn.createStatement(); rs = stmt.executeQuery("Select * from tab1"); while( rs.next() ) { row++; value = rs.getBinaryStream(1); dispValue(value); } } catch (Exception e) { } ... public static void dispValue(InputStream in) { int size; byte buf; int count = 0; try { size = in.available(); byte ary[] = new byte[size]; buf = (byte) in.read(); while(buf!=-1) { ary[count] = buf; count++; buf = (byte) in.read(); } } catch (Exception e) { System.out.println("Error occured while reading stream ... \n"); } }
The example first puts the result of a SELECT statement into a ResultSet object. It then executes the method ResultSet.getBinaryStream() to retrieve the BYTE data into a Java™ InputStream object.
The method dispValue(), whose Java code is also included in the example, is used to print out the contents of the column to the standard output device. The dispValue() method uses byte arrays and the InputStream.read() method to systematically read the contents of the column of type BYTE.
The TextType.java program shows how to select data from a column of type TEXT. It is similar to selecting from a column of type BYTE, except the getAsciiStream() method is used instead of getBinaryStream().