java-mysql(2) Prepared statement

上一篇学习了java如何链接配置mysql,这篇学习下java如何处理sql预处理语句(PreparedStatement),首先是一个sql预处理的例子:

 1 package core;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.PreparedStatement;
 6 import java.sql.SQLException;
 7 import java.sql.Statement;
 8 import java.sql.ResultSet;
 9 
10 public class MethodReferencesTest {
11 
12     public static void main(String[] args) throws CloneNotSupportedException {
13         // TODO Auto-generated method stub
14         Connection connection = null;
15         Statement statement = null;
16         ResultSet resultSet = null;
17         String sqlurl = "jdbc:mysql://172.20.23.75:3306/testdb";
18         String sqluser = "root";
19         String sqlpassword = "123456";
20         long starttime=System.currentTimeMillis();
21         try {
22             connection = DriverManager.getConnection(sqlurl, sqluser,
23                     sqlpassword);
24             PreparedStatement prestatement = connection.prepareStatement("INSERT INTO Testing(Id) VALUES(?)");
25             prestatement.setInt(1, 1001);
26             prestatement.executeUpdate();
27         } catch (SQLException e) {
28             // TODO Auto-generated catch block
29             e.printStackTrace();
30         } finally {
31             try {
32                 if (resultSet != null) {
33                     resultSet.close();
34                 }
35                 if (statement != null) {
36                     statement.close();
37                 }
38                 if (connection != null) {
39                     connection.close();
40                 }
41             } catch (SQLException e) {
42                 e.printStackTrace();
43             }
44         }
45         System.out.println(System.currentTimeMillis()-starttime);
46         
47     }
48 
49 }

首先说一下prepared statement的好处。

1.因为parepared statement 相当与编译好的sql语句模板,所以当你需要运行大量结构相同但只是参数不同的sql语句时候,数据库只需要分析编译一次sql语句即可,其他的都只是参数的替换。这样可以大大提高系统运行效率。

2.因为prepared statement的一开始用占位符来替换sql语句里面的具体参数,这样就坐到了sql语句的参数化,可以避免sql inject的情况出现。

当创建好prepared statement之后,我们就可以设置参数.

prestatement.setInt(1, 1001);

然后执行sql语句

prestatement.executeUpdate();

ps:这里面用executeupdate是因为我们不需要获取返回的结果,所以executeUpdate可以用到我们Create,delete,insert,update的时候。

那么prepared statement到底可以提高多少运行时间呢?可以用两个例子来比较一下:

1.老老实实用executequery

connection = DriverManager.getConnection(sqlurl, sqluser,
                    sqlpassword);
statement = connection.createStatement();

for (int i = 1; i <= 1000; i++) {
                String query = "INSERT INTO Testing(Id) VALUES(" + 2 * i + ")";
                statement.executeUpdate(query);
            }
//耗时 5511MS

2.用prepared statement

connection = DriverManager.getConnection(sqlurl, sqluser,
                    sqlpassword);

PreparedStatement prestatement = connection.prepareStatement("INSERT INTO Testing(Id) VALUES(?)");

for(int i=1;i<1000;i++)
        {
            prestatement.setInt(1,i*2);
            prestatement.executeUpdate();
    }
//耗时:4123ms

 

所以说prepared statement还是能提升不少运行效率的。

 

 

posted @ 2013-08-26 14:09  夏木友人  阅读(858)  评论(1编辑  收藏  举报