星期二, 12月 27, 2011

[Java] Parameterized method in JDBC

JDBC supports many setXXX() methods, one for each Java data type, so that you can set parameter values directly with the desired Java data types without any conversion. Here is a list of setXXX() methods:




  • setArray() - Sets the value of the specified parameter as Java Array type.
  • setAsciiStream() - Sets the value of the specified parameter as a stream of ASCII characters.
  • setBigDecimal() - Sets the value of the specified parameter as java.sql.BigDecimal type.
  • setBinaryStream() - Sets the value of the specified parameter as a stream of bytes.
  • setByte() - Sets the value of the specified parameter as Java byte type.
  • setBlob() - Sets the value of the specified parameter as Java Blob type.
  • setBoolean() - Sets the value of the specified parameter as Java boolean type.
  • setBytes() - Sets the value of the specified parameter as Java byte[] type.
  • setCharacterStream() - Sets the value of the specified parameter as java.io.Reader type.
  • setClob() - Sets the value of the specified parameter as Java Clob type.
  • setDate() - Sets the value of the specified parameter as java.sql.Date type.
  • setDouble() - Sets the value of the specified parameter as Java double type.
  • setFloat() - Sets the value of the specified parameter as Java float type.
  • setInt() - Sets the value of the specified parameter as Java int type.
  • setLong() - Sets the value of the specified parameter as Java long type.
  • setNull() - Sets the value of the specified parameter as a null value.
  • setObject() - Sets the value of the specified parameter as Java Object type.
  • setRef() - Sets the value of the specified parameter as Java Ref type.
  • setShort() - Sets the value of the specified parameter as Java short type.
  • setString() - Sets the value of the specified parameter as Java String type.
  • setTime() - Sets the value of the specified parameter as java.sql.Time type.
  • setTimestamp() - Sets the value of the specified parameter as java.sql.Timestamp type.

經過一些資料的survey後,決定用setObject比較方便,請參考以下範例:

/********************************************
  * Add parameter to preparedstatement
  * @param parameterIndex - the first parameter is 1, the second is 2, ...
  * @param parameterValue - the object containing the input parameter value
  * @param targetSqlType - the SQL type (as defined in java.sql.Types) to be sent to the database
******************************************/ 
this.sqlPreStatement.setObject(parameterIndex, parameterValue, targetSqlType);

沒有留言:

張貼留言

留個話吧:)

其他你感興趣的文章

Related Posts with Thumbnails