What is the difference between the methods setBlob() and setBinaryStream() which is preferable in JDBC?

Java 8MySQLMySQLi DatabaseJDBC

The setBlob() method is used to set value for Blob datatype in the database. It has three variants as follows:

  • void setBlob(int parameterIndex, Blob x): Sets the given Blob value to the parameter at the specified index.

  • void setBlob(int parameterIndex, InputStream inputStream): Sets the contents of the given input stream as a value to the parameter at the specified index.

  • void setBlob(int parameterIndex, InputStream inputStream, long length): Sets the contents of the given input stream as a value to the parameter at the specified index.

The setBinaryStream() method is used to set the contents of the given InputStream as a value for the parameter in the specified index. It has three variants as follows:

  • void setBinaryStream(int parameterIndex, InputStream x): Sets the contents the given input stream as a value to the parameter at the specified index.

  • void setBinaryStream(int parameterIndex, InputStream x, int length): ): Sets the contents the given input stream (which will have the specified number of bytes) as a value to the parameter at the specified index.

  • void setBinaryStream(int parameterIndex, InputStream x, long length): Sets the contents the given input stream (which will have the specified number of bytes) as a value to the parameter at the specified index.

The main difference between the two of these methods is that the setBlob() methods indicates the driver that the parameter value should be sent as a BLOB datatype to the server.

Where as in case of setBinaryStream() method first of all the driver determines in which format the value should be sent, (LONGVARBINARY or BLOB) and then sends it to the Server.

raja
Published on 22-Mar-2019 15:19:55
Advertisements