0

What I did is, I created a new table in oracle database with image_id with numeric datatype and image_scr with blob. So, I wanted to insert image into the table from java.jframe with jdbc driver connected.

in below code I have defined photo as: byte[] photo=null; and filename as: String filename= null;

This is code for getting image from my computer

JButton btnNewButton = new JButton("Change picture");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            JFileChooser chooser = new JFileChooser();
            chooser.showOpenDialog(null);
            File f = chooser.getSelectedFile();
            lblNewLabel_2.setIcon(new ImageIcon(f.toString()));
            filename = f.getAbsolutePath();
            textField_5.setText(filename);
            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);   
                }
                photo= bos.toByteArray();
                fis.close();
            }catch(Exception ex)
            {
                ex.printStackTrace();
            }



        }
    });

This is database query code

JButton btnNewButton_2 = new JButton("insert");
    btnNewButton_2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            try{
                String query ="insert into stu_images(student_id,stu_image)values(01,'"+photo+"')";
                PreparedStatement pstmt = connection.prepareStatement(query);
                pstmt.executeQuery();
            }catch(Exception ex)
            {
                ex.printStackTrace();
            }
        }
    });

1 Answers1

0

Here's what you should do.

Step 1. To understand why your code id failing, print out the value of the query string ... and look at it. It will not be what you expect. It certainly won't be valid SQL. (That's what the ORA message is telling you.)

Step 2. To understand the correct way to do this, read up about how to use PreparedStatement, queries with placeholders in them, and the PreparedStatement::setBytes and PreparedStatement::setBlob methods.

The short version is ... don't use string bashing to add parameters values to SQL query strings.

References:

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216