Sử dụng PreparedStatement trong java


  • 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

  •  Sử dụng PreparedStatement thực thi truy vấn SQL

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

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

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

//Parent
public ResultSet get_Result_catParent(int iSiteid){
GenericDAO dao = new GenericDAO();
Connection con = null;
PreparedStatement objSt = null;
ResultSet objResultSet = null;
try {
con = dao.getConnection();
String sql = “select * from  dual where   ca.caid = ? and ca.parentid = 0”;
System.out.print(sql);
objSt = con.prepareStatement(sql);
objSt.setInt(1, iSiteid);
objResultSet = objSt.executeQuery();
/*while(objResultSet.next()){
int get_id = objResultSet.getInt(“ID”);
int get_caid = objResultSet.getInt(“CAID”);
String get_disname = objResultSet.getString(“DISPLAY_NAME”);
String id_caid = get_id+”,”+get_caid;
}*/

//con.close();

} catch (SQLException e) {
System.out.print(e);
}
return objResultSet;
}

  • Sử dụng PreparedStatement để thiết lập trường giá trị

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

 

  • Sử dụng PreparedStatement để thiết lập 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

 

  • Sử dụng PreparedStatement để thiết lập giá trị 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

 

–@ Developer by Doannv

Gửi phản hồi

Please log in using one of these methods to post your comment:

WordPress.com Logo

Bạn đang bình luận bằng tài khoản WordPress.com Log Out / Thay đổi )

Twitter picture

Bạn đang bình luận bằng tài khoản Twitter Log Out / Thay đổi )

Facebook photo

Bạn đang bình luận bằng tài khoản Facebook Log Out / Thay đổi )

Google+ photo

Bạn đang bình luận bằng tài khoản Google+ Log Out / Thay đổi )

Connecting to %s