sql注入的原因:
目前的sql语句是字符串拼接完成的, 里面的传入的参数如果有关键字如or等, 就会把他当作关键字处理导致的
@Test
public void demo03() {
// 目的: 演示 sql 注入, 非法登录
// 模拟获取用户自己填写的 用户名和密码
// 模拟登录成功
String usernme = "aaa";
String password = "123";
// 模拟用户名正确, 密码不对
//String usernme = "aaa' -- ";
// String usernme = "aaa' #";
// String password = "123456";
// 模拟用户名和密码都不正确
// String usernme = "sdfsfd";
// String password = "sdfsdfs' or '1'='1";
// 登录业务: 根据用户名和密码查询用户信息
// 如果查到了, 表示登录成功, 显示欢迎信息; 如果没有查到, 表示登录失败, 提示错误
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
// 1 获取连接
conn = JDBCUtils.getConnection();
// 2 获取执行sql的对象
String sql = "select * from user where username=? and password=?";
System.out.println("sql = " + sql);
stmt = conn.prepareStatement(sql);
// java.sql.SQLException: No value specified for parameter 1
// 因为sql语句中有?占位符, 应该设置对应的值, 注意下标从1开始的
stmt.setString(1, usernme);
stmt.setString(2, password);
// 3 执行sql 获取结果
rs = stmt.executeQuery();
// 4 处理结果
// 因为 用户名是唯一的, 所以 要么有一行结果, 要么没有, 所以 只判断一次就可以了 if
if(rs.next()) {
// 表示查到了, 表示登录成功, 显示欢迎信息
System.out.println("登录成功, 欢迎访问本网站! " + rs.getString("username"));
} else {
// 表示没有查到, 表示登录失败, 显示失败信息
System.out.println("登录失败, 用户名或密码错误!");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 5 释放资源
JDBCUtils.close(conn, stmt, rs);
}
}
11 通过preparedStatement 完成增删改
@Test
public void demo01() {
// 需求: 向student表新增一条记录
Connection conn = null;
PreparedStatement stmt = null;
try {
// 1 获取连接
conn = JDBCUtils.getConnection();
// 2 获取执行sql的对象
String sql = "insert into student(name, sex, birthday) values(?, ?, ?)";
stmt = conn.prepareStatement(sql);
// 2.1 如果有?, 就必须设置值
stmt.setString(1, "迪丽热巴");
stmt.setBoolean(2,false);
stmt.setDate(3, java.sql.Date.valueOf("2000-1-1"));
// 3 执行sql 获取结果
int count = stmt.executeUpdate();
// 4 处理结果
System.out.println("影响的条数是: " + count);
} catch (Exception e) {
e.printStackTrace();
} finally {
// 5 释放资源
JDBCUtils.close(conn, stmt);
}
}
@Test
public void demo02() {
// 需求: 在student表修改一条记录
Connection conn = null;
PreparedStatement stmt = null;
try {
// 1 获取连接
conn = JDBCUtils.getConnection();
// 2 获取执行sql的对象
String sql = "update student set name=?, sex=?, birthday=? where id=?";
stmt = conn.prepareStatement(sql);
// 2.1 如果有?, 就必须设置值
stmt.setString(1, "刘德华");
stmt.setBoolean(2,true);
stmt.setDate(3, java.sql.Date.valueOf("1965-10-1"));
stmt.setInt(4, 5);
// 3 执行sql 获取结果
int count = stmt.executeUpdate();
// 4 处理结果
System.out.println("影响的条数是: " + count);
} catch (Exception e) {
e.printStackTrace();
} finally {
// 5 释放资源
JDBCUtils.close(conn, stmt);
}
}
@Test
public void demo03() {
// 需求: 在student表 根据id删除一条记录
Connection conn = null;
PreparedStatement stmt = null;
try {
// 1 获取连接
conn = JDBCUtils.getConnection();
// 2 获取执行sql的对象
String sql = "delete from student where id=?";
stmt = conn.prepareStatement(sql);
// 2.1 如果有?, 就必须设置值
stmt.setInt(1, 5);
// 3 执行sql 获取结果
int count = stmt.executeUpdate();
// 4 处理结果
System.out.println("影响的条数是: " + count);
} catch (Exception e) {
e.printStackTrace();
} finally {
// 5 释放资源
JDBCUtils.close(conn, stmt);
}
}
12 将查询的一条记录结果封装到 pojo对象
package cn.itcast.dao;
import cn.itcast.pojo.Student;
import cn.itcast.utils.JDBCUtils;
import org.junit.Test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
/**
* 使用prepaaredStatement执行查询
*/
public class JDBCTest06 {
@Test
public void demo01() {
// 需求: 将查询到一条记录封装到pojo对象
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
// 声明一个参数, 保留结果
Student student = null;
try {
// 1 获取连接
conn = JDBCUtils.getConnection();
// 2 获取执行sql的对象
String sql = "select * from student where id=?";
stmt = conn.prepareStatement(sql);
// 设置参数
stmt.setInt(1, 55);
// 3 执行sql 获取结果
rs = stmt.executeQuery();
// 4 处理结果
if(rs.next()) {
student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setSex(rs.getBoolean("sex"));
student.setBirthday(rs.getDate("birthday"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 5 释放资源
JDBCUtils.close(conn, stmt, rs);
}
// 6 打印查询的结果
System.out.println("查询的结果: " + student);
}
@Test
public void demo02() {
}
}
13 将查询的所有记录结果封装到 List 中
@Test
public void demo02() {
// 需求: 将查询的所有记录结果封装到 List<pojo> 中
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
// 声明一个变量, 保留结果
List<Student> studentList = new ArrayList<Student>();
try {
// 1 获取连接
conn = JDBCUtils.getConnection();
// 2 获取执行sql的对象
String sql = "select * from student";
stmt = conn.prepareStatement(sql);
// 3 执行sql 获取结果
rs = stmt.executeQuery();
// 4 处理结果
while(rs.next()) {
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setSex(rs.getBoolean("sex"));
student.setBirthday(rs.getDate("birthday"));
studentList.add(student);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 5 释放资源
JDBCUtils.close(conn, stmt, rs);
}
// 6 打印查询的结果
System.out.println("查询的结果: " + studentList);
for (Student student : studentList) {
System.out.println("=============================");
System.out.println(student);
}
}