Two dynamic parameters are used in the query string, and both of them are IN parameters. The data type of the first IN parameter is an integer; the second one is a String; and both are represented by a placeholder,?. The first setting method, setInt(1, 101) is to assign an integer value of 101 to the first IN parameter, which is indicated with a position number of 1, and the second setting method, setString(2, “Tom Johnson”), is to assign a String value, “Tom Johnson”, to the second IN parameter, which is indicated with a position number of 2.
From this example, you can see that there is no difference between setting a primitive parameter
and an object value to the IN parameters in a query statement.
Set Object Methods
The setObject() method has three protocols, which are:
setObject(int position, object _ type object _ value);
setObject(int position, object _ type object _ value,
desired _ data _ type);
setobject(int position, object _ type object _ value,
desired _ data _ type, int scale);
data _ type data _ type
The first one is straightforward, and it contains two parameters; the first one is the relative posi-tion of the IN parameter in the query statement, and the second one is the value of a desired object to be assigned to the IN object.
The second one adds one more input parameter, desired _ data _ type, and it is used to indicate a data type to convert the object to.
The third one adds the fourth input parameter, scale, and it is used to make sure that the object conversion result contains a certain number of digits.
An example of the setObject() method is shown here,
pstmt.setObject(2, 101); pstmt.setObject(2, 101, Type.FLOAT); pstmt.setObject(2, 101, Type.FLOAT, 2);
The first method is to set an input parameter, which is the second one in a query statement, to an object (here an integer) with a value of 101. The next method is to set the same input to the same object; however, it needs to convert the object (integer) to a float data type. The final method performs the same operation as the previous one, but it indicates that the conversion result should contain at least two digits.
Set Stream IN Methods
When transferring images between an application and a database, the IN parameters need to have large sizes. In that situation, an InputStream() method should be used. The syntax of this method is:
setXXXStream(int position, data _ type input _ stream, int number _ of _ bytes);
where XXX means the InputStream type: ASCII, Binary or Unicode. The first parameter is the relative position of the IN parameter in the query statement, and the second parameter is the data stream to be read from. The third parameter indicates the number of bytes to be read from the data stream at a time.
A simple example of using the InputStream() method is:
FileInputStream picFile = new FileInputStream(“new _ file”);
String query = “INSERT INTO picture (image) VALUES (?) WHERE pic _ id = 101 “;
PreparedStatement pstmt = prepareStatement(query); pstmt.setUnicodeStream(1, picFile, 2048);
This piece of code is used to set the first IN parameter to read 2KB bytes from a picture file, which is a Unicode file named picFile, at a time.