JDBC_PreparedStatement 接口

参考:传智播客&黑马程序员

PreparedStatement 接口

继承结构与作用
在这里插入图片描述
PreparedStatement 是 Statement 接口的子接口,继承于父接口中所有的方法,它是一个预编译的 SQL 语句。

PreparedSatement 的执行原理
在这里插入图片描述
1.因为有预先编译的功能,提高 SQL 的执行效率。
2.可以有效的防止 SQL 注入的问题,安全性更高。

Connection 创建 PreparedStatement 对象
在这里插入图片描述
PreparedStatement 接口中的方法
在这里插入图片描述
PreparedSatement 的好处

1.repareStatement()会先将 SQL 语句发送给数据库预编译。PreparedStatement 会引用着预编译后的结果。可以多次传入不同的参数给 PreparedStatement 对象并执行。减少 SQL 编译次数,提高效率。
2.安全性更高,没有 SQL 注入的隐患。
3.提高了程序的可读性。

使用 PreparedStatement 的步骤

1.编写 SQL 语句,未知内容使用?占位:“SELECT * FROM user WHERE name=? AND password=?”;
2.获得 PreparedStatement 对象
3.设置实际参数:setXxx(占位符的位置, 真实的值)
4.执行参数化 SQL 语句
5.关闭资源
在这里插入图片描述
使用 PreparedStatement 改写上面的登录程序,看有没有 SQL 注入的情况

package com.itheima;

import com.itheima.utils.JdbcUtils;

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

/**
 * 使用 PreparedStatement
 */
public class Demo8Login {
    //从控制台上输入的用户名和密码
    public static void main(String[] args) throws SQLException {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String name = sc.nextLine();
        System.out.println("请输入密码:");
        String password = sc.nextLine();
        login(name, password);
    }

    /**
     * 登录的方法
     *
     * @param name
     * @param password
     */
    private static void login(String name, String password) throws SQLException {
        Connection connection = JdbcUtils.getConnection();
        //写成登录 SQL 语句,没有单引号
        String sql = "select * from user where name=? and password=?";
        //得到语句对象
        PreparedStatement ps = connection.prepareStatement(sql);
        //设置参数
        ps.setString(1, name);
        ps.setString(2, password);
        ResultSet resultSet = ps.executeQuery();
        if (resultSet.next()) {
            System.out.println("登录成功:" + name);
        } else {
            System.out.println("登录失败");
        }
        //释放资源,子接口直接给父接口
        JdbcUtils.close(connection, ps, resultSet);
    }
}

表与类的关系
在这里插入图片描述

  • 案例:使用 PreparedStatement 查询一条数据,封装成一个学生 Student 对象
package com.itheima;

import com.itheima.entity.Student;
import com.itheima.utils.JdbcUtils;

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

public class Demo9Student {
    public static void main(String[] args) throws SQLException {
        //创建学生对象
        Student student = new Student();
        Connection connection = JdbcUtils.getConnection();
        PreparedStatement ps = connection.prepareStatement("select * from student where id=?");
        //设置参数
        ps.setInt(1, 2);
        ResultSet resultSet = ps.executeQuery();
        if (resultSet.next()) {
            //封装成一个学生对象
            student.setId(resultSet.getInt("id"));
            student.setName(resultSet.getString("name"));
            student.setGender(resultSet.getBoolean("gender"));
            student.setBirthday(resultSet.getDate("birthday"));
        }
        //释放资源
        JdbcUtils.close(connection, ps, resultSet);
        //可以数据
        System.out.println(student);
    }
}
  • 案例:将多条记录封装成集合 List,集合中每个元素是一个 JavaBean 实体类
    需求: 查询所有的学生类,封装成 List返回
    代码:
    在这里插入图片描述
package com.itheima;
import com.itheima.entity.Student;
import com.itheima.utils.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Demo10List {
    public static void main(String[] args) throws SQLException {
        //创建一个集合
        List<Student> students = new ArrayList<>();
        Connection connection = JdbcUtils.getConnection();
        PreparedStatement ps = connection.prepareStatement("select * from student");
        //没有参数替换
        ResultSet resultSet = ps.executeQuery();
        while (resultSet.next()) {
            //每次循环是一个学生对象
            Student student = new Student();
            //封装成一个学生对象
            student.setId(resultSet.getInt("id"));
            student.setName(resultSet.getString("name"));
            student.setGender(resultSet.getBoolean("gender"));
            student.setBirthday(resultSet.getDate("birthday"));
            //把数据放到集合中
            students.add(student);
        }
        //关闭连接
        JdbcUtils.close(connection, ps, resultSet);
        //使用数据
        for (Student stu : students) {
            System.out.println(stu);
        }
    }
}

PreparedStatement 执行 DML 操作

package com.itheima;

import com.itheima.utils.JdbcUtils;

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

public class Demo11DML {
    public static void main(String[] args) throws SQLException {
        //insert();
        //update();
        delete();
    }

    //插入记录
    private static void insert() throws SQLException {
        Connection connection = JdbcUtils.getConnection();
        PreparedStatement ps = connection.prepareStatement("insert into student
                values(null, ?, ?,?)");
        ps.setString(1, "小白龙");
        ps.setBoolean(2, true);
        ps.setDate(3, java.sql.Date.valueOf("1999-11-11"));
        int row = ps.executeUpdate();
        System.out.println("插入了" + row + "条记录");
        JdbcUtils.close(connection, ps);
    }

    //更新记录: 换名字和生日
    private static void update() throws SQLException {
        Connection connection = JdbcUtils.getConnection();
        PreparedStatement ps = connection.prepareStatement("update student set name=?, birthday=?
                where id = ? ");
        ps.setString(1, "黑熊怪");
        ps.setDate(2, java.sql.Date.valueOf("1999-03-23"));
        ps.setInt(3, 5);
        int row = ps.executeUpdate();
        System.out.println("更新" + row + "条记录");
        JdbcUtils.close(connection, ps);
    }
    
    //删除记录: 删除第 5 条记录
    private static void delete() throws SQLException {
        Connection connection = JdbcUtils.getConnection();
        PreparedStatement ps = connection.prepareStatement("delete from student where id=?");
        ps.setInt(1, 5);
        int row = ps.executeUpdate();
        System.out.println("删除了" + row + "条记录");
        JdbcUtils.close(connection, ps);
    }
}
发布了53 篇原创文章 · 获赞 56 · 访问量 1429

猜你喜欢

转载自blog.csdn.net/duwenyanxiaolaji/article/details/104060102