Solved

cannot find the error

Posted on 2006-07-15
18
299 Views
Last Modified: 2012-08-13

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
Comment
Question by:zolf
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 8
18 Comments
 
LVL 92

Expert Comment

by:objects
ID: 17116608
>               pst.setInt(5, cid);

try changing that to setLong
0
 

Author Comment

by:zolf
ID: 17116612

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
 
LVL 92

Expert Comment

by:objects
ID: 17116614
try

              pst.setBytes(5, cid.getBytes());
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 17116615
sorry, wronmg column

              pst.setBytes(4, data.getBytes());
0
 

Author Comment

by:zolf
ID: 17116628

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
 
LVL 92

Expert Comment

by:objects
ID: 17116665
how can your image data be stored in a string?
0
 

Author Comment

by:zolf
ID: 17116666

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
 
LVL 92

Expert Comment

by:objects
ID: 17116676
hhow are you reading it from the app?  your need to be reversing the above process
0
 

Author Comment

by:zolf
ID: 17116680

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
 
LVL 92

Expert Comment

by:objects
ID: 17116711
i mmeant how do u read it from the database
0
 

Author Comment

by:zolf
ID: 17116715

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
 
LVL 86

Expert Comment

by:CEHJ
ID: 17116716
Can you post your implementation of createImagefromBase64 please?
0
 
LVL 92

Expert Comment

by:objects
ID: 17116722
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
 

Author Comment

by:zolf
ID: 17116725

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
 
LVL 92

Expert Comment

by:objects
ID: 17116727
ie. use getBytes() and then create a String from those bytes
0
 

Author Comment

by:zolf
ID: 17116728

could you please give me code example.i dont understand
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 17116733
                   thnPanel.addElement(result.getInt(1),
                                              result.getString(2),
                                              result.getInt(4),
                                              result.getString(5),
                                              new String(result.getBytes(3)));
0
 

Author Comment

by:zolf
ID: 17116739

thanks mate.
0

Featured Post

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Unhandled exception type Exception 18 52
Adding multiple JVM environments to RedHat 6 7 69
web project error add remove 1 51
java mysql insert application 14 44
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question