// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.*;
import java.sql.*;

public class BlobTest {
   public static void main(String[] args) throws Exception {
      byte[] b = new byte[(int)(new File("C:\\elogo.png")).length()]; 
      InputStream is = new FileInputStream("C:\\elogo.png");
      is.read(b);
      is.close();
      Class.forName("com.mysql.jdbc.Driver");
      Connection con = DriverManager.getConnection("jdbc:mysql://localhost/Test", "", "");
      PreparedStatement ins = con.prepareStatement("INSERT INTO BlobTest VALUES (?,?)");
      ins.setInt(1, 123);
      ins.setBytes(2, b);
      ins.executeUpdate();
      PreparedStatement sel = con.prepareStatement("SELECT Picture FROM BlobTest WHERE ID=?");
      sel.setInt(1, 123);
      ResultSet rs = sel.executeQuery();
      rs.next();
      byte[] b2 = rs.getBytes(1);
      con.close();
      OutputStream os = new FileOutputStream("C:\\elogo2.png");
      os.write(b2);
      os.close();
   }
}
