0

I am trying to store an image into db along with a string.i used the code given below

 File image = new File("c:/a1.jpg");
 String d=jTextField1.getText();
FileInputStream  fis 
 try {
        psmnt = con.prepareStatement("insert into c(name,pic) "+ "values(?,?)");
         psmnt.setString(1,d);
        fis = new FileInputStream(image);
         psmnt.setBinaryStream(2,fis,(int) (image.length()));
        psmnt.executeUpdate();
     }
catch().....

I tried a lot.But still am getting error " unimplemented or unreasonable conversion requested" .Can anyone help to solve this?pic is declared as blob datatype.Thank you.

spc
  • 131
  • 5
  • 19
  • possible duplicate of [Storing Image in Data Base Using Java in Binary Format](http://stackoverflow.com/questions/21500339/storing-image-in-data-base-using-java-in-binary-format) – ravibagul91 Feb 02 '14 at 04:54

1 Answers1

0
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {



      try {  
         String sid="orcl";
         String username="user";
         String password= "pass";
        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());  
         Connection connection = DriverManager.getConnection(  
                "jdbc:oracle:thin:@localhost:1521:"+sid, username, password);  

        //Statement statement = connection.createStatement();  
           File image = new File("c:/a1.jpeg");
           String d= "the pics";
           FileInputStream  fis ;
           fis = new FileInputStream(image);

          String query = " insert into c(name,pic)values(?,?)";
          PreparedStatement preparedStmt = connection.prepareStatement(query);
           //System.out.println("Data is inserted:");
           preparedStmt.setString(1, d);
           preparedStmt.setBinaryStream(2,fis,(int) (image.length()));

            // execute the preparedstatement
           preparedStmt.execute();
           connection.close(); 
   } catch (Exception e) {  
        e.printStackTrace();  
     }  
   System.out.println("Data is inserted:");

}

I suppose you have added oracle jar ojdbc14.jar in your project

soulemane moumie
  • 1,005
  • 1
  • 14
  • 26
  • I did try and it is working in my pc. The problem is definitely with oracle especially typecasting ,you may try psmnt.setBinaryStream(2,fis,(byte) (image.length())); instead of psmnt.setBinaryStream(2,fis,(int) (image.length())); – soulemane moumie Feb 03 '14 at 19:17
  • replace int with byte in the setBinaryStream – soulemane moumie Feb 03 '14 at 19:18
  • Yes sir I have done.Now it shows java.lang.NegativeArraySizeException at oracle.jdbc.driver.OraclePreparedStatement.setBinaryStream(OraclePreparedStatement.java:2550).I checked the fis,Image.length.These are not empty – spc Feb 07 '14 at 06:25
  • @spc have a look here http://stackoverflow.com/questions/11207897/negative-array-size-exception – soulemane moumie Feb 08 '14 at 06:50
  • try this byte bytes[]=new byte[(int)image.length()]; preparedStmt.setBinaryStream(2,fis,bytes.length); – soulemane moumie Feb 08 '14 at 11:58