preparedstatement
是從java.sql.connection對象和所提供的sql字符串得到的,sql字符串中包含問號(?),這些問號標明變量的位置,然後提供變量的值,最後執行語句,例如:
string sql = "select * from people p where p.id = ? and p.name = ?";
preparedstatement ps = connection.preparestatement(sql);
ps.setint(1,id);
ps.setstring(2,name);
resultset rs = ps.executequery();
使用preparedstatement的另一個優點是字符串不是動態創建的。