Java - JDBC / Swing - executeUpdate不使用BLOB

时间:2016-12-21 02:56:29

标签: java swing jdbc blob

我遇到了一个问题,据我所知,整个代码一直有效,直到pst.executeQuery我才能看出为什么会出现这个问题。我有一个更新/插入方法,没有相关的问题。我正在使用标签来显示图像,因此它会photoLabel.setIcon();还是我完全偏离轨道。我有一个按钮用于文件选择器,它将图像加载到标签中,保存按钮(此功能)只写入名为Images的12列/字段中的数据库。

请注意 - System.out.Println用于测试目的,只是

System.out.println("Working 5");

不会显示,因此我知道为什么它与pst.executeQuery()有关。我尝试过在网上搜索,使用了不同的.execute方法,我尝试过提交连接等等。唉没有运气。

@Override
public void actionPerformed(ActionEvent e) {
    Connection connection = null;

    Statement statement = null;
    ResultSet rs = null;
//  String s = null;
    try {

        Class.forName("org.sqlite.JDBC");
        connection = DriverManager.getConnection("jdbc:sqlite:employeeDatabase.sqlite");
        //connection.setAutoCommit(false);
        System.out.println("Working 1");
         InputStream is = new FileInputStream(new File(s));
        System.out.println("Working 2");
         String sql = "insert into employees(Images) values(?)";
         PreparedStatement pst = connection.prepareStatement(sql);
            System.out.println("Working 3");
           pst.setBlob(12, is);

            System.out.println("Working 4");


           pst.executeQuery();
           connection.commit();
        System.out.println("Working 5");
           JOptionPane.showMessageDialog(null, "Data Inserted");


}
    }

        catch ( Exception e1 ) {


            JOptionPane.showMessageDialog(null, "Error");

        }
}});

这是通过使用JFileChooser

选择图像的方法
uploadImage.addActionListener(new ActionListener() {

           public void actionPerformed(ActionEvent e){
                 JFileChooser fileChooser = new JFileChooser();
                 fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
                 FileNameExtensionFilter filter = new FileNameExtensionFilter("*.JPG", "jpg","gif","png");      
                 fileChooser.addChoosableFileFilter(filter);
                fileChooser.addChoosableFileFilter(filter);
                 fileChooser.setMultiSelectionEnabled(false);
                 int result = fileChooser.showSaveDialog(null);
                 if(result == JFileChooser.APPROVE_OPTION){
                     File selectedFile = fileChooser.getSelectedFile();
                     String path = selectedFile.getAbsolutePath();
                     photoLabel.setIcon(ResizeImage(path));
                     s = path;
                      }


                 else if(result == JFileChooser.CANCEL_OPTION){
                     System.out.println("No Data");
                 }
             }
            });

编辑 -

不工作我的意思是,我没有错误,程序没有中断。只是图像不会上传到数据库,代码System.out.println("Working 5");也不会打印到控制台。所以它似乎在那时被卡住/冻结了。

1 个答案:

答案 0 :(得分:2)

首先,仔细检查表格employees和列Images的表格结构是否正确

然后,在准备好的语句中设置正确的占位符,只有一个

pst.setBlob(1, ...

然后,使用这种方法

private byte[] readFile(String file) {
    ByteArrayOutputStream bos = null;
    try {
        File f = new File(file);
        FileInputStream fis = new FileInputStream(f);
        byte[] buffer = new byte[1024];
        bos = new ByteArrayOutputStream();
        for (int len; (len = fis.read(buffer)) != -1;) {
            bos.write(buffer, 0, len);
        }
    } catch (FileNotFoundException e) {
        System.err.println(e.getMessage());
    } catch (IOException e2) {
        System.err.println(e2.getMessage());
    }
    return bos != null ? bos.toByteArray() : null;
}

使用

pst.setBytes(1, readFile(s));

最后,请致电

pst.executeUpdate();

来源:http://www.sqlitetutorial.net/sqlite-java/jdbc-read-write-blob/