如何在单击鼠标时将图像从jTable显示到jLabel

时间:2018-07-29 22:35:42

标签: sql jtable jlabel

我想在单击鼠标以显示jTable到jLabel的图像时显示更新信息。我正在使用MS SQL Server2008。我已使用以下代码:

byte[] img = (byte[]) (model.getValueAt(selectedRow, 6)); ImageIcon imageIcon = new ImageIcon (new ImageIcon(img).getImage().getScaledInstance(selected_img_lbl.getWidth(),selected_img_lbl.getHeight(),Image.SCALE_SMOOTH)); selected_img_lbl.setIcon(imageIcon);

此处6是图像列号。我收到此错误:

  

线程“ AWT-EventQueue-0”中的异常java.lang.ClassCastException:无法将java.lang.String强制转换为[B

我已使用此代码获取图像文件:

JFileChooser chooser = new JFileChooser();
    chooser.showOpenDialog(null);
    File f = chooser.getSelectedFile();
    filename = f.getAbsolutePath();
    ImageIcon imageIcon = new ImageIcon (new ImageIcon(filename).getImage().getScaledInstance(lib_pic_lbl.getWidth(),lib_pic_lbl.getHeight(),Image.SCALE_SMOOTH));
    lib_pic_lbl.setIcon(imageIcon);
    try {
        File image = new File(filename);
        FileInputStream fis = new FileInputStream(image);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        for(int readNum; (readNum = fis.read(buf)) != -1;){
            bos.write(buf,0,readNum);
        }
        lib_image = bos.toByteArray();

    } catch (Exception e) {
    }

并用于将数据插入SQL数据库表:

String query = "Insert into librarian (L_name,L_password,L_email,L_address,L_contact,L_image,L_sex) values(?,?,?,?,?,?,?)";
               PreparedStatement pst = connection.prepareStatement(query);
               pst.setString(1, lib_name);
               pst.setString(2, lib_pass);
               pst.setString(3, lib_email);
               pst.setString(4, lib_add);
               pst.setString(5, lib_contact);
               pst.setBytes(6, lib_image);
               pst.setString(7,gender);
               pst.execute();

JOptionPane.showMessageDialog(admin_page.this,“信息已保存!”,“成功”,JOptionPane.INFORMATION_MESSAGE);

请帮助我如何解决该问题。谢谢。

0 个答案:

没有答案