PreparedStatement和Statement

PreparedStatement接口继承了Statement,所以statement所有的功能,PreparedStatement都会拥有。

Statement实现查询:

Connection con = null;
Statement s = con.createStatement();
s.execute("sql");

PreparedStatement实现查询:

PreparedStatement ps = conn.prepareStatement( "INSERT into employees values (?, ?, ?)");   
ps.setString(name[n]);   
ps.setLong(id[n]);  
ps.setInt(salary[n]);  
ps.executeUpdate(); 

PreparedStatement实例包含已经编译好的SQL语句,所以其执行的速度快于Statement对象。Statement在执行时传入SQL,可能会出现在拼接SQL字符时带来的安全问题。

posted on 2013-04-01 22:31  it_code  阅读(373)  评论(0编辑  收藏  举报