Solved

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

Posted on 2013-01-23
8
254 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
 

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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now