Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-01-23
8
Medium Priority
?
291 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

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 …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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.
Suggested Courses

885 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