如何编辑/更新BLOB图像(PNG)

时间:2012-10-28 11:41:16

标签: java sqlite blob

我在创建编辑BLOB图像的方法时遇到问题。您可以在下面找到我的代码(所有代码都缩短了);

String edit=txt_gid.getText();
**byte[] e = icon_image1;**
 String sql="update GamesCollector set FrontCover='"+e+"'where GameId='"+edit+"' ";
    ps=conn.prepareStatement(sql);
    ps.execute();

    JOptionPane.showMessageDialog(null, "Data Edited");
    Browse.UpdateTable();

这就是我在JLabel(Swing组件)上显示图像的方式;

 public static ImageIcon pic1=null;

 String sql="select * from GamesCollector where Title=?";
        ps=conn.prepareStatement(sql);


        ps.setString(1, txt_search.getText().toUpperCase().trim());
        rs=ps.executeQuery();

 if(rs.next()){

byte[] datapic = rs.getBytes("FrontCover");
            pic1 = new ImageIcon(datapic);
            front.setIcon(pic1);}

这是导致问题的代码;

static void Edit(){


 try{



        String edit=txt_gid.getText();

        String edit1=txt_title.getText().toUpperCase().trim();
        String edit2=cmb_platform.getSelectedItem().toString();
        if(edit2==null){edit2="";}

        String edit3=cmb_format.getSelectedItem().toString();
        if(edit3==null){edit3="";}

        String edit4=list_genres.getSelectedValue().toString();
        if(edit4==null){edit4="";}

        String edit5=((JTextField)pck_releasedate.getDateEditor().getUiComponent()).getText(); 
        String edit6=txt_desc.getText().toUpperCase().trim();
        String edit7=txt_developer.getText().toUpperCase().trim();
        String edit8=txt_publisher.getText().toUpperCase().trim();
        String edit9=txt_series.getText().toUpperCase().trim();
        String edit10=txt_barcode.getText().toUpperCase().trim();
        String edit11=collection;
        String edit12=cmb_esrb.getSelectedItem().toString();
        if(edit12==null){edit12="";}

        String edit13=cmb_pegi.getSelectedItem().toString();
        if(edit13==null){edit13="";}

        String edit14 = Integer.toString(rating_slider.getValue());

        **byte[] e = icon_image1;**



        String sql="update GamesCollector set Title=='"+edit1+"',Platform='"+edit2+"',Format='"+edit3+"',Genres='"+edit4+"',ReleaseDate='"+edit5+"',Description='"+edit6+"',Developer='"+edit7+"',Publisher='"+edit8+"',Series='"+edit9+"',Barcode='"+edit10+"',Collection='"+edit11+"',ESRB='"+edit12+"',PEGI='"+edit13+"',Rating='"+edit14+"',FrontCover='"+e+"'   where GameId='"+edit+"' ";
        ps=conn.prepareStatement(sql);
        ps.execute();

        JOptionPane.showMessageDialog(null, "Data Edited");
        Browse.UpdateTable();


    }

    catch(Exception e){
        JOptionPane.showMessageDialog(null, e);
    }
    finally {

        try{
            rs.close();
            ps.close();

        }

        catch(Exception e){}

    }

  }

}

此方法用于获取路径并将其转换为ByteArray;

protected static void AttachImage(){

    JFileChooser chooser= new JFileChooser("D:\\Downloads\\Software Development\\GameCollector\\Images\\Game");
    chooser.showOpenDialog(null);
    File s=chooser.getSelectedFile();
    filename = s.getAbsolutePath();


    try{
    File image = new File(filename);
    FileInputStream fis = new FileInputStream(image);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    byte[] bug = new byte[1024];

    for(int readNum; (readNum=fis.read(bug)) !=-1;){
        baos.write(bug,0,readNum);        
    }

     icon_image1=baos.toByteArray();
     icon_image2=baos.toByteArray();

    }
    catch(Exception e)
    {
        JOptionPane.showMessageDialog(null, e);
    }
 }

此方法用于保存文件;

