Solved

cannot find the error

Posted on 2006-07-15
18
287 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Android studio getdrawable(int) is deprecated 4 86
hibernate example issues from command prompt 10 42
null output 3 25
going to wrong jsp page 2 21
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

803 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