
Chapter 9. Advanced JDBC
ITSS Java Programming
NGUYEN Hong Quang, HUT

Statement and Prepared Statement (1)
Making of Statement object
All of parameters must be in
the statement
Making of Preparerd
Statement object
Some parameters are
not in the statement
Setting value of
parameter
Execution of Prepared
Statement
Execution of Statement

Statement and Prepared Statement (2)
Prepared Statement : compile only one time
Statement : compile each time to run
If we have to use one SQL statement several
times, it would better to use Preparerd
Statement

Creation of Prepared Statement
PreparedStatement
java.sql.Connection.prepareStatement(String sql) throws
SQLException
Arguments : setting by parameters “?”
Exemple :
Connection con;
PreparedStatement stmt;
try {
con = DriverManager.getConnection(url,userName,userPassword);
stmt = con.prepareStatement
("Update Student Set mark = ? Where id = ?");
} catch (SQLException e) { e.printStackTrace(); }

Setting of parameters
Format :
stmt. setXXX (index, value);
setXXX : method corresponding with each data type
Index : numerical value corresponding to the position of
the parameter
Value : value of parameter