How to create a java.sql.Blob object in Java SE 1.5.0 with a byte[] input?

后端 未结 3 1521
孤城傲影
孤城傲影 2021-02-12 20:14

I want to create a Blob object from a byte[] input to update a table using PreparedStatement#setBlob(). In J2SE 6, we have java.sql.Connection#createBlob() to get t

3条回答
  •  离开以前
    2021-02-12 20:45

    You don't have to worry about creating Blob objects at all. Treat them as blobs on the database, and byte[]s in Java. For example:

    @Entity
    @Table(name = "some.table")
    public class MyEntity
    {
        @Id
        int myId;
    
        @Lob
        byte[] myBlob;
    
        // snip getters & setters
    }
    

    If you're really intent on creating a Blob instance yourself, you can use the SerialBlob implementation:

    byte[] bytes = ...;
    Blob myBlob = new SerialBlob(bytes);
    

提交回复
热议问题