Java Reference
In-Depth Information
Here's an example for a connection object called cc :
PreparedStatement ps = cc.prepareStatement(
“SELECT * FROM Coal WHERE (Country='?') ORDER BY YEAR”);
Here's another example with more than one question mark:
PreparedStatement ps = cc.prepareStatement(
“INSERT INTO BOOKDATA VALUES(?, ?, ?, ?, ?, ?, ?)”);
The question marks in these SQL statements are placeholders for data. Before you can
execute the statement, you must put data in each of these places using one of the meth-
ods of the PreparedStatement class.
To put data into a prepared statement, you must call a method with the position of the
placeholder followed by the data to insert.
For example, to put the string “Swaziland” in the first prepared statement, call the
setString( int , String ) method:
ps.setString(1, “Swaziland”);
The first argument indicates the position of the placeholder, numbered from left to right.
The first question mark is 1, the second is 2, and so on.
The second argument is the data to put in the statement at that position.
The following methods are available:
setAsciiStream( int , InputStream , int ) —At the position indicated by the first
argument, inserts the specified InputStream , which represents a stream of ASCII
characters. The third argument indicates how many bytes from the input stream to
insert.
n
setBinaryStream( int , InputStream , int ) —At the position indicated by the
first argument, inserts the specified InputStream , which represents a stream of
bytes. The third argument indicates the number of bytes to insert from the stream.
n
setCharacterStream( int , Reader , int ) —At the position indicated by the first
argument, inserts the specified Reader , which represents a character stream. The
third argument indicates the number of characters to insert from the stream.
n
setBoolean( int , boolean ) —Inserts a boolean value at the position indicated by
the integer.
n
setByte( int , byte ) —Inserts a byte value at the indicated position.
n
setBytes( int , byte[] ) —Inserts an array of bytes at the indicated position.
n
Search WWH ::




Custom Search