【问题标题】:Java - JDBC/Swing - executeUpdate Not Working with BLOBJava - JDBC/Swing - executeUpdate 不适用于 BLOB
【发布时间】:2017-05-06 09:09:57
【问题描述】:

我遇到了一个问题,据我所知,整个代码一直有效,直到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"); 不会打印到控制台。所以它似乎在那一点上被卡住/冻结了。

【问题讨论】:

  • 定义不工作
  • @ScaryWombat 我没有收到任何错误,程序没有中断。只是不会将图像上传到数据库和代码 System.out.println("Working 5");不打印到控制台。
  • 在您的代码中,您有一个 try - 您是否忽略了问题?
  • @ScaryWombat 是的,忘记在我的原始帖子中包含 catch 异常。我现在已经添加了。它似乎只能到达pst.executeQuery,然后到达catch

标签: java swing jdbc blob


【解决方案1】:

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

然后,在prepared statement中为输入设置正确的占位符,只有一个

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/

【讨论】:

  • 好吧,你把我弄丢了 首先,在准备好的语句中为输入设置正确的占位符,只有一个。不过谢谢你的回复。 :)
  • 阅读此docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html 这取决于 #of 吗?在声明中。第一个是 1 秒是 2,依此类推。
  • 我已添加代码,但私有字节 [] readFile(Stringfile) = 令牌“readFile”上的语法错误,应为 AnnotationName
  • 老兄,成功了!!!!对不起,我的愚蠢,已经很晚了。 03:36AM.... 享受圣诞假期哈哈哈!谢谢,我接受了你的回答。认真的哥们太感谢了!
  • 很高兴你把它整理好了。节日快乐。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-02
  • 1970-01-01
  • 2021-08-19
  • 2016-03-21
  • 1970-01-01
  • 2017-08-14
  • 1970-01-01
相关资源
最近更新 更多