Java - JDBC - BLOBDataRetrieve
public class BLOBDataRetrieve {
public void retrieveBLOBState () throws SQLException, IOException {
Connection conn = DB_Util.getConn(DB_Types.MYSQLDB);
PreparedStatement pst = null;
Scanner scanner = new Scanner(System.in);
try {
String sql = "select file2 from account where id = ?";
pst = conn.prepareStatement(sql);
System.out.print("Enter Account ID: ");
pst.setInt(1, scanner.nextInt());
ResultSet rs = pst.executeQuery();
if(rs.next()) {
Blob imgBlob = rs.getBlob("file2");
FileOutputStream fos = new FileOutputStream("C:/Users/audrius.januskeviciu/Documents/Test.png");
fos.write(imgBlob.getBytes(1, (int)imgBlob.length()));
fos.flush();
fos.close();
System.out.println("Image was Downloaded Successfully!");
}
else {
System.err.println("No Image for this Account");
}
rs.close();
scanner.close();
pst.close();
conn.close();
}
catch (SQLException e) {
System.out.println("Something wrong - " + e);
}
}
}