zoukankan      html  css  js  c++  java
  • <四>JDBC_PreparedStatement的使用

    WHY?

      <1>使用Statement需要进行拼写SQL语句,容易出错;
      <2>PreparedStatement:是Statement的子接口,可以传入带占位符的SQL语句,并且提供了补充占位符变量的方法。

      <3>有效的防止SQL注入

        SQL注入是利用某些系统没有对用户输入的数据进行充分的检查,而在用户输入数据中注入非法的SQL语句段或命令从而利用系统的SQL引擎完成恶意行为的做法;对于java而言要防范SQL注入,只要用PreparedStatement取代Statement就可以了。

      SQL注入的实现:

        String username="a' or password= ";
          String password=" or '1'='1";
          String SQL="select * from users where username='"+username+"' and password='"+password+"'";

      <4>PreparedStatement能最大可能提高性能:

        ---DBServer会对预编译语句提供性能优化。因为预编译语句可能重复调用,所以语句在被DBServer的编译器编译后的执行代码被缓存下来,下次调用时只要是相同的预编译语句就不需要编译,只要将参数直                 接传入编译过的语句执行代码中就会得到执 行。

        ---在Statement语句中,即使是相同的操作但因为数据内容的不一样所以整个语句本身不能匹配,没有缓存语句的意义。事实是没有数据库会对普通语句编译后的执行代码缓存,这样每执行一次都要对传入

                   的语句进行一次编译。

        ---语法检查、语义检查、翻译成二进制命令,缓存。

    @Test
     public void testPreparedStatement(){
      
        Student stu=null;
        Connection conn=null;
        PreparedStatement ps=null;
      
        try {
           conn=JDBCTools.getConnection();
           String sql="insert into customers (name,email,birth) "
             + "values(?,?,?)";
           ps=conn.prepareStatement(sql);

         //设置占位符的值
           ps.setString(1, "kk");
           ps.setString(2, "123@。com");
           ps.setDate(3, new Date(new java.util.Date().getTime()));
           ps.executeUpdate();
        } catch (Exception e) {
           e.printStackTrace();
        }finally{
           JDBCTools.release(null, ps, conn);
        }
     }

    修改后的更新语句方法:

    @Test
     public void testPreparedStatement(){
      
        Student stu=null;
       
        String sql="insert into customers (name,email,birth) "
             + "values(?,?,?)";
        JDBCTools.update(sql, "mx","123@。com",new Date(new java.util.Date().getTime()));
     }

    JDBCTools.java  修改后的工具类

    import java.io.InputStream;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.Properties;

    public class JDBCTools {
     
     /*
      * 执行SQL的方法
      *   insert,update,delete
      * */
     public static void update(String sql,Object...args){
      
      Connection conn=null;
      PreparedStatement ps=null;
      
      try {
       /*
        * 1、获取Connection连接
        * 2、获取Statement
        * 3、SQL语句
        * 4、关闭数据库连接
        *
        * */
       conn=getConnection();
       ps=conn.prepareStatement(sql);
       
       for (int i = 0; i < args.length; i++) {
        ps.setObject(i+1, args[i]);
       }
       
       ps.executeUpdate();
       
       
      } catch (Exception e) {
       e.printStackTrace();
      }finally{
       release(null,ps, conn);
      }
     }

     public static Connection getConnection() throws Exception {

      String driverClass = null;
      String jdbcUrl = null;
      String user = null;
      String password = null;

      // 读取类路径下的jdbc.properties文件
      InputStream in = JDBCTools.class.getClassLoader().getResourceAsStream("jdbc.properties");
      Properties properties = new Properties();
      properties.load(in);

      driverClass = properties.getProperty("driver");
      jdbcUrl = properties.getProperty("jdbcUrl");
      user = properties.getProperty("user");
      password = properties.getProperty("password");
      // 加载数据库驱动程序
      Class.forName(driverClass);
      // 通过DriverManager的getConnection()方法获取数据库连接
      Connection connection = DriverManager.getConnection(jdbcUrl, user, password);
      return connection;

     }
      
     public static void release(ResultSet rs,Statement st,Connection conn){
      
      if (rs!=null) {
       try {
        rs.close();
       } catch (SQLException e) {
        e.printStackTrace();
       }
      }
      
      if (st!=null) {
       try {
        st.close();
       } catch (SQLException e) {
        e.printStackTrace();
       }
      }
      
      if (conn!=null) {
       try {
        conn.close();
       } catch (SQLException e) {
        e.printStackTrace();
       }
      }
     }
    }

  • 相关阅读:
    Spring事务管理(详解+实例)
    DRUID连接池的实用 配置详解
    spring配置c3p0
    Spring使用JdbcTemplate实现对数据库操作
    spring整合web 项目
    2018美团JAVA面试问题与总结
    Mybatis中的update动态SQL语句
    去除文件夹左下角的问号
    spring的aop实现原理
    Spring的bean管理注解和配置文件混合使用
  • 原文地址:https://www.cnblogs.com/iamkk/p/6059592.html
Copyright © 2011-2022 走看看