简体   繁体   中英

How to edit/Update BLOB image (PNG)

I'm having problems with a method I created to edit a BLOB image. You can find my code bellow (all code is shortened);

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();

This is how I display an image on a JLabel (Swing component);

 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);}

This is the code which is causing a problem;

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){}

    }

  }

}

This method is used to get the path and to convert it into a 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);
    }
 }

This method is used to save the file;

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){}

    }
  }

When I'm retrieving the data everything works fine.

This method is used to edit the data;

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){}

    }

  }

After an edit, the method won't retrieve any data anymore.

The data is saved when I'm editing it. The problem only occurs when I'm showing it on my graphical interface. All the data will be shown except for the image which shows me a blank area.

How do I solve the issue I'm having?

You are trying to put the image into the SQL command string:

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

This does not work with binary data.

To ensure that binary data goes into the database correctly, use parameters, like in your Save method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM