Solved

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

Posted on 2013-01-23
8
276 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Adding multiple JVM environments to RedHat 6 7 81
CSV file parsing thru Java 13 54
add projects t working set in maven 2 68
import as existing maven project 3 77
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

734 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