static void Save(){

   try{

        String sql="insert into GamesCollector (GameId,Title,Platform,Format,Genres,ReleaseDate,Description,Developer,Publisher,Series,Barcode,Collection,ESRB,PEGI,Rating,FrontCover,BackCover) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        ps=conn.prepareStatement(sql);

        ps.setString(1, txt_gid.getText().toUpperCase().trim());
        ps.setString(2, txt_title.getText().toUpperCase().trim());
        String platform=cmb_platform.getSelectedItem().toString();
        if(platform==null){platform="";}
        ps.setString(3, platform);
        String format=cmb_format.getSelectedItem().toString();
        if(format==null){format="";}
        ps.setString(4, format);
        String genres=list_genres.getSelectedValue().toString();
        if(genres==null){genres="";}
        ps.setString(5, genres);
        ps.setString(6, ((JTextField)pck_releasedate.getDateEditor().getUiComponent()).getText());
        ps.setString(7, txt_desc.getText().toUpperCase().trim());
        ps.setString(8, txt_developer.getText().toUpperCase().trim());
        ps.setString(9, txt_publisher.getText().toUpperCase().trim());
        ps.setString(10, txt_series.getText().toUpperCase().trim());
        ps.setString(11, txt_barcode.getText().toUpperCase().trim());

        ps.setString(12, collection);

        String esrb=cmb_esrb.getSelectedItem().toString();
        if(esrb==null){esrb="";}
        ps.setString(13, esrb);
        String pegi=cmb_pegi.getSelectedItem().toString();
        if(pegi==null){pegi="";}
        ps.setString(14, pegi);
        int rating = rating_slider.getValue();
        ps.setString(15, Integer.toString(rating));
        ps.setBytes(16, icon_image1);
        ps.setBytes(17, icon_image2);

        ps.execute();

        JOptionPane.showMessageDialog(null, "Data Saved");
       }

    catch(Exception e){
        JOptionPane.showMessageDialog(null, e);
    }
    finally {

        try{
            rs.close();
            ps.close();

        }

        catch(Exception e){}

    }
  }

当我检索数据时,一切正常。

此方法用于编辑数据;

static void Edit(){
   try{
        String edit=txt_gid.getText();

        String edit1=txt_title.getText().toUpperCase().trim();
        String edit2=cmb_platform.getSelectedItem().toString();
        if(edit2==null){edit2="";}

        String edit3=cmb_format.getSelectedItem().toString();
        if(edit3==null){edit3="";}

        String edit4=list_genres.getSelectedValue().toString();
        if(edit4==null){edit4="";}

        String edit5=((JTextField)pck_releasedate.getDateEditor().getUiComponent()).getText(); 
        String edit6=txt_desc.getText().toUpperCase().trim();
        String edit7=txt_developer.getText().toUpperCase().trim();
        String edit8=txt_publisher.getText().toUpperCase().trim();
        String edit9=txt_series.getText().toUpperCase().trim();
        String edit10=txt_barcode.getText().toUpperCase().trim();
        String edit11=collection;
        String edit12=cmb_esrb.getSelectedItem().toString();
        if(edit12==null){edit12="";}

        String edit13=cmb_pegi.getSelectedItem().toString();
        if(edit13==null){edit13="";}

        String edit14 = Integer.toString(rating_slider.getValue());

        byte[] e = icon_image1;



        String sql="update GamesCollector set Title=='"+edit1+"',Platform='"+edit2+"',Format='"+edit3+"',Genres='"+edit4+"',ReleaseDate='"+edit5+"',Description='"+edit6+"',Developer='"+edit7+"',Publisher='"+edit8+"',Series='"+edit9+"',Barcode='"+edit10+"',Collection='"+edit11+"',ESRB='"+edit12+"',PEGI='"+edit13+"',Rating='"+edit14+"',FrontCover='"+icon_image1+"'   where GameId='"+edit+"' ";
        ps=conn.prepareStatement(sql);
        ps.execute();

        JOptionPane.showMessageDialog(null, "Data Edited");
        Browse.UpdateTable();


    }

    catch(Exception e){
        JOptionPane.showMessageDialog(null, e);
    }
    finally {

        try{
            rs.close();
            ps.close();

        }

        catch(Exception e){}

    }

  }

编辑后,该方法将不再检索任何数据。

我正在编辑数据时保存数据。问题只发生在我在图形界面上显示时。除了显示空白区域的图像外,将显示所有数据。

我如何解决我遇到的问题?

1 个答案:

答案 0 :(得分:0)

您正尝试将图像放入SQL命令字符串:

String sql="update ... set ... FrontCover='"+icon_image1+"' ...

这不适用于二进制数据。

要确保二进制数据正确进入数据库,请使用参数,例如Save方法。