0
try {
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost/EMP", "root", "");
    PreparedStatement ps = con.prepareStatement("insert into imgtable values(?,?)");
    ps.setString(234, "tester");
    FileInputStream fin = new FileInputStream("d:\\resume.pdf");
    ps.setBinaryStream(2, fin, fin.available());
    int i = ps.executeUpdate();
    System.out.println(i + " Your details successfully uploaded");
    con.close();
} catch (Exception e) {
    e.printStackTrace();
}

Using this code i have inserted pdf file into database it's working fine, please help for further process. how to download pdf file form database. using blob data type.

1
  • Way too broad for SO, I can just advise you to follow a good tutorial on JDBC. And maybe folllow a good SQL tutorial as well, to understand why insert into imgtable values(?,?) isn't a good idea (hint: without column names all hell breaks loose should one day the layout of your table change). Commented Jul 4, 2015 at 12:34

2 Answers 2

1
stmt = con.prepareStatement("SELECT myimg from imgtable where mykey = ?");

stmt.setString(1, "tester");
rs = stmt.executeQuery();
if (!rs.next()) {
  System.out.println("File not found in the database.");
} else {
  Blob b = rs.getBlob(1);
  BufferedOutputStream os;
  File f = new File("d:\\retrievedResume.pdf");


  os = new BufferedOutputStream(new FileOutputStream(f));
  os.write(b.getBytes(0, (int) b.length()), 0, (int) b
      .length());
  os.flush();
  os.close();
}
Sign up to request clarification or add additional context in comments.

Comments

0

If it is not special reason, I think you should save the pdf file to hard driver and save the location directory to database.
It's saved a lot of cost for performance, we don't need to binary serialize and deserialize object, especially when you save file which have big size.
Certainly, when you want to move database, you must to move the folder which saving data.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.