Hi,
I am trying to a game, and i wanted to add my images to mysql. I have managed to save and retrieve the images.

I have written a code which allows me to only retrive 1 image on my jframe in a jlabel from the database, i am a little confused on how to retrieve more than 1 image, i will need atleast 20 images and they all need to go in to a jlabel on the frame.

I was wondering if anyone can help me please, also i was would anyone know how i can move layout the labels(images) in coloums of 5?

I would really appreciate any help.

Connection connection = null;
PreparedStatement statement = null;

ResultSet result;

public DisplayImage() {
    super("Image Display");
    setSize(600, 600);
    connection = getConnection();
    try { // table name:image and second image is field name
        statement = connection
                .prepareStatement("select image from  image where id = 1");
        result = statement.executeQuery();

        byte[] image = null;
        while (result.next()) {
            image = result.getBytes("image");

        }
        Image img = Toolkit.getDefaultToolkit().createImage(image);
        ImageIcon icon = new ImageIcon(img);
        JLabel lPhoto = new JLabel();
        lPhoto.setIcon(icon);
        add(lPhoto);

    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    setVisible(true);
}

public Connection getConnection() {
    Connection connection = null;

    try {
        Class.forName("com.mysql.jdbc.Driver");
        connection = DriverManager.getConnection(
                // user name:root and password:blank
                "jdbc:mysql://localhost:3306/insertRetriveImages", "root",
                "");

    } catch (Exception e) {
        System.out.println("Error Occured While Getting the Connection: - "
                + e);
    }
    return connection;
}

if you already have the code that retrieves it 1 time ... run that code several times.
or, replace your query

statement = connection
                .prepareStatement("select image from  image where id = 1");

with something like:

            statement = connection
            .prepareStatement("select * from  image");

then later on, based on the id, you can choose the image(s) you need.

The code that puts the image into a label (lines 20-24) can go inside the while loop so you create one label for each image in the result set.

If you set a GridLayout layoput manager for your window then the labels that you add will be placed ina grid with whatever number of rows and columns you specified.
THis tutorial explains - it looks complex but you only need the very simplest part - so that's easy:
http://docs.oracle.com/javase/tutorial/uiswing/layout/grid.html

Edited 2 Years Ago by JamesCherrill

Comments
to compensate for unappreciated help

Jamescherrill are u able to help with the coding for the loop i have tried to do it several different ways but im not getting anywere.

The code i have atm is

    while (rs.next) {
val theBlob = rs.getBlob("my_blob") 

Why?
You had code to read one image - why did you change that.
Did you read my previous post (first sentance)? It told you exactly what to do.

Comments
to compensate for unappreciated help

yes but i thought i had to change it for some reason.

so it should just be like

 while (result.next()) {

        Image img = Toolkit.getDefaultToolkit().createImage(image);
Image img1 = Toolkit.getDefaultToolkit().createImage(image);

        ImageIcon icon1 = new ImageIcon(img1);
        JLabel lPhoto1 = new JLabel();
        lPhoto1.setIcon(icon1);

        add(lPhoto1);
        }


    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    setVisible(true);
    }

but now nothing shows :( i dont know why but i am confused

That's a good start - you are adding lots of labels, each with an image in it.
Now on the the second part of my previous post. You need to set a GridLayout for your window so those labels get layed out in a grid. Read the tutorial link I gave you.

Comments
to compensate for unappreciated help

THIS IS NOT SHOWING ANY ANYTHING ON THE JFRAME.

 while (result.next()) {
        Image img = Toolkit.getDefaultToolkit().createImage(image);
Image img1 = Toolkit.getDefaultToolkit().createImage(image);
        ImageIcon icon1 = new ImageIcon(img1);
        JLabel lPhoto1 = new JLabel();
        lPhoto1.setIcon(icon1);
        add(lPhoto1);
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    setVisible(true);
    }

Edited 2 Years Ago by stresstedout

using my code which i have got uploading images i have can only do one, so do i need to change this so it save multiple images?

Connection connection = null;
String connectionURL = “jdbc:mysql://localhost:3306/mydb”;
ResultSet rs = null;

PreparedStatement psmnt = null;

FileInputStream fis;

try {

Class.forName(“com.mysql.jdbc.Driver”);//.newInstance();
connection = DriverManager.getConnection(connectionURL, “root”, “magnet”);
File image = new File(“c:/Me.jpg”);

psmnt = connection.prepareStatement(“insert into images(image) “+ “values(?)”);

fis = new FileInputStream(image); 
psmnt.setBinaryStream(1, (InputStream)fis, (int)(image.length()));

int flag= psmnt.executeUpdate();
if(flag>0) 
{
System.out.println(“Uploaded successfully !”);
}
else 
{
System.out.println(“unsucessfull to upload image.”);
}
}
catch (Exception ex) 
{
System.out.println(“Found some error : “+ex);
}
finally 
{ 
connection.close();
psmnt.close();
}
}
}

Edited 2 Years Ago by stresstedout

systematically downvoting posts of those trying to help you will not really 'lure' others into helping you.
you are saying that your code does not what you want it to do, but:
have you done what James suggested?
are you getting errors, and if so, which are they?
what is your complete code? you are merely showing bits, and not all the relevant code.

This question has already been answered. Start a new discussion instead.