Build the User-Defined Method DBConnection() 2 – Develop Java Web Services to Access Databases

A. Some useful packages are first imported into the source window for this operation. You can right-click on the code window and select Fix Imports from the popup menu if you do not want to declare these packages yourself when this piece of code is done.

B. Some local objects and variables are declared here; the bimg is a Blob object since we need to retrieve a desired faculty image from the Faculty Table and the image is a Blob format in our database column. fimg is a type of Image object since this type of object can be used as a returned object from the Web Service project. Since this object belongs to the java.awt.Image class, a full package name is used.

C. The query string is declared with faculty _ name as an input position parameter.
D. A try-catch block is used to perform the image query operation. First a connection method, DBConnection(), which will be built in the next section, is executed to connect to our sample database, CSE _ DEPT.

E. A new PreparedStatement instance, pstmt, is declared and created to perform the query. F. The setString() method is used to set up the actual value that is our input faculty name
for the positional parameter faculty _ name.
G. The image query is performed by calling the executeQuery() method, and the query result is returned and stored in a ResultSet object, rs.

H. To get more detailed information about the queried database, the getMetaData() method is executed, and the result is stored in a ResultSetMetaData instance rsmd.

FIGURE 9.27  The code for the second operation QueryImage().

I. while() and for() loops are used to pick up the queried faculty image stored in the ResultSet object rs. In fact, the while() loop only runs one time since only one match-ing faculty row will be returned. The getColumnCount() method is used as the upper-bound of the for loop, and it should be equal to 8, since the eighth column in the Faculty Table stores the desired faculty image.

J. An if selection structure is used to check if the eighth column has been retrieved. If it is, the getBlob() method is executed to pick up the image in that column and assign it to the local object bimg, which is a Blob-type object.

K. The system method getBinaryStream() is used to convert the Blob object to an Image object, fimg, with another system method, read(), that belongs to the ImageIO class.
L. Then a break instruction skips out and terminates this for loop.

M. A sequence of cleaning jobs is performed to close all used objects, and the queried faculty image is returned.

N. The catch block is used to catch and display any possible error during the image query.

At this point, we have finished all code development for our Web service used to perform queries to our Faculty Table. Prior to building and running our project to test its functions, we need first to set up the Oracle JDBC driver and our correct Java platform.

Leave a Reply

Your email address will not be published. Required fields are marked *