We help IT Professionals succeed at work.

How to store image in oracle d/b using java?

paku
paku asked
on
i am java programmer. I want to store image in oracle d/b.
I have created table with blob and varchar2 colomns.
Well can somebody provide me the exact systax how to store,retrive and update the image in oracle d/b using java program.
Comment
Watch Question

File file = new File("myimage.gif");
FileInputStream fis = new FileInputStream(file);
PreparedStatement ps = conn.prepareStatement("INSERT INTO images VALUES (?, ?)");
ps.setString(1, file.getName());
ps.setBinaryStream(2, fis, file.length());
ps.executeUpdate();
ps.close();
fis.close();
And for retrieving the image back use the following

// column is column index in ResultSet containing the image,

Image myImage = null;
InputStream stream = rset.getBinaryStream(column);
ByteArrayOutputStream output = new ByteArrayOutputStream();
try {
  int a1 = stream.read();
  while (a1 >= 0) {
    output.write((char)a1);
     a1 = stream.read();
     }
  myImage =
    Toolkit.getDefaultToolkit().createImage(output.toByteArray());
  output.close();
  }
catch(Exception e){}
 

Author

Commented:
Rajesh as i am trying to insert the
image using your given code but i am getting the following error.
The Exception :java.sql.SQLException: ORA-01461: can bind a LONG value only for insert into a LONG column
Can u post a snippet of ur code. It seems like a casting problem.
R u using windows NT?

Author

Commented:
Yes rajesh i am using Windows NT. Here is the snippet of the code

<%@ page import="java.sql.*,java.io.*"%>
<html>
<head>
<title>ImageExample</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF">
<form method="post" name="regform" action="SuccessfulRegistration.jsp">
<%!
Connection conn = null;
PreparedStatement ps =null;
ResultSet rs  =null;
%>
<%
 try
{
      Class.forName( "oracle.jdbc.driver.OracleDriver" );
      conn =  DriverManager.getConnection("jdbc:oracle:thin:@10.21.10.6:1521:ORCL","scott","tiger");
        File file = new File("myimage.gif");
     FileInputStream fis = new FileInputStream(file);
     ps = conn.prepareStatement("INSERT INTO image_test VALUES (?, ?)");
     ps.setsetBinaryStream(1, fis, (int)file.length());
     ps.setString(2, file.getName());
     ps.executeUpdate();
     ps.close();
     fis.close();
}
catch (Exception e)
{ out.println("The Exception :"+e);}
 %>
 </form>
</body>
</html>
I learnt that in windows NT, oracle can't support new features. IT will support all the features available in 7.x. So, try the same program in windows 98/2000 where oracle is insatalled.
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if still open in seven days.  Please post closing recommendations before that time.

Question(s) below appears to have been abandoned. Your options are:
 
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you. You must tell the participants why you wish to do this, and allow for Expert response.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question. Again, please comment to advise the other participants why you wish to do this.

For special handling needs, please post a zero point question in the link below and include the question QID/link(s) that it regards.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click the Help Desk link on the left for Member Guidelines, Member Agreement and the Question/Answer process.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Please click you Member Profile to view your question history and keep them all current with updates as the collaboration effort continues, to track all your open and locked questions at this site.  If you are an EE Pro user, use the Power Search option to find them.  Anytime you have questions which are LOCKED with a Proposed Answer but does not serve your needs, please reject it and add comments as to why.  In addition, when you do grade the question, if the grade is less than an A, please add a comment as to why.  This helps all involved, as well as future persons who may access this item in the future to seek help.

To view your open questions, please click the following link(s) and keep them all current with updates.
http://www.experts-exchange.com/questions/Q.12042039.html
http://www.experts-exchange.com/questions/Q.20000226.html
http://www.experts-exchange.com/questions/Q.20003260.html
http://www.experts-exchange.com/questions/Q.20087455.html
http://www.experts-exchange.com/questions/Q.20089805.html
http://www.experts-exchange.com/questions/Q.20094072.html
http://www.experts-exchange.com/questions/Q.20102954.html
http://www.experts-exchange.com/questions/Q.20105249.html
http://www.experts-exchange.com/questions/Q.20175437.html
http://www.experts-exchange.com/questions/Q.20265897.html
http://www.experts-exchange.com/questions/Q.20268452.html


To view your locked questions, please click the following link(s) and evaluate the proposed answer.
http://www.experts-exchange.com/questions/Q.11962239.html
http://www.experts-exchange.com/questions/Q.20078199.html
http://www.experts-exchange.com/questions/Q.20080442.html
http://www.experts-exchange.com/questions/Q.20138500.html
http://www.experts-exchange.com/questions/Q.20174850.html

PLEASE DO NOT AWARD THE POINTS TO ME.  
 
------------>  EXPERTS:  Please leave any comments regarding your closing recommendations if this item remains inactive another seven (7) days.  Also, if you are interested in the cleanup effort, please click this link http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643

Moderators will finalize this question if still open in 7 days, by either moving this to the PAQ (Previously Asked Questions) at zero points, deleting it or awarding expert(s) when recommendations are made, or an independent determination can be made.  Expert input is always appreciated to determine the fair outcome.
 
Thank you everyone.
 
Moondancer
Moderator @ Experts Exchange

P.S.  For any year 2000 questions, special attention is needed to ensure the first correct response is awarded, since they are not in the comment date order, but rather in Member ID order.
Comment from expert accepted as answer.

Computer101
E-E Moderator

Explore More ContentExplore courses, solutions, and other research materials related to this topic.