java - 使用 Eclipse SWT Image、JAVA 和 SQLite 插入、存储和检索图像

标签 java eclipse image sqlite swt

我正在编写一个基本的 Java 应用程序,允许用户将有关个人的详细信息插入到 SQLite 数据库中。我将 Eclipse SWT 用于 GUI。

Eclipse SWT 定义了一个图像类型 (org.eclipse.swt.graphics.Image) 用于在 GUI 中显示图像。

我试图让用户浏览文件系统,选择图像,然后将该图像插入数据库。我还希望能够从数据库中检索该图像并将其显示在 GUI 中。

一切都非常简单,但对于我来说,我无法让它发挥作用!我也进行了很多搜索,但似乎无法找到解决方案。

我正在使用面向 Java 开发人员的 Eclipse IDE (3.6)、sqlite-jdbc-3.7.2.jar 和 JDK 1.6.0_22。

import org.eclipse.swt.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.graphics.*;

import java.sql.*;

/***********************************************************************/
/*** Tests reading and writing SWT Images from an SQLite Database    ***/
/***********************************************************************/
public class ImageTest {

    Shell shell;

    //Variables to store the current values when editing
    private Canvas personPhoto;
    private Image personImage;
    private int personID = 1;

    private double photoWidth = 100;
    private double photoHeight = 100;

    //Database connection and statement variables
    private static Connection connection = null;
    private static Statement statement = null;
    private static PreparedStatement ps = null;
    private static ResultSet rs = null;

    public ImageTest(Shell parent, Connection passedConnection) {
        shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.PRIMARY_MODAL);
        shell.setLayout(new GridLayout());
        connection = passedConnection;
    }

    private void createControlButtons() {
        Composite composite = new Composite(shell, SWT.NONE);
        composite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
        GridLayout layout = new GridLayout();
        layout.numColumns = 2;
        composite.setLayout(layout);

        Button okButton = new Button(composite, SWT.PUSH);
        okButton.setText("OK");
        okButton.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                if(personID > 0){
                    try {
                        PreparedStatement ps = connection.prepareStatement("UPDATE person SET photo = ? " +
                                                                                              "WHERE person_id = ?");
                        ps.setBytes(1, personImage.getImageData().data);
                        ps.setInt(2, personID);
                        ps.executeUpdate();
                        ps.close();
                    } catch (SQLException err) {
                        err.printStackTrace();
                    }
                } else {
                    try {
                        PreparedStatement ps = connection.prepareStatement("INSERT INTO person (photo) VALUES (?)");
                        ps.setBytes(1, personImage.getImageData().data);
                        ps.executeUpdate();
                        ps.close();
                    } catch (SQLException err) {
                        err.printStackTrace();
                    }
                }               
                shell.close();
            }
        });

        Button cancelButton = new Button(composite, SWT.PUSH);
        cancelButton.setText("Cancel");
        cancelButton.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                shell.close();
            }
        });

        shell.setDefaultButton(okButton);
    }

    private void createTextWidgets(final Display display) {

        GridLayout gridLayout = new GridLayout();
        gridLayout.numColumns = 2;
        shell.setLayout(gridLayout);
        new Label(shell, SWT.NONE).setText("Photo:");

        personPhoto = new Canvas(shell, SWT.BORDER);
        GridData gridData = new GridData(GridData.FILL, GridData.FILL, true, true);
        gridData.widthHint = (int)photoWidth;
        gridData.heightHint = (int)photoHeight;
        gridData.verticalSpan = 5;
        gridData.horizontalSpan = 2;
        personPhoto.setLayoutData(gridData);
        personPhoto.redraw();

        personPhoto.addPaintListener(new PaintListener() {
            public void paintControl(final PaintEvent event) {
                if (personImage != null) {
                    event.gc.drawImage(personImage, 0, 0);
                }
            }
        });

        //Skip a Column
        new Label(shell, SWT.NONE);

        Button browse = new Button(shell, SWT.PUSH);
        browse.setText("Browse...");
        gridData = new GridData(GridData.FILL, GridData.CENTER, true, false);
        gridData.horizontalIndent = 5;
        browse.setLayoutData(gridData);
        browse.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent event) {
                String fileName = new FileDialog(shell).open();
                if (fileName != null) {
                    personImage = new Image(display, fileName);
                    personPhoto.redraw();
                }
            }
        });

        Button delete = new Button(shell, SWT.PUSH);
        delete.setText("Delete");
        gridData = new GridData(GridData.FILL, GridData.BEGINNING, true, false);
        gridData.horizontalIndent = 5;
        delete.setLayoutData(gridData);
        delete.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent event) {
                if (personImage != null) {
                    personImage.dispose();
                    personImage = null;
                    personPhoto.redraw();
                }
            }
        });

        //Skip a Column
        new Label(shell, SWT.NONE);

        //Skip two Rows
        new Label(shell, SWT.NONE);
        new Label(shell, SWT.NONE);
        new Label(shell, SWT.NONE);
        new Label(shell, SWT.NONE);     
    }

    public void open() {
        Display display = shell.getDisplay();
        //To avoid null pointer exceptions
        personImage = new Image(display,"user.png");

        try{
            PreparedStatement ps = connection.prepareStatement("SELECT photo FROM person WHERE person_id = ?");
            ps.setInt(1, personID);
            rs = ps.executeQuery();
            while (rs.next()) {
                //dispose of the current image
                personImage.dispose();
                personImage = new Image(display, (int) photoWidth, (int) photoHeight);
                //Retrieve the photo for this person
                personImage.getImageData().data = rs.getBytes("photo");
            }
            ps.close();
            rs.close();    
        } catch (SQLException e) {
            e.printStackTrace();
        }

        createTextWidgets(display);
        createControlButtons();
        shell.pack();
        shell.open();
        while(!shell.isDisposed()){
            if(!display.readAndDispatch())
                display.sleep();
        }
    }
}

