zoukankan      html  css  js  c++  java
  • 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还是能提升不少运行效率的。

  • 相关阅读:
    mvc与springmvc
    mybatis一级与二级缓存详解
    resultType和resultMap的使用场景
    mybatis第一天学习总结
    linux常用命令(不断更新)
    SSH基本框架搭建的详细过程
    hibernate多条件组合查询的两种方式
    AJAX验证用户名是否被注册
    Vimrc
    关于window.open在不同浏览器的不同点
  • 原文地址:https://www.cnblogs.com/xiamuyouren/p/3282484.html
Copyright © 2011-2022 走看看