1

In my postgres database table column :

photograph bytea

I want to retrieve all the image from database and save it to a folder .

So far I have tried this and getting null every time :

public class Test {

    public static void main(String[] argv) throws SQLException, IOException {

        System.out.println("-------- PostgreSQL "
                + "JDBC Connection Testing ------------");

        try {

            Class.forName("org.postgresql.Driver");

        } catch (ClassNotFoundException e) {

            System.out.println("Where is your PostgreSQL JDBC Driver? "
                    + "Include in your library path!");
            e.printStackTrace();
            return;

        }

        System.out.println("PostgreSQL JDBC Driver Registered!");

        Connection connection = null;

        try {

            connection = DriverManager.getConnection(
                    "jdbc:postgresql://127.0.0.1:5432/a?currentSchema=a", "postgres"
                                                + "",
                    "123456");

        } catch (SQLException e) {

            System.out.println("Connection Failed! Check output console");
            e.printStackTrace();
            return;

        }

        if (connection != null) {
            System.out.println("You made it, take control your database now!");
                        //start
                        String query = "SELECT photograph FROM a.c limit 10";
                        PreparedStatement pst = connection.prepareStatement(query);
                        ResultSet rs = pst.executeQuery();
                        while (rs.next()) {
                            System.out.println(rs.getString(1));
                            // convert byte array back to BufferedImage
                        if(null == rs.getString(1)){
                            continue;
                        }    
            InputStream in = new ByteArrayInputStream(rs.getString(1).getBytes());
            BufferedImage bImageFromConvert = ImageIO.read(in);
                        if(null == bImageFromConvert){
                            continue;
                        }
                        OutputStream out = new FileOutputStream("c:/"+"1"+".jpg");

            ImageIO.write(bImageFromConvert, "jpg", out);
                        }
                        //end


        } else {
            System.out.println("Failed to make connection!");
        }
    }

}

After debugging , I got bImageFromConvert as null everytime.

On System out print image data shows as like this :

\xffd8ffe000104a46494600010101004800480000ffe1174a4578696600004d4d002a00000008000d01000004000000010000102001..

I inserted data as private Byte[] photograph;

How to solve this issue ??

Insanity Geek
  • 157
  • 1
  • 12
  • 3
    There is `getBytes` or `getBinaryStream` which should replace `getString` – MadProgrammer Apr 26 '18 at 04:52
  • As a [conceptual example](https://stackoverflow.com/questions/32623165/updating-jlabel-via-seticon-from-bytea-data-type-in-postgres/32623457#32623457) – MadProgrammer Apr 26 '18 at 04:54
  • You are right. Its working . Can you tell me how to set height and width of that image?? – Insanity Geek Apr 26 '18 at 05:04
  • The size should already be set - did you mean you want to scale it? – MadProgrammer Apr 26 '18 at 05:07
  • Yes . Like height =300 and width =300 fixed when saved in folder – Insanity Geek Apr 26 '18 at 05:11
  • That raises more questions, but perhaps have a look at [maintaining aspect ratio of JPanel background image](https://stackoverflow.com/questions/11959758/java-maintaining-aspect-ratio-of-jpanel-background-image/11959928#11959928) for some ideas – MadProgrammer Apr 26 '18 at 05:16
  • Scaling images is not really a database task. That said, you might be able to achieve it with PostGIS raster support. But I'd do it on the client side. – Laurenz Albe Apr 26 '18 at 07:45

0 Answers0