JDBC02-ResultSet、Statement、PreparedStatement介绍及使用

1. ResultSet[结果集]

1.1 基本介绍

在这里插入图片描述
在这里插入图片描述

细节注意:
光标可以理解为在标题行,但是是不能够获取标题的

1.2 应用实例

底层图示
在这里插入图片描述

package com.hspedu.jdbc.resultset_;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

/**
* @version 1.0
* 演示 select 语句返回 ResultSet ,并取出结果
*/
@SuppressWarnings({"all"})
public class ResultSet_ {
	public static void main(String[] args) throws Exception {
		//通过 Properties 对象获取配置文件的信息
		Properties properties = new Properties();
		properties.load(new FileInputStream("src\\mysql.properties"));
		
		//获取相关的值
		String user = properties.getProperty("user");
		String password = properties.getProperty("password");
		String driver = properties.getProperty("driver");
		String url = properties.getProperty("url");
		//1. 注册驱动
		Class.forName(driver);//建议写上
		//2. 得到连接
		Connection connection = DriverManager.getConnection(url, user, password);
		//3. 得到 Statement
		Statement statement = connection.createStatement();
		//4. 组织 SqL
		String sql = "select id, name , sex, borndate from actor";
		//执行给定的 SQL 语句,该语句返回单个 ResultSet 对象
		/*
			+----+-----------+-----+---------------------+
			| id | name | sex | borndate |
			+----+-----------+-----+---------------------+-------+
			| 4 | 刘德华 | 男 | 1970-12-12 00:00:00 |
			| 5 | jack | 男 | 1990-11-11 00:00:00 |
			+----+-----------+-----+---------------------+-------+
		*/
		/*
			阅读 debug 代码 resultSet 对象的结构,如上图图片
		*/
		ResultSet resultSet = statement.executeQuery(sql);
		//5. 使用 while 取出数据
		while (resultSet.next()) { // 让光标向后移动,如果没有更多行,则返回 false
			int id = resultSet.getInt(1); //获取该行的第 1 列
			//int id1 = resultSet.getInt("id"); 通过列名来获取值, 推荐
			String name = resultSet.getString(2);//获取该行的第 2 列
			String sex = resultSet.getString(3);
			Date date = resultSet.getDate(4);
			System.out.println(id + "\t" + name + "\t" + sex + "\t" + date);
		}
		
		//6. 关闭连接
		resultSet.close();
		statement.close();
		connection.close();
	}
}

2. Statement

2.1 基本介绍

在这里插入图片描述

2.2 SQL注入的问题演示

-- 演示 sql 注入
-- 创建一张表
CREATE TABLE admin ( -- 管理员表
NAME VARCHAR(32) NOT NULL UNIQUE, pwd VARCHAR(32) NOT NULL DEFAULT '') CHARACTER SET utf8; 

-- 添加数据
INSERT INTO admin VALUES('tom', '123'); 

-- 查找某个管理是否存在
SELECT *
FROM admin
WHERE NAME = 'tom' AND pwd = '123'

-- SQL
-- 输入用户名 为 1' or
-- 输入万能密码 为 or '1'= '1
SELECT *
FROM admin
WHERE NAME = '1' OR' AND pwd = 'OR '1'= '1' 

SELECT * FROM admin

应用实例
在这里插入图片描述

package com.hspedu.jdbc.statement_;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.sql.*;
import java.util.Properties;
import java.util.Scanner;

/**
* 演示 statement 的注入问题
*/
@SuppressWarnings({"all"})
public class Statement_ {
	public static void main(String[] args) throws Exception {
	
		Scanner scanner = new Scanner(System.in);
		//让用户输入管理员名和密码
		System.out.print("请输入管理员的名字: "); //next(): 当接收到 空格或者 '就是表示结束
		String admin_name = scanner.nextLine(); // 老师说明,如果希望看到 SQL 注入,这里需要用 nextLine
		System.out.print("请输入管理员的密码: ");
		String admin_pwd = scanner.nextLine();
		
		//通过 Properties 对象获取配置文件的信息
		Properties properties = new Properties();
		properties.load(new FileInputStream("src\\mysql.properties"));
		
		//获取相关的值
		String user = properties.getProperty("user");
		String password = properties.getProperty("password");
		String driver = properties.getProperty("driver");
		String url = properties.getProperty("url");
		
		//1. 注册驱动
		Class.forName(driver);//建议写上
		
		//2. 得到连接
		Connection connection = DriverManager.getConnection(url, user, password);
		
		//3. 得到 Statement
		Statement statement = connection.createStatement();
		
		//4. 组织 SqL
		String sql = "select name , pwd from admin where name ='" + admin_name + "' and pwd = '" + admin_pwd + "'";
		ResultSet resultSet = statement.executeQuery(sql);
		
		if (resultSet.next()) { //如果查询到一条记录,则说明该管理存在
			System.out.println("恭喜, 登录成功");
		} else {
			System.out.println("对不起,登录失败");
		}
		
		//关闭连接
		resultSet.close();
		statement.close();
		connection.close();
	}
}

