-1

I have a Mysql database which contain path of images stored in my local drive. I need to write a Java program which reads those images one-by-one.

I wrote the following code:

import java.sql.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;


public class ReadMultipleImages
{

  public static void main(String[] args)
  {
    try
    {

      String myDriver = "com.mysql.cj.jdbc.Driver";
      String myUrl = "jdbc:mysql://localhost/db?useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
      Class.forName(myDriver);
      Connection conn = DriverManager.getConnection(myUrl, "user", "password");

      String query = "SELECT * FROM test";

      Statement st = conn.createStatement();

      ResultSet rs = st.executeQuery(query);

      while (rs.next())
      {

        String img = rs.getString("img");

        BufferedImage in = ImageIO.read(new File(img));     
        System.out.println("Width of Image : " + in.getWidth());

      }
      st.close();
    }
    catch (Exception e)
    {
      System.err.println("Got an exception! ");
      System.err.println(e.getMessage());
    }
  }
}

It doesn't workout with multiple image paths. However, when I passed the direct single image path:

BufferedImage in = ImageIO.read(new File('/tmp/frame_004.jpej'));

It gives me exact results that I want. My finding are it work only with single image path not more than that. How can I work on multiple images returned by the variable img ?

Ali R. Memon
  • 121
  • 1
  • 1
  • 12
  • The img string receives the value '/home/arm/images/frame_1524043865776151594.jpeg' from database. Then I used this value to pass into ImageIO.read(). My database contains many image paths as above. – Ali R. Memon May 14 '18 at 19:14
  • I think you are right about splitting string into multiple paths but the while loop read one record at a time. – Ali R. Memon May 14 '18 at 19:17

1 Answers1

0

Finally the things on wheels:

import java.sql.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;


public class ReadMultipleImages
{

  public static void main(String[] args)
  {
    try
    {

      String myDriver = "com.mysql.jdbc.Driver";
      String myUrl = "jdbc:mysql://localhost:port/db_name?useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
      Class.forName(myDriver);
      Connection conn = DriverManager.getConnection(myUrl, "user", "password");

      String query = "SELECT * FROM metadata";

      Statement st = conn.createStatement();

      ResultSet rs = st.executeQuery(query);

      while (rs.next())
      {

    String srno = rs.getString("srno"); 
    String path = rs.getString("path");
    String file = rs.getString("file");

    // PATH: /home/memon/images/, FILE: frame_1524043865776151594.jpeg

    String loc = path + file;

    System.out.println("location : " + loc);

    BufferedImage in = ImageIO.read(new File(loc));     
        System.out.println("SRno. "+srno+" Width of Image : " + in.getWidth() + " X " + in.getHeight());

      }
      st.close();
    }
    catch (Exception e)
    {
      System.err.println("Got an exception! ");
      System.err.println(e.getMessage());
    }
  }
}

Output:

location : /home/memon/images/frame_1524043865776151594.jpeg
SRno. 0 Width of Image : 262 X 90
location : /home/memon/images/frame_1524043865776151595.jpeg
SRno. 1 Width of Image : 1920 X 1080
Ali R. Memon
  • 121
  • 1
  • 1
  • 12