Solved

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

Posted on 2013-01-23
8
257 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
wordappend challenge 8 145
allswap challenge 6 77
micro services vs rest web services 16 84
Java - Why doesn't this JFrame work 3 41
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…
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 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.

911 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

22 Experts available now in Live!

Get 1:1 Help Now