Java .

Java PreparedStatement

java.sql.PreparedStatement

a PreparedStatement example

PreparedStatement preparedStatement =
  connection.prepareStatement(
    "Select favoritefood from favoritefoods where catname = ?");
preparedStatement.setString(1, "Cappuccino");
                
ResultSet resultSet = preparedStatement.executeQuery();
  //now run the SQL

PreparedStatement methods to execute the SQL

preparedStatementName.execute();
  //will execute any SQL

preparedStatementName.executeQuery(); //will execute a query

preparedStatementName.executeUpdate(); //will execute an insert, update, or delete

PreparedStatement methods to set fields

preparedStatementName.setArray(
  intOfPositionToSet, arrayToSet);

preparedStatementName.setBigDecimal( intOfPositionToSet, bigDecimalToSet);

preparedStatementName.setBlob( intOfPositionToSet, blobToSet); //must be using JDBC 2.0 or above

preparedStatementName.setByte( intOfPositionToSet, byteToSet); //uses primitive byte

preparedStatementName.setBytes( intOfPositionToSet, byteArrayToSet); //uses primitive byte array

preparedStatementName.setClob( intOfPositionToSet, clobToSet); //must be using JDBC 2.0 or above

preparedStatementName.setDate( intOfPositionToSet, dateToSet); //uses java.sql.Date

preparedStatementName.setDate( intOfPositionToSet, dateToSet, calendarToUse); //JDBC 2.0 and above //uses java.sql.Date & java.util.Calendar

preparedStatementName.setDouble( intOfPositionToSet, doubleToSet); //uses primitive double preparedStatementName.setFloat( intOfPositionToSet, floatToSet); //uses primitive float

preparedStatementName.setInt( intOfPositionToSet, intToSet); //uses primitive int preparedStatementName.setLong( intOfPositionToSet, longToSet); //uses primitive long

preparedStatementName.setObject( intOfPositionToSet, objectToSet);

preparedStatementName.setObject( intOfPositionToSet, objectToSet, intTargetSqlType); //sql types: (see java.sql.Types below) preparedStatementName.setObject( intOfPositionToSet, objectToSet, intTargetSqlType); //sql types: (see java.sql.Types below) //for java.sql.Types.DECIMAL or java.sql.Types.NUMERIC // specifies number of digits after the decimal. preparedStatementName.setRef( intOfPositionToSet, refToSet); //JDBC 2.0 and above //uses java.sql.Ref specifying a // SQL structured type value in the database.

preparedStatementName.setShort( intOfPositionToSet, shortToSet); //uses primitive short preparedStatementName.setString( intOfPositionToSet, stringToSet); preparedStatementName.setTime( intOfPositionToSet, timeToSet); //uses java.sql.Time

preparedStatementName.setTime( intOfPositionToSet, timeToSet, calendarToUse); //JDBC 2.0 and above //uses java.sql.Time & java.util.Calendar preparedStatementName.setTimestamp( intOfPositionToSet, timeStampToSet); //uses java.sql.Timestamp

preparedStatementName.setTimestamp( intOfPositionToSet, timeStampToSet, calendarToUse); //JDBC 2.0 and above //uses java.sql.Timestamp & java.util.Calendar

PreparedStatement methods to set streams

preparedStatementName.setAsciiStream(
  intOfPositionToSet, asciiStreamToSet, intOfStreamLength);
    //sets a java.io.InputStream

preparedStatementName.setBinaryStream( intOfPositionToSet, binaryStreamToSet, intOfStreamLength); //sets a java.io.InputStream

preparedStatementName.setCharacterStream( intOfPositionToSet, readerObjectToGetCharStreamFrom, intOfStreamLength); //must be using JDBC 2.0 or above //sets a java.io.Reader

PreparedStatement methods to set nulls



preparedStatementName.setNull( intOfPositionToSet, sqlType); //sql types: (see java.sql.Types below) preparedStatementName.setNull( intOfPositionToSet, sqlType, stringTypeName); //sql types: (see java.sql.Types below) //sql type names: user named or REF type parameters. // examples of users named: STRUCT, DISTINCT, // JAVA_OBJECT, and named array types.

java.sql.Types

java.sql.Types.ARRAY
java.sql.Types.BIGINT
java.sql.Types.BINARY
java.sql.Types.BIT
java.sql.Types.BLOB     (JDBC 2.0 and above)
java.sql.Types.CHAR
java.sql.Types.CLOB     (JDBC 2.0 and above)
java.sql.Types.DATE
java.sql.Types.DECIMAL
java.sql.Types.DISTINCT (JDBC 2.0 and above)
java.sql.Types.DOUBLE
java.sql.Types.FLOAT
java.sql.Types.INTEGER
java.sql.Types.JAVA_OBJECT (JDBC 2.0 and above)
java.sql.Types.LONGVARBINARY
java.sql.Types.LONGVARCHAR
java.sql.Types.NULL
java.sql.Types.NUMERIC
java.sql.Types.OTHER
java.sql.Types.REAL
java.sql.Types.REF (JDBC 2.0 and above)
java.sql.Types.SMALLINT
java.sql.Types.STRUCT (JDBC 2.0 and above)
java.sql.Types.TIME
java.sql.Types.TIMESTAMP
java.sql.Types.TINYINT
java.sql.Types.VARBINARY
java.sql.Types.VARCHAR

PreparedStatement methods to alter what is in the PreparedStatement

preparedStatementName.clearParameters();
  //clears all parameters

References


online
The Java 2 Platform Specification
Books
Thinking in Java by Bruce Eckel
Core Java (TM) 2 Volume 2 - Advanced Features by Cay Horstmann and Gary Cornell
Comments
Sign in to be the first to comment on Java PreparedStatement.