• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 813
  • Last Modified:

URGENT: Retrieve a BLOB file from MySQL with JAVA

Hi, i saved in my database a file using the code below. How can i retrieve the file and possibly open the file? Thank you
File family_genogram = fg; // I get fg file from a JFrame and pass it through a parameter
insertNewPsychocologicalAssessment = connectionClients.prepareStatement("INSERT INTO assessement " + "(....... , " family_genogram, ...........) " + " VALUES (......,?,.....)");
 if (!family_genogram.getName().equals("")) {
     FileInputStream fis = new FileInputStream(family_genogram);
     insertNewPsychocologicalAssessment.setBinaryStream(14, fis, (int) family_genogram.length());
 } else {
    insertNewPsychocologicalAssessment.setBinaryStream(14, null, (int) family_genogram.length());
 }
So far i have:
 try {
     ResultSet fileInfo = getClientPsychologicalAssessment.executeQuery("SELECT    family_genogram FROM assessement " + "WHERE assessement.assessementID = " + assessmentID );
     //?????????????
     // WHAT SHOULD I DO WITH THE RESULTSET (fileInfo) TO END UP AS A FILE????
 } catch (IndexOutOfBoundsException iobe) {
          iobe.printStackTrace();
 } catch (SQLException sqle) {
        sqle.printStackTrace();
  }

Open in new window

0
nvlachos
Asked:
nvlachos
  • 3
  • 2
2 Solutions
 
CEHJCommented:
>>// WHAT SHOULD I DO WITH THE RESULTSET (fileInfo) TO END UP AS A FILE????

Try
InputStream in = fileInfo.getBinaryStream(1);
// Now read it

Open in new window

0
 
ksivananthCommented:
InputStream is = fileInfo.getBinaryStream( "family_genogram" ) ;

read the data from this stream and write to a file!
0
 
nvlachosAuthor Commented:
Thanks both are correct but i don't know how to write the is into a file type
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
nvlachosAuthor Commented:
I did that:
File fi = new File("");
Object file = fileInfo.getBinaryStream("family_genogram");
fi =  (File) file;
but when i run the program i get the following exception..
java.sql.SQLException: Before start of result set
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:910)
        at com.mysql.jdbc.ResultSet.checkRowPos(ResultSet.java:692)
        at com.mysql.jdbc.ResultSet.getBinaryStream(ResultSet.java:1341)
        at com.mysql.jdbc.ResultSet.getBinaryStream(ResultSet.java:1368)
0
 
nvlachosAuthor Commented:
I got rid of the exception because it needs a while(fileInfo.next()) { .................. }
Now i have the InputStream is = fileInfo.getBinaryStream(1) OR ("family_genogram")
and i try to convert it to file.
I tried:
OutputStream out = new FileOutputStream(fi);
                while (is.read() > 0) {
                out.write(is.read());
            }
            out.close();
            is.close();

but it throws:
java.io.FileNotFoundException:
        at java.io.FileOutputStream.open(Native Method)

0
 
CEHJCommented:
:-)

See the following for convenient stream copying

http://technojeeves.com/joomla/index.php/free/51-copying-streams
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now