PreparedStatement防止SQL注入

插入数据

import com.pan.lesson01.lesson02.utils.JdbcUtils;

import java.sql.*;
import java.util.Date;

public class TestInsert {
    public static void main(String[] args) throws SQLException {
        Connection conn = null;
        PreparedStatement pstm = null;
        ResultSet rs = null;
        try {
             conn = JdbcUtils.getConnection();
             String sql = "INSERT INTO users(`id`,`NAME`,`PASSWORD`,`email`,`birthday`)"  +
                                      "VALUES(?,?,?,?,?)"; //使用问号占位符,代替参数
             pstm = conn.prepareStatement(sql); //预编译sql,先写sql不执行
             //手动给参数(?问号)赋值
            pstm.setInt(1,5);
            pstm.setString(2,"李洋");
            pstm.setString(3,"54446666");
            pstm.setString(4,"245884732@qq.com");
            //注意点:sql.Date util.Date 类型包不一样 最后要把时间戳转换为java.sql.Date类型
            pstm.setDate(5,new java.sql.Date(new Date().getTime()));
         //执行sql
            int i = pstm.executeUpdate();
            if (i>0){
                System.out.println("插入成功");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JdbcUtils.release((com.mysql.jdbc.Connection) conn,pstm,rs);
        }
    }
}

删除数据

import com.pan.lesson01.lesson02.utils.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

public class TestDelet {
    public static void main(String[] args) throws SQLException {
        Connection conn = null;
        PreparedStatement pstm = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtils.getConnection();
            String sql = "delete from users where id=?";
            pstm = conn.prepareStatement(sql); //预编译sql,先写sql不执行
            //手动给参数(?问号)赋值
            pstm.setInt(1,5);


            //执行sql
            int i = pstm.executeUpdate();
            if (i>0){
                System.out.println("删除成功");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JdbcUtils.release((com.mysql.jdbc.Connection) conn,pstm,rs);
        }
    }
    }

查询数据

import com.pan.lesson01.lesson02.utils.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class TestSelect {

    public static void main(String[] args) throws SQLException {
        Connection conn = null;
        PreparedStatement pstm = null;
        ResultSet rs = null;
        try {
             conn = JdbcUtils.getConnection();
             String sql = "select * from users where id=?"; //编写sql
           pstm = conn.prepareStatement(sql); //预编译
           pstm.setInt(1,1); //传递参数 表中id=1 的第一条数据
           //执行
            rs = pstm.executeQuery();
         if (rs.next()){
             System.out.println(rs.getString("NAME"));
         }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JdbcUtils.release((com.mysql.jdbc.Connection) conn,pstm,rs);
        }
    }
}

修改数据
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值