Solved

cannot find the error

Posted on 2006-07-15
18
294 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
  • 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet  URL Rewriting 1 42
servlet example 11 49
use lov values 2 61
java imports not found 4 26
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

856 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