APIdia Launch Partner
Top Description Fields Methods
java.sql

public Interface PreparedStatement

extends Statement
Known Direct Subinterfaces
java.sql.CallableStatement
Imports
java.math.BigDecimal, java.util.Calendar, java.io.Reader, .InputStream

An object that represents a precompiled SQL statement.

A SQL statement is precompiled and stored in a PreparedStatement object. This object can then be used to efficiently execute this statement multiple times.

Note

The setter methods (setShort, setString, and so on) for setting IN parameter values must specify types that are compatible with the defined SQL type of the input parameter. For instance, if the IN parameter has SQL type INTEGER, then the method setInt should be used.

If arbitrary parameter type conversions are required, the method setObject should be used with a target SQL type.

In the following example of setting a parameter, con represents an active connection:

BigDecimal sal = new BigDecimal("153833.00");
  PreparedStatement pstmt = con.prepareStatement("UPDATE EMPLOYEES
                                    SET SALARY = ? WHERE ID = ?");
  pstmt.setBigDecimal(1, sal);
  pstmt.setInt(2, 110592);
Since
1.1
See Also
Connection#prepareStatement, ResultSet

Field Summary

Inherited from java.sql.Statement:
CLOSE_ALL_RESULTSCLOSE_CURRENT_RESULTEXECUTE_FAILEDKEEP_CURRENT_RESULTNO_GENERATED_KEYSRETURN_GENERATED_KEYSSUCCESS_NO_INFO

Method Summary

Modifier and TypeMethod and Description
public void
addBatch()

Adds a set of parameters to this PreparedStatement object's batch of commands.

public void
clearParameters()

Clears the current parameter values immediately.

public boolean

Returns:

true if the first result is a ResultSet object; false if the first result is an update count or there is no result
execute
()

Executes the SQL statement in this PreparedStatement object, which may be any kind of SQL statement.

public default long

Returns:

either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothing
executeLargeUpdate
()

Executes the SQL statement in this PreparedStatement object, which must be an SQL Data Manipulation Language (DML) statement, such as INSERT, UPDATE or DELETE; or an SQL statement that returns nothing, such as a DDL statement.

public ResultSet

Returns:

a ResultSet object that contains the data produced by the query; never null
executeQuery
()

Executes the SQL query in this PreparedStatement object and returns the ResultSet object generated by the query.

public int

Returns:

either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothing
executeUpdate
()

Executes the SQL statement in this PreparedStatement object, which must be an SQL Data Manipulation Language (DML) statement, such as INSERT, UPDATE or DELETE; or an SQL statement that returns nothing, such as a DDL statement.

public ResultSetMetaData

Returns:

the description of a ResultSet object's columns or null if the driver cannot return a ResultSetMetaData object
getMetaData
()

Retrieves a ResultSetMetaData object that contains information about the columns of the ResultSet object that will be returned when this PreparedStatement object is executed.

public ParameterMetaData

Returns:

a ParameterMetaData object that contains information about the number, types and properties for each parameter marker of this PreparedStatement object
getParameterMetaData
()

Retrieves the number, types and properties of this PreparedStatement object's parameters.

public void
setArray(int
the first parameter is 1, the second is 2, ...
parameterIndex
,
Array
an Array object that maps an SQL ARRAY value
x
)

Sets the designated parameter to the given java.sql.Array object.

public void
setAsciiStream(int
the first parameter is 1, the second is 2, ...
parameterIndex
,
InputStream
the Java input stream that contains the ASCII parameter value
x
,
int
the number of bytes in the stream
length
)

Sets the designated parameter to the given input stream, which will have the specified number of bytes.

public void
setAsciiStream(int
the first parameter is 1, the second is 2, ...
parameterIndex
,
InputStream
the Java input stream that contains the ASCII parameter value
x
,
long
the number of bytes in the stream
length
)

Sets the designated parameter to the given input stream, which will have the specified number of bytes.

public void
setAsciiStream(int
the first parameter is 1, the second is 2, ...
parameterIndex
,
InputStream
the Java input stream that contains the ASCII parameter value
x
)

Sets the designated parameter to the given input stream.

public void
setBigDecimal(int
the first parameter is 1, the second is 2, ...
parameterIndex
,
BigDecimal
the parameter value
x
)

Sets the designated parameter to the given java.math.BigDecimal value.

public void
setBinaryStream(int
the first parameter is 1, the second is 2, ...
parameterIndex
,
InputStream
the java input stream which contains the binary parameter value
x
,
int
the number of bytes in the stream
length
)

Sets the designated parameter to the given input stream, which will have the specified number of bytes.

public void
setBinaryStream(int
the first parameter is 1, the second is 2, ...
parameterIndex
,
InputStream
the java input stream which contains the binary parameter value
x
,
long
the number of bytes in the stream
length
)

Sets the designated parameter to the given input stream, which will have the specified number of bytes.

public void
setBinaryStream(int
the first parameter is 1, the second is 2, ...
parameterIndex
,
InputStream
the java input stream which contains the binary parameter value
x
)

Sets the designated parameter to the given input stream.

public void
setBlob(int
the first parameter is 1, the second is 2, ...
parameterIndex
,
Blob
a Blob object that maps an SQL BLOB value
x
)

Sets the designated parameter to the given java.sql.Blob object.

public void
setBlob(int
index of the first parameter is 1, the second is 2, ...
parameterIndex
,
InputStream
An object that contains the data to set the parameter value to.
inputStream
,
long
the number of bytes in the parameter data.
length
)

Sets the designated parameter to a InputStream object.

public void
setBlob(int
index of the first parameter is 1, the second is 2, ...
parameterIndex
,
InputStream
An object that contains the data to set the parameter value to.
inputStream
)

Sets the designated parameter to a InputStream object.

public void
setBoolean(int
the first parameter is 1, the second is 2, ...
parameterIndex
,
boolean
the parameter value
x
)

Sets the designated parameter to the given Java boolean value.

public void
setByte(int
the first parameter is 1, the second is 2, ...
parameterIndex
,
byte
the parameter value
x
)

Sets the designated parameter to the given Java byte value.

public void
setBytes(int
the first parameter is 1, the second is 2, ...
parameterIndex
,
byte[]
the parameter value
x
)

Sets the designated parameter to the given Java array of bytes.

public void
setCharacterStream(int
the first parameter is 1, the second is 2, ...
parameterIndex
,
Reader
the java.io.Reader object that contains the Unicode data
reader
,
int
the number of characters in the stream
length
)

Sets the designated parameter to the given Reader object, which is the given number of characters long.

public void
setCharacterStream(int
the first parameter is 1, the second is 2, ...
parameterIndex
,
Reader
the java.io.Reader object that contains the Unicode data
reader
,
long
the number of characters in the stream
length
)

Sets the designated parameter to the given Reader object, which is the given number of characters long.

public void
setCharacterStream(int
the first parameter is 1, the second is 2, ...
parameterIndex
,
Reader
the java.io.Reader object that contains the Unicode data
reader
)

Sets the designated parameter to the given Reader object.

public void
setClob(int
the first parameter is 1, the second is 2, ...
parameterIndex
,
Clob
a Clob object that maps an SQL CLOB value
x
)

Sets the designated parameter to the given java.sql.Clob object.

public void
setClob(int
index of the first parameter is 1, the second is 2, ...
parameterIndex
,
Reader
An object that contains the data to set the parameter value to.
reader
,
long
the number of characters in the parameter data.
length
)

Sets the designated parameter to a Reader object.

public void
setClob(int
index of the first parameter is 1, the second is 2, ...
parameterIndex
,
Reader
An object that contains the data to set the parameter value to.
reader
)

Sets the designated parameter to a Reader object.

public void
setDate(int
the first parameter is 1, the second is 2, ...
parameterIndex
,
Date
the parameter value
x
)

Sets the designated parameter to the given java.sql.Date value using the default time zone of the virtual machine that is running the application.

public void
setDate(int
the first parameter is 1, the second is 2, ...
parameterIndex
,
Date
the parameter value
x
,
Calendar
the Calendar object the driver will use to construct the date
cal
)

Sets the designated parameter to the given java.sql.Date value, using the given Calendar object.

public void
setDouble(int
the first parameter is 1, the second is 2, ...
parameterIndex
,
double
the parameter value
x
)

Sets the designated parameter to the given Java double value.

public void
setFloat(int
the first parameter is 1, the second is 2, ...
parameterIndex
,
float
the parameter value
x
)

Sets the designated parameter to the given Java float value.

public void
setInt(int
the first parameter is 1, the second is 2, ...
parameterIndex
,
int
the parameter value
x
)

Sets the designated parameter to the given Java int value.

public void
setLong(int
the first parameter is 1, the second is 2, ...
parameterIndex
,
long
the parameter value
x
)

Sets the designated parameter to the given Java long value.

public void
setNCharacterStream(int
of the first parameter is 1, the second is 2, ...
parameterIndex
,
Reader
the parameter value
value
,
long
the number of characters in the parameter data.
length
)

Sets the designated parameter to a Reader object.

public void
setNCharacterStream(int
of the first parameter is 1, the second is 2, ...
parameterIndex
,
Reader
the parameter value
value
)

Sets the designated parameter to a Reader object.

public void
setNClob(int
of the first parameter is 1, the second is 2, ...
parameterIndex
,
NClob
the parameter value
value
)

Sets the designated parameter to a java.sql.NClob object.

public void
setNClob(int
index of the first parameter is 1, the second is 2, ...
parameterIndex
,
Reader
An object that contains the data to set the parameter value to.
reader
,
long
the number of characters in the parameter data.
length
)

Sets the designated parameter to a Reader object.

public void
setNClob(int
index of the first parameter is 1, the second is 2, ...
parameterIndex
,
Reader
An object that contains the data to set the parameter value to.
reader
)

Sets the designated parameter to a Reader object.

public void
setNString(int
of the first parameter is 1, the second is 2, ...
parameterIndex
,
String
the parameter value
value
)

Sets the designated parameter to the given String object.

public void
setNull(int
the first parameter is 1, the second is 2, ...
parameterIndex
,
int
the SQL type code defined in java.sql.Types
sqlType
)

Sets the designated parameter to SQL NULL.

public void
setNull(int
the first parameter is 1, the second is 2, ...
parameterIndex
,
int
a value from java.sql.Types
sqlType
,
String
the fully-qualified name of an SQL user-defined type; ignored if the parameter is not a user-defined type or REF
typeName
)

Sets the designated parameter to SQL NULL.

public void
setObject(int
the first parameter is 1, the second is 2, ...
parameterIndex
,
Object
the object containing the input parameter value
x
,
int
the SQL type (as defined in java.sql.Types) to be sent to the database
targetSqlType
)

Sets the value of the designated parameter with the given object.

public void
setObject(int
the first parameter is 1, the second is 2, ...
parameterIndex
,
Object
the object containing the input parameter value
x
)

Sets the value of the designated parameter using the given object.

public void
setObject(int
the first parameter is 1, the second is 2, ...
parameterIndex
,
Object
the object containing the input parameter value
x
,
int
the SQL type (as defined in java.sql.Types) to be sent to the database. The scale argument may further qualify this type.
targetSqlType
,
int
for java.sql.Types.DECIMAL or java.sql.Types.NUMERIC types, this is the number of digits after the decimal point. For Java Object types InputStream and Reader, this is the length of the data in the stream or reader. For all other types, this value will be ignored.
scaleOrLength
)

Sets the value of the designated parameter with the given object.

public default void
setObject(int
the first parameter is 1, the second is 2, ...
parameterIndex
,
Object
the object containing the input parameter value
x
,
SQLType
the SQL type to be sent to the database. The scale argument may further qualify this type.
targetSqlType
,
int
for java.sql.JDBCType.DECIMAL or java.sql.JDBCType.NUMERIC types, this is the number of digits after the decimal point. For Java Object types InputStream and Reader, this is the length of the data in the stream or reader. For all other types, this value will be ignored.
scaleOrLength
)

Sets the value of the designated parameter with the given object.

public default void
setObject(int
the first parameter is 1, the second is 2, ...
parameterIndex
,
Object
the object containing the input parameter value
x
,
SQLType
the SQL type to be sent to the database
targetSqlType
)

Sets the value of the designated parameter with the given object.

public void
setRef(int
the first parameter is 1, the second is 2, ...
parameterIndex
,
Ref
an SQL REF value
x
)

Sets the designated parameter to the given REF(<structured-type>) value.

public void
setRowId(int
the first parameter is 1, the second is 2, ...
parameterIndex
,
RowId
the parameter value
x
)

Sets the designated parameter to the given java.sql.RowId object.

public void
setShort(int
the first parameter is 1, the second is 2, ...
parameterIndex
,
short
the parameter value
x
)

Sets the designated parameter to the given Java short value.

public void
setSQLXML(int
index of the first parameter is 1, the second is 2, ...
parameterIndex
,
SQLXML
a SQLXML object that maps an SQL XML value
xmlObject
)

Sets the designated parameter to the given java.sql.SQLXML object.

public void
setString(int
the first parameter is 1, the second is 2, ...
parameterIndex
,
String
the parameter value
x
)

Sets the designated parameter to the given Java String value.

public void
setTime(int
the first parameter is 1, the second is 2, ...
parameterIndex
,
Time
the parameter value
x
)

Sets the designated parameter to the given java.sql.Time value.

public void
setTime(int
the first parameter is 1, the second is 2, ...
parameterIndex
,
Time
the parameter value
x
,
Calendar
the Calendar object the driver will use to construct the time
cal
)

Sets the designated parameter to the given java.sql.Time value, using the given Calendar object.

public void
setTimestamp(int
the first parameter is 1, the second is 2, ...
parameterIndex
,
Timestamp
the parameter value
x
)

Sets the designated parameter to the given java.sql.Timestamp value.

public void
setTimestamp(int
the first parameter is 1, the second is 2, ...
parameterIndex
,
Timestamp
the parameter value
x
,
Calendar
the Calendar object the driver will use to construct the timestamp
cal
)

Sets the designated parameter to the given java.sql.Timestamp value, using the given Calendar object.

public void
setUnicodeStream(int
the first parameter is 1, the second is 2, ...
parameterIndex
,
InputStream
a java.io.InputStream object that contains the Unicode parameter value
x
,
int
the number of bytes in the stream
length
)
Deprecated since 1.2. Use setCharacterStream

Sets the designated parameter to the given input stream, which will have the specified number of bytes.

public void
setURL(int
the first parameter is 1, the second is 2, ...
parameterIndex
,
URL
the java.net.URL object to be set
x
)

Sets the designated parameter to the given java.net.URL value.

Inherited from java.sql.Statement:
addBatchcancelclearBatchclearWarningsclosecloseOnCompletionenquoteIdentifierenquoteLiteralenquoteNCharLiteralexecuteexecuteexecuteexecuteexecuteBatchexecuteLargeBatchexecuteLargeUpdateexecuteLargeUpdateexecuteLargeUpdateexecuteLargeUpdateexecuteQueryexecuteUpdateexecuteUpdateexecuteUpdateexecuteUpdategetConnectiongetFetchDirectiongetFetchSizegetGeneratedKeysgetLargeMaxRowsgetLargeUpdateCountgetMaxFieldSizegetMaxRowsgetMoreResultsgetMoreResultsgetQueryTimeoutgetResultSetgetResultSetConcurrencygetResultSetHoldabilitygetResultSetTypegetUpdateCountgetWarningsisClosedisCloseOnCompletionisPoolableisSimpleIdentifiersetCursorNamesetEscapeProcessingsetFetchDirectionsetFetchSizesetLargeMaxRowssetMaxFieldSizesetMaxRowssetPoolablesetQueryTimeout