占位符的使用和PreparedStatement接口使用:

  一、PreparedStatement 接口的使用

  首先占位符我们可以使用 Statement 接口来操作数据, 但是这个接口存在两个问题:

1、使用 Statement  接口对象发送的 sql 语句需要在数据库中进行一次编译之后成为指令才能执行,

并且每条 sql 语句都需要编译一次, 这样效率是很慢的。

2、使用 Statement 接口操作的 sql 可以使用字符串拼接的方式实现, 这样的方式可能存在 sql 注入

的安全风险问题,并且拼接字符串比较麻烦。

  解决以上两个问题我们可以使用 PreparedStatement 接口来避免。 使用 PreparedStatement 接口

操作的 sql 语句会先预编译成指令再发送给数据库,数据库

就执行指令即可,这样提高 一定的速度,而且使用 PreparedStatement 接口可以避开 sql 需要要使用

字符串拼接的方式,从而使用占位符(?)来代替。这样就解决了 sql 注入的安全风险。

使用占位符示例代码如下:

 

package sxt.scc;

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

import sxt.scc.util.ConnectionUtil;


public class SqlInject {
    //    取得连接
    private static Connection conn= ConnectionUtil.getConnection();
    public static void main(String[] args) {
        selectLogin("smith","1234");
    }
    public static boolean selectLogin(String name,String password) {
        //准备sql语句
        String sql= "SELECT * FROM myuser WHERE username=? AND password=?";
        System.out.println(sql);
        try {
            PreparedStatement pst =conn.prepareStatement(sql);
            //为占位符设置具体内容
            pst.setString(1, name);
            pst.setString(2, password);
            //发送sql语句
            ResultSet rst = pst.executeQuery();
            if (rst.next()) {
                System.out.println("登录成功!");
            }else {
                System.out.println("用户名不正确");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        return false;
        
    }
}

使用占位符这样就解决了 sql 注入的安全风险。

使用占位符插入数据示例如下:

 

package sxt.scc.vo;

import java.io.Serializable;
import java.util.Date;

public class Emp implements Serializable{
    private Integer empno;
    private String ename;
    private String job;
    private Integer mgr;
    private Date hiredate;
    private Double sal;
    private Double comm;
    private Integer deptno;
    public Emp(Integer empno, String ename, String job, Integer mgr, Date hiredate, Double sal, Double comm,
            Integer deptno) {
        super();
        this.empno = empno;
        this.ename = ename;
        this.job = job;
        this.mgr = mgr;
        this.hiredate = hiredate;
        this.sal = sal;
        this.comm = comm;
        this.deptno = deptno;
    }
    public Emp() {
        super();
    }
    
    public Double getComm() {
        return comm;
    }
    public Integer getDeptno() {
        return deptno;
    }
    public Integer getEmpno() {
        return empno;
    }
    public String getEname() {
        return ename;
    }
    public Date getHiredate() {
        return hiredate;
    }
    public String getJob() {
        return job;
    }
    public Integer getMgr() {
        return mgr;
    }
    public Double getSal() {
        return sal;
    }
    public void setComm(Double comm) {
        this.comm = comm;
    }
    public void setDeptno(Integer deptno) {
        this.deptno = deptno;
    }
    public void setEmpno(Integer empno) {
        this.empno = empno;
    }
    public void setEname(String ename) {
        this.ename = ename;
    }
    public void setHiredate(Date hiredate) {
        this.hiredate = hiredate;
    }
    public void setJob(String job) {
        this.job = job;
    }
    public void setMgr(Integer mgr) {
        this.mgr = mgr;
    }
    public void setSal(Double sal) {
        this.sal = sal;
    }
    @Override
    public String toString() {
        return "Emp [empno=" + empno + ", ename=" + ename + ", job=" + job + ", mgr=" + mgr + ", hiredate=" + hiredate
                + ", sal=" + sal + ", comm=" + comm + ", deptno=" + deptno + "]";
    }
    
}

 

public class TesMysql {
    //取得连接
    private static Connection conn = ConnectionUtil.getConnection();

    public static void main(String[] args) throws Exception {
    //调用下面的方法
        insertEmp();
}

 


//
问:使用占位符插入数据 public static boolean insertEmp(Emp emp) throws SQLException { //取得Connection连接对象 Connection conn = ConnectionUtil.getConnection(); //定义sql语句 String sql= "INSERT INTO emp(ename, job, sal, hiredate, mgr, comm, deptno)" + "VALUES(?,?,?,?,?,?,?)"; //取得发送sql语句对象 PreparedStatement pst = conn.prepareStatement(sql); //为占位符赋值 pst.setObject(1, emp.getEname()); pst.setObject(2, emp.getJob()); pst.setObject(3, emp.getSal()); pst.setObject(4, new java.sql.Date(emp.getHiredate().getTime())); pst.setObject(5, emp.getMgr()); pst.setObject(6, emp.getComm()); pst.setObject(7, emp.getDeptno()); //执行sql语句 int row = pst.executeUpdate(); //取得自动增长的主键值 ConnectionUtil.close(conn); return row > 0; }

以上代码就是使用占位符和PreparedStatement接口的使用了。

 

posted @ 2019-04-16 21:06  一杯香米酒  阅读(2312)  评论(0编辑  收藏  举报