this is the code I create to import pics

select image:

JFileChooser chooser = new JFileChooser();
        chooser.showOpenDialog(null);
        File f = chooser.getSelectedFile();
        filename = f.getAbsolutePath();

       file_picker1.setText(filename);


       try{

           File image = new File (filename);
           FileInputStream fis = new FileInputStream(filename);

          ByteArrayOutputStream  bos = new ByteArrayOutputStream();

           byte[] buf = new byte[1024];

           for (int readNum; (readNum=fis.read(buf))!=-1; ){

               bos.write(buf,0,readNum);

           }
            bill_image = bos.toByteArray();


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

save it in SQL :

 try{
                String sql= "INSERT INTO Pics (Invoicenumber, Photo) VALUES (?,?)";
                pst=conn.prepareStatement(sql);
                pst.setString(1, LBL2.getText());
                pst.setBytes(2, bill_image);

                pst.execute();
                JOptionPane.showMessageDialog(null, "Data saved");

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



            }finally{
            try{
             rs.close();
             pst.close();                
            }
              catch(Exception e){        
                                }    

            }

but now to retrieve those pics?
I've made a simple code but it doesnt work if the pics has the some InvoiceNumber.
this is the code I use to get pics:

 String b = jTextField1.getText();
          int row = jTable1.getSelectedRow();

            String Table_click1 =(jTable1.getModel().getValueAt(row, 0).toString());

       try{
       String sql = "Select Photo from Pics where InvoiceNumber='"+Table_click1+"' ";

       pst = conn.prepareStatement(sql);
       rs = pst.executeQuery();

       if(rs.next()){
       byte[] imagedata =rs.getBytes("Photo");
       format = new ImageIcon(imagedata);



        ImIc.setIcon(format);



       }

       }catch(Exception e){

       }

if pics has different Invoice Number it show the pics. but if has the some it show only the first row pic.

I want to give the user the chance to import as much pics as he want for the products and retrieve those. not all in the some time

Recommended Answers

All 3 Replies

if you have multiple results, the ResultSet will have multiple rows in it. Iterate over those rows, rather than just picking the first one and discarding the rest.
so something like

while (rs.next()) {
//whatever
}

I put while, and it show the 2nd row image. select the first row. it show the pic of the 2nd, select the 3rd show the 2nd again. select a row with a different Invoice Number, it show the pic, but when select a row with some id, it still show the 2nd row image

you do need to THINK about what you do inside that while...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.