Statement与PreparedStatement的区别

Statement与PreparedStatement的区别

原创 2014年04月18日 10:43:11

  • 9305

1:创建时的区别: 

    Statement statement = conn.createStatement();
    PreparedStatement preStatement = conn.prepareStatement(sql);
    执行的时候: 
    ResultSet rSet = statement.executeQuery(sql);
    ResultSet pSet = preStatement.executeQuery();

由上可以看出,PreparedStatement有预编译的过程,已经绑定sql,之后无论执行多少遍,都不会再去进行编译,

而 statement 不同,如果执行多变,则相应的就要编译多少遍sql,所以从这点看,preStatement 的效率会比 Statement要高一些

[java] view plain copy

  1. import java.sql.Connection;  
  2. import java.sql.DriverManager;  
  3. import java.sql.PreparedStatement;  
  4. import java.sql.ResultSet;  
  5. import java.sql.Statement;  
  6.   
  7. public class JDBCTest {  
  8.       
  9.     public static void main(String[] args) throws Exception {  
  10.         //1 加载数据库驱动  
  11.         Class.forName("com.mysql.jdbc.Driver");  
  12.           
  13.         //2 获取数据库连接  
  14.         String url = "jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8";  
  15.         String user = "root";  
  16.         String password = "root";  
  17.         Connection conn = DriverManager.getConnection(url, user, password);  
  18.           
  19.         //3 创建一个Statement  
  20.         String sql = "select id,loginName,email from user where id=";  
  21.         String tempSql;   
  22.         int count = 1000;  
  23.         long time = System.currentTimeMillis();  
  24.         for(int i=0 ;i<count ;i++){  
  25.             Statement statement = conn.createStatement();  
  26.             tempSql=sql+(int) (Math.random() * 100);  
  27.             ResultSet rSet = statement.executeQuery(tempSql);  
  28.             statement.close();  
  29.         }  
  30.         System.out.println("statement cost:" + (System.currentTimeMillis() - time));    
  31.           
  32.         String psql = "select id,loginName,email from user where id=?";  
  33.         time = System.currentTimeMillis();    
  34.         for (int i = 0; i < count; i++) {    
  35.             int id=(int) (Math.random() * 100);    
  36.             PreparedStatement preStatement = conn.prepareStatement(psql);  
  37.             preStatement.setLong(1, new Long(id));    
  38.             ResultSet pSet = preStatement.executeQuery();  
  39.             preStatement.close();    
  40.         }    
  41.         System.out.println("preStatement cost:" + (System.currentTimeMillis() - time));    
  42.         conn.close();  
  43.     }  
  44.       
  45. }  


上述代码反复执行,

statement cost:95           preStatement cost:90

statement cost:100         preStatement cost:89

statement cost:92           preStatement cost:86

当然,这个也会跟数据库的支持有关系,http://lucaslee.iteye.com/blog/49292  这篇帖子有说明

虽然没有更详细的测试 各种数据库, 但是就数据库发展 版本越高,数据库对 preStatement的支持会越来越好,

所以总体而言, 验证  preStatement 的效率 比 Statement 的效率高

2>安全性问题

这个就不多说了,preStatement是预编译的,所以可以有效的防止 SQL注入等问题

所以 preStatement 的安全性 比 Statement 高

3>代码的可读性 和 可维护性 
这点也不用多说了,你看老代码的时候  会深有体会

preStatement更胜一筹

别的暂时还没有想到,说没有查到会更好一些(汗),如果有别的差异,以后再补充

猜你喜欢

转载自my.oschina.net/u/3787897/blog/1628404