0

假设我有这样的代码:

    import java.awt.*;
    import java.awt.event.*;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import javax.swing.*;

    public class InsertImg2Dbase extends JFrame implements ActionListener{

        JButton open = new JButton("Open image to save...");
        private Connection con;

        public void getConnection(){
            try{
                if(con==null){
                    Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
                    con=DriverManager.getConnection("jdbc:db2://localhost:50001/sample","username","password");
                }
            }catch (SQLException e){
                JOptionPane.showMessageDialog(null, e.getMessage());
            }catch (Exception e){
                JOptionPane.showMessageDialog(null, e);
            }
        }

        public void actionPerformed(ActionEvent e){
            Object source = e.getSource();
            if(source==open){
              // this is where the event when opening the file to be saved will be coded  
            }
        }

        public InsertImg2Dbase(){
            ActionListener al = new InsertImg2Dbase();
            open.setBounds(20, 20, 175, 25);
            open.addActionListener(al);
            add(open);
        }

        public void properties(){
            setLayout(null);
            setTitle("Open Image To Save");
            setResizable(false);
            setSize(220,90);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationRelativeTo(null);   
            setVisible(true);
        }

        public static void main(String[] args){
            InsertImg2Dbase ins = new InsertImg2Dbase();
            ins.properties();
        }    
    }

我应该在代码中添加什么,以便 JButton 将打开一个资源管理器以打开一个图像文件(例如 jpeg、png、gif、bmp),该文件将作为 blob 保存到我的数据库(比如我的表名是:“images”)文件。我可以添加一个函数来调整我的图像大小,比如说在保存到我的数据库之前的尺寸为 300x650?

我欢迎任何帮助,我仍在学习过程中,如果您能指导我,我将不胜感激。欢迎任何帮助!谢谢

4

1 回答 1

0

您可以在 DB2 中加入一个外部工具,以扩展基本功能并添加对特定事物的支持,例如您的案例图像。为此,您需要 ImageMagik,然后创建一些外部存储过程,就是这样。

DeveloperWorks 中有一个非常好的教程:http: //www.ibm.com/developerworks/data/library/techarticle/dm-0504stolze/

完成此操作后,您可以调用 db2 过程,在检索到应用程序之前操作存储的图像。

于 2013-03-04T16:44:47.487 回答