How to convert Byte Array to BLOB in java?

Java 8Object Oriented ProgrammingProgramming

You can convert a byte array to blob bypassing byte array to the constructor of the SerialBlob class.

Example

import java.io.BufferedReader;
import java.io.FileReader;
import java.sql.Blob;
import java.util.Scanner;
import javax.sql.rowset.serial.SerialBlob;
public class ByteArrayToBlob {
   public static void main(String args[]) throws Exception {
      Scanner sc = new Scanner(new BufferedReader(new
      FileReader("myData")));
      Blob blob = null;
      while(sc.hasNext()) {
         byte[] myArray = sc.nextLine().getBytes();
         blob = new SerialBlob(myArray );
      }
   }
}
raja
Published on 09-Jan-2018 16:52:41
Advertisements