我现在已经简化了代码,但我仍然无法正确地从数据库中提取字节数组并将其显示为 SWT 图像。有人有什么想法吗?非常感谢任何帮助!

谢伊

    /* Imports */
import org.eclipse.swt.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.graphics.*;

import java.sql.*;

/***********************************************************************/
/*** Tests reading and writing SWT Images from an SQLite Database    ***/
/***********************************************************************/
public class ImageTest {

    Shell shell;

    //Variables to store the current values when editing
    private Canvas personPhoto;
    private Image personImage;
    private int personID = 1;

    private double photoWidth = 100;
    private double photoHeight = 100;

    //Database connection and statement variables
    private static Connection connection = null;
    private static Statement statement = null;
    private static PreparedStatement ps = null;
    private static ResultSet rs = null;

    public ImageTest(Shell parent, Connection passedConnection) {
        shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.PRIMARY_MODAL);
        shell.setLayout(new GridLayout());
        connection = passedConnection;
    }

    private void createControlButtons() {
        Composite composite = new Composite(shell, SWT.NONE);
        composite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
        GridLayout layout = new GridLayout();
        layout.numColumns = 2;
        composite.setLayout(layout);

        Button okButton = new Button(composite, SWT.PUSH);
        okButton.setText("OK");
        okButton.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                if(personID > 0){
                    try {
                        PreparedStatement ps = connection.prepareStatement("UPDATE person SET photo = ? " +
                                                                                              "WHERE person_id = ?");
                        ps.setBytes(1, personImage.getImageData().data);
                        ps.setInt(2, personID);
                        ps.executeUpdate();
                        ps.close();
                    } catch (SQLException err) {
                        err.printStackTrace();
                    }
                } else {
                    try {
                        PreparedStatement ps = connection.prepareStatement("INSERT INTO person (photo) VALUES (?)");
                        ps.setBytes(1, personImage.getImageData().data);
                        ps.executeUpdate();
                        ps.close();
                    } catch (SQLException err) {
                        err.printStackTrace();
                    }
                }               
                shell.close();
            }
        });

        Button cancelButton = new Button(composite, SWT.PUSH);
        cancelButton.setText("Cancel");
        cancelButton.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                shell.close();
            }
        });

        shell.setDefaultButton(okButton);
    }

    private void createTextWidgets(final Display display) {

        GridLayout gridLayout = new GridLayout();
        gridLayout.numColumns = 2;
        shell.setLayout(gridLayout);
        new Label(shell, SWT.NONE).setText("Photo:");

        personPhoto = new Canvas(shell, SWT.BORDER);
        GridData gridData = new GridData(GridData.FILL, GridData.FILL, true, true);
        gridData.widthHint = (int)photoWidth;
        gridData.heightHint = (int)photoHeight;
        gridData.verticalSpan = 5;
        gridData.horizontalSpan = 2;
        personPhoto.setLayoutData(gridData);
        personPhoto.redraw();

        personPhoto.addPaintListener(new PaintListener() {
            public void paintControl(final PaintEvent event) {
                if (personImage != null) {
                    event.gc.drawImage(personImage, 0, 0);
                }
            }
        });

        //Skip a Column
        new Label(shell, SWT.NONE);

        Button browse = new Button(shell, SWT.PUSH);
        browse.setText("Browse...");
        gridData = new GridData(GridData.FILL, GridData.CENTER, true, false);
        gridData.horizontalIndent = 5;
        browse.setLayoutData(gridData);
        browse.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent event) {
                String fileName = new FileDialog(shell).open();
                if (fileName != null) {
                    personImage = new Image(display, fileName);
                    personPhoto.redraw();
                }
            }
        });

        Button delete = new Button(shell, SWT.PUSH);
        delete.setText("Delete");
        gridData = new GridData(GridData.FILL, GridData.BEGINNING, true, false);
        gridData.horizontalIndent = 5;
        delete.setLayoutData(gridData);
        delete.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent event) {
                if (personImage != null) {
                    personImage.dispose();
                    personImage = null;
                    personPhoto.redraw();
                }
            }
        });

        //Skip a Column
        new Label(shell, SWT.NONE);

        //Skip two Rows
        new Label(shell, SWT.NONE);
        new Label(shell, SWT.NONE);
        new Label(shell, SWT.NONE);
        new Label(shell, SWT.NONE);     
    }

    public void open() {
        Display display = shell.getDisplay();
        //To avoid null pointer exceptions
        personImage = new Image(display,"user.png");

        try{
            PreparedStatement ps = connection.prepareStatement("SELECT photo FROM person WHERE person_id = ?");
            ps.setInt(1, personID);
            rs = ps.executeQuery();
            while (rs.next()) {
                //dispose of the current image
                personImage.dispose();
                personImage = new Image(display, (int) photoWidth, (int) photoHeight);
                //Retrieve the photo for this person
                personImage.getImageData().data = rs.getBytes("photo");
            }
            ps.close();
            rs.close();    
        } catch (SQLException e) {
            e.printStackTrace();
        }

        createTextWidgets(display);
        createControlButtons();
        shell.pack();
        shell.open();
        while(!shell.isDisposed()){
            if(!display.readAndDispatch())
                display.sleep();
        }
    }
}

最佳答案

如果是我,我不会将图像保存到数据库中。首先是因为这样的问题,其次是因为它会使数据库变得非常大。在数据库中包含图像的路径,然后从该路径加载该图像可能更容易。同样,插入只会插入图像文件的路径。

关于java - 使用 Eclipse SWT Image、JAVA 和 SQLite 插入、存储和检索图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4026888/

相关文章:

eclipse - 如何从 Eclipse Mars (4.5.1) 卸载标准插件

eclipse - 以编程方式注册Ecore元模型

c++ - 写入BMP文件后,图片上下颠倒

javascript - 如何从图像的 ul 中获取 img src 属性?

javascript - 在 javascript 中使用正则表达式从缩略图获取 YouTube 视频 ID?

java - 为什么当通过 Web 服务同时创建文档时 libreoffice sdk 会崩溃?

java - 将 org.apache.poi.ss.usermodel.Cell 的 Cellstyle 设置为 boolean 值

java - 从 Java EE6 开始

java - 实现接口(interface)时的对象类型

database - eclipse 月神 : Tool to Create new MySQL database