Solved

cannot find the error

Posted on 2006-07-15
18
278 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Core Java. What output will be and why ? 6 66
Starting to learn JAVA, 7 59
GUI builder for Eclipse? 8 27
expectj telnet failing 5 26
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

896 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now