Using BYTEA data with Java v42.7.3.1
The BYTEA
data type stores a binary string in a sequence of bytes. Digital images and sound files are often stored as binary data. EDB Postgres Advanced Server can store and retrieve binary data by way of the BYTEA
data type.
The following Java sample stores BYTEA
data in an EDB Postgres Advanced Server database and then shows how to retrieve that data.
First, the following 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.
The following creates a procedure (ADD_PIC
) that inserts a row into the emp_detail
table:
Then, the following creates a function (GET_PIC
) that returns the photograph for a given employee:
Inserting BYTEA data into an EDB Postgres Advanced Server
The following shows a Java method that invokes the ADD_PIC
procedure 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 doesn't 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()
:
Once the placeholders are bound to actual values, InsertPic()
executes the CallableStatement
:
If all goes well, InsertPic()
displays a message verifying that the image was added to the table. If an error occurs, the catch
block displays a message to the user:
Retrieving BYTEA data from an EDB Postgres Advanced Server database
Now that you know how to insert BYTEA
data from a Java application, the following shows 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 program prompts the user for the name of the file to store the photograph:
The FileOutputStream
object writes the binary data that contains the photograph to the destination file:
Finally, GetPic()
displays a message confirming that the file was saved at the new location: