• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 442
  • Last Modified:

cannot find the error


Hello there,

I get a error when i try to insert image file into oracle db
the data that i insert are
tehran.gif  
C:\eclipseWorkspace\test2\images\tom.gif  
gif
image (the image itself) and
1

and the error is
java.sql.SQLException: ORA-01461: can bind a LONG value only for insert into a LONG column

my java code is this
connect.setAutoCommit(false);
                Statement st = connect.createStatement();
                //System.out.println("after statement");
                PreparedStatement pst =
                      connect.prepareStatement("INSERT INTO cophoto(photo_id,file_name, file_path, file_type, data, co_id) " +
                              "VALUES(photo_id_seq.nextval,?, ?, ?, ?, ?)");
                
                pst.setString(1, fname);
                pst.setString(2, path);
                pst.setString(3, type);
                pst.setString(4, data);
                pst.setInt(5, cid);

               pst.executeUpdate();

and my table structure is
CREATE TABLE "DSL"."COPHOTO"
   (      "PHOTO_ID" NUMBER(10,0),
      "FILE_NAME" VARCHAR2(50 BYTE) NOT NULL ENABLE,
      "FILE_PATH" VARCHAR2(400 BYTE),
      "FILE_TYPE" VARCHAR2(5 BYTE),
      "DATA" BLOB,
      "CO_ID" NUMBER(10,0) NOT NULL ENABLE,
       PRIMARY KEY ("PHOTO_ID") ENABLE
   ) ;

please help

cheers
zolf
0
zolf
Asked:
zolf
  • 9
  • 8
1 Solution
 
objectsCommented:
>               pst.setInt(5, cid);

try changing that to setLong
0
 
zolfAuthor Commented:

no use,i still get the error.
is it because of data which is defined as blob in database and in java i am trying to insert in setString ??
0
 
objectsCommented:
try

              pst.setBytes(5, cid.getBytes());
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
objectsCommented:
sorry, wronmg column

              pst.setBytes(4, data.getBytes());
0
 
zolfAuthor Commented:

now i dont get that error and the image gets saved in the db.but then when i try to again view the image it is not viewable in my java application.then when i try to view the image in the db.the oracle gives a message saying
Image could not be decoded from the binary stream.

0
 
objectsCommented:
how can your image data be stored in a string?
0
 
zolfAuthor Commented:

private String readPhotoFromFile(String fname) {
          String data = null;
          ByteArrayOutputStream baos = null;
          try {
                   FileInputStream fis = new FileInputStream(fname);
                   BufferedInputStream bis = new BufferedInputStream(fis);
                   byte[] buf = new byte[1024];
                  
                   baos = new ByteArrayOutputStream();
                  
                   int rd = -1;
                   while ((rd = bis.read(buf)) != -1) {
                         baos.write(buf, 0, rd);
                   }
                   bis.close();
                   fis.close();
                  
                   data = new BASE64Encoder().encode(baos.toByteArray());
                  
             }
             catch (Exception ex) {
                   System.err.println("error : " + ex.getMessage());
             }
             return data;
    }
0
 
objectsCommented:
hhow are you reading it from the app?  your need to be reversing the above process
0
 
zolfAuthor Commented:

String data = readPhotoFromFile(path);
                                //int aid = insertPhoto(data, name, path, ext, coId);
                                int aid = insertPhoto(data, name, path, ext, coId);
                                
                                if (aid != -1)
                                {
                                      thnPanel.addElement(aid, name, coId, coName, data);
                                      thnPanel.recountOnResize(jspPhoto.getViewport().getWidth());
                                      thnPanel.repaint();
                                      homeDir = path;
                                }


public void addElement(int id, String fname, int coId, String coName, String photoData)
      {
            ThumbnailElement thnElement = new ThumbnailElement(id, fname, coId, coName, createImagefromBase64(photoData));
            thumbnails.add(thnElement);
      }

public ThumbnailElement(int id, String name, int coId, String coName, Image image)
 {
            this.id = new Integer(id);
            this.name = name;
            this.coId = new Integer(coId);
            this.coName = coName;
            this.image = image;
            thnail = createThumbnail();
      }

private Image createThumbnail() {
            if (image == null) return null;
            
            if (image.getWidth(null) <= THN_SIDE && image.getHeight(null) <= THN_SIDE) {
                  return image;
            }
            
            int max = image.getWidth(null);
            int x = THN_SIDE;
            float scale = THN_SIDE / (float)max ;
            int y = (int)(image.getHeight(null) * scale);
            
            if (image.getHeight(null) > max) {
                  max = image.getHeight(null);
                  scale = THN_SIDE / (float)max ;
                  y = THN_SIDE;
                  x = (int)(image.getWidth(null) * scale);
            }
            
            AffineTransform at = AffineTransform.getScaleInstance(scale, scale);
            BufferedImage bi = new BufferedImage(x, y, BufferedImage.TYPE_INT_RGB);//image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
          
            Graphics2D g2d = (Graphics2D)bi.getGraphics();
          g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
          g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
          g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
          g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
          g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
          g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                //      RenderingHints.VALUE_INTERPOLATION_BICUBIC
                //      RenderingHints.VALUE_INTERPOLATION_BILINEAR
                      RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR
          
          );
            

          g2d.drawImage(image, at, null);
          return bi;
      }
0
 
objectsCommented:
i mmeant how do u read it from the database
0
 
zolfAuthor Commented:

private void fillThumbnails(boolean co, int id) {
          try {
                Connection connect = PictMenuFrameDemo.getDBConnection();
                Statement st = connect.createStatement();
                  String query = "SELECT ph.photo_id, ph.file_name, ph.data, c.co_id, c.co_eng_name " +
                        " FROM co c, cophoto ph WHERE  ph.co_id=c.co_id(+) AND ";
                  if (!co){
                        query += "c.city_id=" + Integer.toString(id);
                  }
                  else {
                        query += "ph.co_id=" + Integer.toString(id);
                  }
                  System.out.println(query);
                  
                  ResultSet result = st.executeQuery(query);
                  while (result.next()) {
                        thnPanel.addElement(result.getInt(1),
                                                       result.getString(2),
                                                       result.getInt(4),
                                                       result.getString(5),
                                                       result.getString(3));
                  }
          }
          catch (SQLException sqle) {
                System.err.println("Can't get photo data from database : " + sqle.getMessage());
          }
    }
0
 
CEHJCommented:
Can you post your implementation of createImagefromBase64 please?
0
 
objectsCommented:
that won't work, you need to get the bytes from the database, create a string and then base64decode that string.
ie the reverse process of what you did when storing it.
0
 
zolfAuthor Commented:

private Image createImagefromBase64(String data) {
            Image im = null;
            if (data != null && data.length() > 0) {
                  try {
                        im =
                              new ImageIcon(
                                          Toolkit.getDefaultToolkit().createImage(
                                                      new BASE64Decoder().decodeBuffer(data))
                                                      ).getImage();
                        
                  }
                  catch (Exception exc) {
                        
                  }
            }
            return im;
      }
0
 
objectsCommented:
ie. use getBytes() and then create a String from those bytes
0
 
zolfAuthor Commented:

could you please give me code example.i dont understand
0
 
objectsCommented:
                   thnPanel.addElement(result.getInt(1),
                                              result.getString(2),
                                              result.getInt(4),
                                              result.getString(5),
                                              new String(result.getBytes(3)));
0
 
zolfAuthor Commented:

thanks mate.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 9
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now