3. PreparedStatement

3.1 基本介绍

在这里插入图片描述

3.2 预处理好处

在这里插入图片描述
在这里插入图片描述

3.3 应用案例

1)

package com.hspedu.jdbc.preparedstatement_;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.sql.*;
import java.util.Properties;
import java.util.Scanner;

/**
* 演示 PreparedStatement 使用
*/

@SuppressWarnings({"all"})
public class PreparedStatement_ {
	public static void main(String[] args) throws Exception {
		//看 PreparedStatement 类图,如上
		Scanner scanner = new Scanner(System.in);
		
		//让用户输入管理员名和密码
		System.out.print("请输入管理员的名字: "); //next(): 当接收到 空格或者 '就是表示结束
		String admin_name = scanner.nextLine(); // 老师说明,如果希望看到 SQL 注入,这里需要用 nextLine
		System.out.print("请输入管理员的密码: ");
		String admin_pwd = scanner.nextLine();
		
		//通过 Properties 对象获取配置文件的信息
		Properties properties = new Properties();
		properties.load(new FileInputStream("src\\mysql.properties"));
		//获取相关的值
		String user = properties.getProperty("user");
		String password = properties.getProperty("password");
		String driver = properties.getProperty("driver");
		String url = properties.getProperty("url");
		
		//1. 注册驱动
		Class.forName(driver);//建议写上
		
		//2. 得到连接
		Connection connection = DriverManager.getConnection(url, user, password);
		
		//3. 得到 PreparedStatement
		//3.1 组织 SqL , Sql 语句的 ? 就相当于占位符
		String sql = "select name , pwd from admin where name =? and pwd = ?";
		
		//3.2 preparedStatement 对象实现了 PreparedStatement 接口的实现类的对象
		PreparedStatement preparedStatement = connection.prepareStatement(sql);
		
		//3.3 给 ? 赋值
		preparedStatement.setString(1, admin_name);
		preparedStatement.setString(2, admin_pwd);
		
		//4. 执行 select 语句使用 executeQuery
		// 如果执行的是 dml(update, insert ,delete) executeUpdate()
		// 这里执行 executeQuery ,不要在写 executeQuery(sql)
		ResultSet resultSet = preparedStatement.executeQuery();
		if (resultSet.next()) { //如果查询到一条记录,则说明该管理存在
			System.out.println("恭喜, 登录成功");
		} else {
			System.out.println("对不起,登录失败");
		}


		//关闭连接
		resultSet.close();
		preparedStatement.close();
		connection.close();
	}
}

2)

package com.hspedu.jdbc.preparedstatement_;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;
import java.util.Scanner;

/**
* 演示 PreparedStatement 使用 dml 语句
*/

@SuppressWarnings({"all"})
public class PreparedStatementDML_ {
	public static void main(String[] args) throws Exception {
		//看 PreparedStatement 类图
		Scanner scanner = new Scanner(System.in);
		
		//让用户输入管理员名和密码
		System.out.print("请输删除管理员的名字: "); //next(): 当接收到 空格或者 '就是表示结束
		String admin_name = scanner.nextLine(); // 老师说明,如果希望看到 SQL 注入,这里需要用 nextLine
		
		// System.out.print("请输入管理员的新密码: ");
		// String admin_pwd = scanner.nextLine();
		//通过 Properties 对象获取配置文件的信息
		Properties properties = new Properties();
		properties.load(new FileInputStream("src\\mysql.properties"));
		//获取相关的值
		String user = properties.getProperty("user");
		String password = properties.getProperty("password");
		String driver = properties.getProperty("driver");
		String url = properties.getProperty("url");
		
		//1. 注册驱动
		Class.forName(driver);//建议写上
		//2. 得到连接
		Connection connection = DriverManager.getConnection(url, user, password);
		
		//3. 得到 PreparedStatement
		//3.1 组织 SqL , Sql 语句的 ? 就相当于占位符
		//添加记录
		//String sql = "insert into admin values(?, ?)";
		//String sql = "update admin set pwd = ? where name = ?";
		String sql = "delete from admin where name = ?";
		
		//3.2 preparedStatement 对象实现了 PreparedStatement 接口的实现类的对象
		PreparedStatement preparedStatement = connection.prepareStatement(sql);
		
		//3.3 给 ? 赋值
		preparedStatement.setString(1, admin_name);
		//preparedStatement.setString(2, admin_name);
		
		//4. 执行 dml 语句使用 executeUpdate
		int rows = preparedStatement.executeUpdate();
		System.out.println(rows > 0 ? "执行成功" : "执行失败");
		//关闭连接
		preparedStatement.close();
		connection.close();
	}
}

4. JDBC 的相关 API 小结

在这里插入图片描述
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值