Table of Contents Previous Next


8 Advanced JDBC Connector Functionality : 8.5 Using BYTEA Data with Java

The BYTEA data type stores a binary string in a sequence of bytes; digital images and sound files are often stored as binary data. Advanced Server can store and retrieve binary data via the BYTEA data type.
The following Java sample stores BYTEA data in an Advanced Server database and then demonstrates how to retrieve that data. The example requires a bit of setup; Listings 1.12-a, 1.12-b, and 1.12-c create the server-side environment for the Java example.
Listing 1.12-a creates a table (emp_detail) that stores BYTEA data. emp_detail contains two columns: the first column stores an employee’s ID number (type INT) and serves as the primary key for the table; the second column stores a photograph of the employee in BYTEA format.
Listing 1.12-b creates a procedure (ADD_PIC) that inserts a row into the emp_detail table:
And finally, Listing 1.12-c creates a function (GET_PIC) that returns the photograph for a given employee:
Listing 1.13 shows a Java method that invokes the ADD_PIC procedure (see Listing 1.12-b) to copy a photograph from the client file system to the emp_detail table on the server.
InsertPic() prompts the user for an employee number and the name of an image file:
If the requested file does not exist, InsertPic() displays an error message and terminates:
Next, InsertPic() prepares a CallableStatement object (stmt) that calls the ADD_PIC procedure. The first placeholder (?) represents the first parameter expected by ADD_PIC (p_empno); the second placeholder represents the second parameter (p_photo). To provide actual values for those placeholders, InsertPic() calls two setter methods. Since the first parameter is of type INTEGER, InsertPic() calls the setInt() method to provide a value for p_empno. The second parameter is of type BYTEA, so InsertPic() uses a binary setter method; in this case, the method is setBinaryStream():
Now that the placeholders are bound to actual values, InsertPic() executes the CallableStatement:
If all goes well, InsertPic() displays a message verifying that the image has been added to the table. If an error occurs, the catch block displays a message to the user:
Now that you know how to insert BYTEA data from a Java application, Listing 1.14 demonstrates how to retrieve BYTEA data from the server.
GetPic() starts by prompting the user for an employee ID number:
Next, GetPic() prepares a CallableStatement with one IN parameter and one OUT parameter. The first parameter is the OUT parameter that will contain the photograph retrieved from the database. Since the photograph is BYTEA data, GetPic() registers the parameter as a Type.BINARY. The second parameter is the IN parameter that holds the employee number (an INT), so GetPic() uses the setInt() method to provide a value for the second parameter.
Next, GetPic() uses the getBytes getter method to retrieve the BYTEA data from the CallableStatement:
The FileOutputStream object writes the binary data that contains the photograph to the destination filename:
Finally, GetPic() displays a message confirming that the file has been saved at the new location:

8 Advanced JDBC Connector Functionality : 8.5 Using BYTEA Data with Java

Table of Contents Previous Next