MySQL——PreparedStatement对象

PreparedStatement可以防止SQL注入,效率更高。

1. 增

public class TestInsert {
    public static void main(String[] args) throws SQLException {

        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            conn = jdbcUtils.getConnection();

            // 区别
            // 使用问号占位符代替参数
            String sql = "INSERT INTO users(id,`NAME`,`PASSWORD`,`email`,`birthday`) values(?,?,?,?,?)";

            ps = conn.prepareStatement(sql);  // 预编译SQl,先写sql,然后不执行

            // 手动给参数赋值
            ps.setInt(1,5);
            ps.setString(2,"haha");
            ps.setString(3,"55555");
            ps.setString(4,"5555@qq.com");
            // 注意点:sql.Date:数据库用      util.Date:Java用    Date().getTime():获得时间戳   java.sql.Date():转换
            ps.setDate(5, new java.sql.Date(new Date().getTime()));

            // 执行
            int i = ps.executeUpdate();
            if (i>0){
                System.out.println("插入成功!");
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            jdbcUtils.release(conn,ps,null);
        }
    }
}

2. 删

public class TestDelete {
    public static void main(String[] args) throws SQLException {

        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            conn = jdbcUtils.getConnection();

            // 区别
            // 使用问号占位符代替参数
            String sql = "delete from users where id = ?";

            ps = conn.prepareStatement(sql);  // 预编译SQl,先写sql,然后不执行

            // 手动给参数赋值
            ps.setInt(1,5);

            // 执行
            int i = ps.executeUpdate();
            if (i>0){
                System.out.println("删除成功!");
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            jdbcUtils.release(conn,ps,null);
        }
    }
}

3. 改

public class TestUpdate {
    public static void main(String[] args) throws SQLException {


        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            conn = jdbcUtils.getConnection();

            // 区别
            // 使用问号占位符代替参数
            String sql = "update users set `NAME`=? where id=?";

            ps = conn.prepareStatement(sql);  // 预编译SQl,先写sql,然后不执行

            // 手动给参数赋值
            ps.setString(1,"hahaha");
            ps.setInt(2,5);

            // 执行
            int i = ps.executeUpdate();
            if (i>0){
                System.out.println("修改成功!");
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            jdbcUtils.release(conn,ps,null);
        }
    }
}

4. 查

public class TestSelect {
    public static void main(String[] args) throws SQLException {

        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            conn = jdbcUtils.getConnection();

            String sql = "select * from users where id=?";  // 编写SQL

            ps = conn.prepareStatement(sql);  // 预编译

            ps.setInt(1,4);  // 传递参数

            rs = ps.executeQuery();  // 执行

            if (rs.next()){
                System.out.println(rs.getString("NAME"));
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            jdbcUtils.release(conn,ps,rs);
        }
    }
}

5. 防止SQL注入

public class 防止SQL注入 {
    public static void main(String[] args) throws SQLException {

        login("chenyang8","123456");  //正常登录
        //login(" ' 'or 1=1","123456");  // 技巧

    }

    // 登录业务
    public static void login(String username, String password) throws SQLException {

        Connection conn = null;
        PreparedStatement st = null;
        ResultSet rs = null;
        try {
            conn = jdbcUtils.getConnection();
            // PreparedStatement 防止SQl注入的本质,把传递进来的参数当作字符
            // 假设其中存在转义字符,比如说 ' 会被直接转义
            String sql = "select * from users where `NAME`=? and `PASSWORD`=?";

            st = conn.prepareStatement(sql);
            st.setString(1,username);
            st.setString(2,password);

            rs = st.executeQuery();  //查询完毕会返回一个结果集
            while (rs.next()){
                System.out.println(rs.getString("NAME"));
                System.out.println(rs.getString("PASSWORD"));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            jdbcUtils.release(conn,st,rs);
        }
    }
}

相关推荐

  1. 对象 对象实例

    2024-07-13 01:12:04       38 阅读
  2. js <span style='color:red;'>对象</span>

    js 对象

    2024-07-13 01:12:04      63 阅读
  3. XMLHttpRequestUpload 对象

    2024-07-13 01:12:04       58 阅读
  4. Servlet对象

    2024-07-13 01:12:04       53 阅读
  5. PreparedStatement对象

    2024-07-13 01:12:04       29 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-13 01:12:04       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-13 01:12:04       71 阅读
  3. 在Django里面运行非项目文件

    2024-07-13 01:12:04       58 阅读
  4. Python语言-面向对象

    2024-07-13 01:12:04       69 阅读

热门阅读

  1. 【小超嵌入式】猜数字游戏详细分析

    2024-07-13 01:12:04       19 阅读
  2. 基于深度学习的文本分类

    2024-07-13 01:12:04       20 阅读
  3. VRRP虚拟路由器协议的基本概述

    2024-07-13 01:12:04       21 阅读
  4. ubuntu服务器部署vue springboot前后端分离项目

    2024-07-13 01:12:04       19 阅读
  5. c++二分算法

    2024-07-13 01:12:04       19 阅读
  6. try catch 解决大问题

    2024-07-13 01:12:04       20 阅读
  7. [C++]多态

    2024-07-13 01:12:04       23 阅读
  8. [Python学习篇] Python Socket网络编程

    2024-07-13 01:12:04       24 阅读
  9. 洛谷 P1506 拯救 oibh 总部

    2024-07-13 01:12:04       22 阅读