Statement与PreparedStatement的区别

Statement与PreparedStatement的区别

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

  • 9305

1:建立时的区别: mysql

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

由上能够看出,PreparedStatement有预编译的过程,已经绑定sql,以后不管执行多少遍,都不会再去进行编译,数据库

而 statement 不一样,若是执行多变,则相应的就要编译多少遍sql,因此从这点看,preStatement 的效率会比 Statement要高一些安全

 

[java] view plain copyapp

  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. }  


上述代码反复执行,dom

 

statement cost:95           preStatement cost:90测试

statement cost:100         preStatement cost:89url

statement cost:92           preStatement cost:86spa

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

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

因此整体而言, 验证  preStatement 的效率 比 Statement 的效率高

2>安全性问题

这个就很少说了,preStatement是预编译的,因此能够有效的防止 SQL注入等问题

因此 preStatement 的安全性 比 Statement 高

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

preStatement更胜一筹

别的暂时尚未想到,说没有查到会更好一些(汗),若是有别的差别,之后再补充

相关文章
相关标签/搜索