• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 810
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

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