在JAVA数据库 - Netbeans中存储和查看图像 [英] Store and view images in JAVA database - Netbeans

查看:461
本文介绍了在JAVA数据库 - Netbeans中存储和查看图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用JAVA和Netbeans创建一个简单的照片目录。我有基本的Swing布局都可以,我可以存储和检索文本/数字等/从数据库,增量记录等。这是所有罚款。

I am creating a simple photo catalogue using JAVA and Netbeans. I have the basic Swing layout all OK and I can store and retrieve text/number etc from/to the database, delta records etc etc. This is all Fine.

I我真的不知道如何去存储图像并在jFrame中查看它们。

I am really not sure how to go about storing the images and viewing them in the jFrame.

我想将图像存储为文件路径我想(相对于BLOB )我已经读过这是更好?

I want to store the images as a file path I think (as opposed to BLOB) as I have read this is better?

我使用getText和setText显示表单上的数据库条目目前,这是没有问题。如下:

I am using getText and setText to display the database entries on the form at present and this is all fine. Like below:

String location = rs.getString("Location");

textLocation.setText(location);

我正在使用JavaDB derby数据库连接。

I am using the JavaDB derby database connection.

我已经搜索过一个教程,但没有太多的运气。有图像的tutees等,但不是真正的如何存储/从数据库检索他们。

I have searched for a tutorial but have not had much luck. There are tutees fro images etc but not really how to store/retrieve them from a database.

如果任何人可以指向正确的方向或一个很好的教程覆盖。

If anyone can point me in the right direction or to a good tutorial covering this that would be grand.

* UPDATE *

感谢您的帮助。

设置图片文件夹的路径:

Set path to location of images folder:

private static final String PATH = "/images/";

分配字符串

String image = rs.getString("ImageURL");

将图片添加到标签

image_label.setIcon(new javax.swing.ImageIcon(getClass().getResource(PATH + image)));

再次感谢。

推荐答案

需要考虑的因素(如果您的图片文件是嵌入式资源,而不依赖于您的文件系统)

Things to consider, (if your image files are embedded resources, and not dependent on your file system)


  1. 如何存储图像路径?您可以简单地存储文件名 image.png

。有一个设置路径(不包括图像文件名),并根据您的应用程序文件结构,这是您将使用的路径,与图像文件名称连接。示例

What complete file path will you ultimately use. Have a set path (excluding the image file name), and based off your application file structure that is the path you will use, concatenated with with image file name. Example

ProjectRoot
         src
            resources
                   images
                        image.png

private static final String PATH = "/resources/images/";


  • 如何加载图片。你应该从类路径中读取图像,使用 getClass()。getResource(),你可以加载图像到 ImageIcon 并最终将它们添加到 JLabel 。示例

  • How will you load the image. You should read the image from the class path, using getClass().getResource() and you can just load the images to an ImageIcon and eventually adding them to JLabel. Example

    String location = rs.getString("Location");
    Image image = ImageIO.read(getClass().getResource(PATH + location));
    ImageIcon icon = new ImageIcon(image);
    JLabel label = new JLabel(icon);
    // add label to something.
    


  • 这篇关于在JAVA数据库 - Netbeans中存储和查看图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