Read an Image using JDBC

Here is an example for how to read an image in JDBC. In the previous example, we insert an image using JDBC. Now in this example, we are going to read that image from the database. If you are not coming through the previous example, it is recommended to read once insert an image using JDBC.

As we already discussed in the previous tutorial, reading an image is nothing but reading binary data. For reading operations in JDBC, we can use the executeQuery() method on a statement.

executeQuery() method returns the ResultSet object. It contains the data rows. Usually, we can read the data from ResultSet using getXXX() methods.

But here, in our example, we are not reading ordinary data. We are going to retrieve an image i.e binary data. To read the binary objects, ResultSet given us getBinaryStream(String col) method.

To call the getBinaryStream() method, we need to pass the column name of BOLB type in database, in our case it “photo”.


                        InputStream is = rs.getBinaryStream("photo");
                    
Read an Image in JDBC Example :

                        public class JDBCGettingLargeObjectsExample {

                            public static void main(String[] args) throws Exception {
                        
                                Connection connection = null;
                                Statement stmt = null;
                        
                                try {
                                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                                    connection = DriverManager.getConnection(
                                            "jdbc:mysql://localhost:3306/jdbc", "root", "123456");
                                    stmt = connection.createStatement();
                                    ResultSet rs = stmt.executeQuery("select * from employee");
                                    if (rs.next()) {
                                        InputStream is = rs.getBinaryStream("photo");
                                        FileOutputStream fos = new FileOutputStream(
                                                "C:/Users/cgoka/Desktop/sample_NEW.jpg");
                                        int bytesRead = 0;
                                        byte[] buffer = new byte[4096];
                                        while ((bytesRead = is.read(buffer)) != -1) {
                                            fos.write(buffer, 0, bytesRead);
                                        }
                                        is.close();
                                        fos.close();
                                    }
                        
                                    connection.close();
                                    System.out.println("Image created");
                        
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    

By running this example, you can find an image in your desired folder.