Consider a case where we want that along with the name of the person and its information, his image should also come. For this it is necessary that there must be his/her image in the database so that whenever needed we can retrieve it. In this program we are going to retrieve the image from the database table. You can do it very easily after going through this program.
To get the program working we need to use a doGet() method to write our business logic as it is server side programming so all the processing will be done by the container. First of all make a class named JdbcSelectingImage, the name of the class should be such that the person can understand what the program is going to do. This class must extend the HttpServlet class which is an abstract method. Now inside the doGet() method call the method getWriter() of the class PrintWriter. To retrieve an image from the database table our java program need to make a connection with the MySql database. The database can be any, it all depends on the person's choice. After the connection establishment we will pass a query for retrieving the image from the database table in prepareStatement() method of the Connection object which returns the PreparedStatement object.
As we have to retrieve the image from the database table so there is need of a ResultSet object which will help us to retrieve the image from the database. The ResultSet obiect will be retrieved from calling the executeQuery() method of the PreparedStatement object.. To get the image from the database use getBinaryStream() method of the ResultSet interface.
The code of the program is given below:
The output of the program is given below:
Download this example.
Ask Questions? Discuss: Retrieving the Image from a database Table
Post your Comment
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.