Solved

JAVA JDBC, Unable to insert record in Access database with one IMAGE field

Posted on 2013-01-23
8
270 Views
Last Modified: 2013-02-06
Dear Expert,

I am unable to inserty record in the Access data table with one Image field. I am constantly getting error like
"End of InputStream reached before satisfying length specified when InputStream was set"
Please help.

Regards
0
Comment
Question by:MILIND_JOG
  • 4
  • 3
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 38810529
Please post code used
0
 

Author Comment

by:MILIND_JOG
ID: 38810728
Dear CEHJ,
Thanks for prompt response. Please find the part of code where I am facing problem.

               File image = new File("c:/cust1823.jpg");
                FileInputStream fis = new FileInputStream(image);
                PreparedStatement psmnt = c.prepareStatement("INSERT INTO customer1   (SR_NO,PR_SR_NO,CUST_PHOTO) values (1026,0,?)");
                psmnt.setBinaryStream(1, fis, (int)(image.length()));
                try {

                        int retv = psmnt.executeUpdate();                
                }
                catch (Exception e){
                    System.out.println(e.getLocalizedMessage());
                }
 


Error throwing in highlighted statement

Regards
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 38811111
System.out.println(e.getLocalizedMessage());

Open in new window

should really be
e.printStackTrace();

Open in new window

You should also close the Statement and Connection post-insert
What field type are you using for the image?

General points:

a. avoid Access wherever possible - it's really not that compatible with Java and not even a proper database
b. bear in mind that saving the image in the db gives you a lot of hoops to jump through before you get access to it. You might find it better to leave it in the file system and simply store the path only.
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

 

Accepted Solution

by:
MILIND_JOG earned 0 total points
ID: 38811143
Ok CEHJ,

I will try to keep the path rather than the Image. I will come back to you once I get through.

Regards
0
 
LVL 16

Expert Comment

by:Valeri
ID: 38813926
Could it be because of autocommiting of the transactionf of conn?
put this line :
c.setAutoCommit(false);
before this line :
 PreparedStatement psmnt = c.prepareStatement("INSERT INTO customer1   (SR_NO,PR_SR_NO,CUST_PHOTO) values (1026,0,?)");

and put this line:
c.commit();
after this line:
 int retv = psmnt.executeUpdate();

You also have to close first your PreparedStatement and then your connection as well in the "finally" block after your "catch" statement.
0
 

Author Comment

by:MILIND_JOG
ID: 38843183
Dear CEHJ,
I am surprised why the 500 points were not given to you as I accepted your sugestion only.

Regards
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 38844556
I am surprised why the 500 points were not given to you as I accepted your sugestion only.
Here's why: ;)
MILIND_JOG requested that this question be closed by accepting MILIND_JOG's comment #a38811143 (0 points) as the solution for the following reason:
0
 

Author Closing Comment

by:MILIND_JOG
ID: 38858677
Dear CEHJ,

I accepted your sugestion and modified the system accordingly.

Thanks once again.
Regards
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

830 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