1

so as above i need to upload more the one image to my database and i'm using Jfilechooser to select my files my code works fine for only one image at a time and i have a blob column to save my images in it

the code for my sql query

try {

        PreparedStatement pst =null;
        ResultSet rst=(ResultSet) pst;

        Connection con=(Connection)    DriverManager.getConnection("jdbc:mysql://localhost/iqari", "root","");
        String sql="UPDATE first SET test = ? WHERE   id  = ?";
        pst=(PreparedStatement) con.prepareStatement(sql);
           int row = masterTable.getSelectedRow();

        Object d =   masterTable.getValueAt(row, 0);
        pst.setBytes(1,person_image);
        pst.setObject(2,d);
        pst.execute();
        JOptionPane.showMessageDialog(mainPanel, "done");
    } catch (Exception e) {


        System.out.println(e.getMessage());
    }

the code for attaching my image EDITED

    JFileChooser chooser =new JFileChooser();
    chooser.setMultiSelectionEnabled(true);
    chooser.showOpenDialog(null);

    File[] files = chooser.getSelectedFiles();

    for (File f : files) {
   filename = f.getAbsolutePath();
   try
   {
    File image2 = new File(filename);
    FileInputStream fis = new FileInputStream(image2);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] buf = new byte[1024];

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

     person_image = bos.toByteArray();
    }
     catch (Exception e) {
    JOptionPane.showMessageDialog(null, e);
     }
  }

any help can be useful guys.

ehab
  • 129
  • 1
  • 9

1 Answers1

0

The reason you're getting an error when you're using .getSelectedFiles() is because it's returning an Array of objects, not a singular object. Try something like this;

Updated code, I cannot test this..

File[] files = chooser.getSelectedFiles();

// instantiates an array of byte arrays of size files.length
byte[][] images = new byte[files.length][];
int numberOfImages = 0;
for (File f : files) {
    filename = f.getAbsolutePath();
    try 
    {
        File image2 = new File(filename);
        FileInputStream fis = new FileInputStream(image2);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];

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

        person_image = bos.toByteArray();

        // adds the byte array to your array & increases the numberOfImages
        images[numberOfImages++] = person_image;
    } 
    catch (Exception e) {
        JOptionPane.showMessageDialog(null, e);
    }
}

In your original SQL code change this part;

pst = (PreparedStatement) con.prepareStatement(sql);
int row = masterTable.getSelectedRow();

Object d = masterTable.getValueAt(row, 0);
pst.setBytes(1,person_image);
pst.setObject(2,d);
pst.execute();

To this;

for(byte[] imageAsByteArray : images) {
    pst = (PreparedStatement) con.prepareStatement(sql);

    int row = masterTable.getSelectedRow();
    Object d = masterTable.getValueAt(row, 0);

    pst.setBytes(1, imageAsByteArray);
    pst.setObject(2, d);
    pst.execute();
}
ambs
  • 487
  • 3
  • 7
  • i'll try it now just a sec please. – ehab Oct 15 '16 at 23:40
  • seems like i can't use foreach because i'm working with netbeans jdk 6 and it may need jdk 8 – ehab Oct 15 '16 at 23:45
  • 1
    sorry my bad just a sec – ehab Oct 15 '16 at 23:52
  • no errors now but saves only one image and the other doesn't show in the column – ehab Oct 15 '16 at 23:57
  • could you edit your original post with the code you're using? I have a feeling you're only sending `person_image` once to your table – ambs Oct 15 '16 at 23:58
  • JFileChooser chooser =new JFileChooser(); chooser.setMultiSelectionEnabled(true); chooser.showOpenDialog(null); File[] files = chooser.getSelectedFiles(); for (File f : files) { filename = f.getAbsolutePath();...... the rest is as your code exactly – ehab Oct 16 '16 at 00:04
  • that's fine, but what i'm saying is you are assigning your `byte` array to `person_image` what are you doing with that? are you sending it within the `for()` loop or out of it? – ambs Oct 16 '16 at 00:05
  • should i change any thing in my sql query? – ehab Oct 16 '16 at 00:06
  • see my edit soon, please read and understand it.. it's really hard to help you without knowing the structure of your application – ambs Oct 16 '16 at 00:11
  • alright thank you my friend i would give a +1 but i need more points – ehab Oct 16 '16 at 00:14
  • no problem, i'm happy to help..if i answered your question, just mark it as so...no need for a +1 – ambs Oct 16 '16 at 00:27
  • :\ sorry nothing worked this time Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException – ehab Oct 16 '16 at 00:39
  • a `NullPointerException` is generally easy to diagnose, have a look up about them. Debug your application, run through it step-by-step and see what's causing the issue. – ambs Oct 16 '16 at 00:41
  • catch clause gives a null now and no image was able to be saved – ehab Oct 16 '16 at 00:54