简体   繁体   English

如何使用Java从MySQL数据库中获取图像

[英]How to fetch images from mysql db with java

I am trying to fetch every picture from a database and put it in a ArrayList, then printing it out. 我试图从数据库中获取每张图片,并将其放入ArrayList中,然后将其打印出来。 But I get a result that I dont understand. 但是我得到一个我不明白的结果。

The result: 结果:

在此处输入图片说明

Q1: Why does I get this result? Q1:为什么我得到此结果?

Q2: If I then want to show the picture with a ImageIcon, is ArrayList they way to go? Q2:如果然后我想用ImageIcon显示图片,是否可以使用ArrayList?

Here is my code: 这是我的代码:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.*;


public class ImageData {

    public static void main(String[] args){

        Connection con = null;
        Statement st = null;
        ResultSet rs = null;

        //databasens lokalisation, namn, användare & lösenord
        String url      = "jdbc:mysql://localhost:3306/";
        String dbname   = "101207-facemash";
        String user     = "root";
        String pwd      = "root";
        List<String> img = new ArrayList<String>();

        try{
            // Vi upprätta en anslutning till databasen med hjälp av anslutningen URL, dbnamnet, användarnamn och lösenord.
            con = DriverManager.getConnection(url + dbname, user, pwd);
            st = con.createStatement();
            rs = st.executeQuery("SELECT filename FROM images");

            while(rs.next()){
                img.add(rs.getString("filename"));
                System.out.println(img);
            }

         } catch (SQLException ex) {
                Logger lgr = Logger.getLogger(ImageData.class.getName());
                lgr.log(Level.SEVERE, ex.getMessage(), ex);

            } finally {
                try {
                    if (rs != null) {
                        rs.close();
                    }
                    if (st != null) {
                        st.close();
                    }
                    if (con != null) {
                        con.close();
                    }

                } catch (SQLException ex) {
                    Logger lgr = Logger.getLogger(ImageData.class.getName());
                    lgr.log(Level.WARNING, ex.getMessage(), ex);
                }
            }

        }

    }

You're retrieving the names of the image files, not the images themselves. 您正在检索图像文件的名称,而不是图像本身。 If the images are stored in the data base, you'll have to retrieve them, either with a second query using the image name retrieved from the db with the first query or by retrieving a different column of the table (depending on how your db is organized). 如果图像存储在数据库中,则必须使用第二个查询(使用通过第一次查询从db中检索到的图像名称)或通过检索表的不同列来检索它们(取决于数据库的方式)组织)。 If they are stored in separate files (with the db containing only the file names), then you'll need to read each image in after retrieving the name. 如果它们存储在单独的文件中(db仅包含文件名),那么检索名称后,您需要读取每个图像。 The ImageIO class will be useful for that. ImageIO类将对此有用。 (Also, the ImageIcon class can retrieve images itself if you put the name in the form of a URL.) (此外,如果您以URL的形式输入名称,则ImageIcon类也可以检索图像本身。)

As to using an ArrayList — that's generally a good way of dealing with an array of indeterminate size. 至于使用ArrayList,这通常是处理不确定大小数组的好方法。 I don't think that the fact that you're using an ImageIcon (which deals with one image at a time) is relevant to whether an ArrayList is a good idea. 我不认为您使用ImageIcon(一次处理一个图像)这一事实与ArrayList是否是一个好主意无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM