database binary data

     When using a database, in most cases, variable storage of numeric, character and date types has met many of our needs. But at some point, we also need a type to store data, which is binary data. If we need to store files, pictures or videos that can only be stored using binary data, we need to know how to manipulate the binary data in the database.

       In many databases, the binary processing methods are different, and the data types are also different. In Microsoft's sqlserver , the binary data type can use the IMAGE type. The IMAGE type can store binary data of up to  2G , which should meet the vast majority of needs. . Blob objects are used for binary data in the mysql database, and 4 types of blob objects can be selected for the size of the storage space . In the Oracle database, the RowId object can store 10 bytes of binary data, and the Blob object can store up to 4G of data, which is enough.

       So how to use JDBC to store binary data in the database? The answer is to use streams for processing. In JDBC , use the input stream to read a data, and then store the data in the stream as a byte[]  type byte array, that is, as a binary object. Of course, if you use the PrepareStatement interface, it is very convenient. You can use the precompiled parameter setting method to directly set the input stream as a storage parameter. The method issetBinaryStream(int parameterIndex, InputStream x, int length)。这样就可以将二进制的数据存储到数据库中了。

    读取数据库中的二进制数据很简单,就是使用ResultSet结果集中的getBinaryStream()方法就可以从数据库中读取出二进制的数据了。

    对于JDBC还有一中存储二进制数据的方法,Blob对象提供可以操作二进制数据的接口,用来对二进制数据操作是很方便的。

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326754127&siteId=291194637