jdbc入门(三) Statement 和 prepareStatement

4 篇文章 0 订阅

我们前面使用Statement创建sql语句对象,但是这样存在一个bug。

这是数据库中的账号和密码。

package com.ck.jdbc;

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

public class Test4 {

	public static void main(String[] args) {

		ResultSet rs = null;
		Connection con = null;
		Statement stmt = null;
		try {
			Class.forName("oracle.jdbc.OracleDriver");
			con = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORCL","scott","a");
			
			//登入
			Scanner input = new Scanner(System.in);
			
			System.out.print("请输入QQ号:");
			String id = input.nextLine();
			System.out.print("请输入密码:");
			String pwd = input.nextLine();
			//存在bug 密码为 XXX ' or '1' = '1 形式时可以登入    称之为注入攻击
			stmt = con.createStatement();
			String  sql = "select * from test where id = '"+ id +"'and pwd = '"+ pwd +"'";
			rs = stmt.executeQuery(sql);			
			
			if(rs.next()){
				System.out.println("登入成功");
			}else{
				System.out.println("账户或密码错误");
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally{
			//关闭连接
			if(rs != null){
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if(stmt != null){
				try {
					stmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				};
			}
			if(con != null){
				try {
					con.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

我们输入正确的账号和密码,登入成功。

但是我们这样输入密码,同样登入成功,我们管这种叫做注入攻击。

现在我们采用prepareStatement(预编译)来解决这个问题

package com.ck.jdbc;

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

public class Test4 {

	public static void main(String[] args) {

		ResultSet rs = null;
		Connection con = null;
		PreparedStatement pstmt = null;
		try {
			Class.forName("oracle.jdbc.OracleDriver");
			con = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORCL","scott","a");
			
			//登入
			Scanner input = new Scanner(System.in);
			
			System.out.print("请输入QQ号:");
			String id = input.nextLine();
			System.out.print("请输入密码:");
			String pwd = input.nextLine();

			//改进 使用预处理
			//? 占位符 到时会将 ?直接替换为设置的值
			String  sql = "select * from qq where qname = ? and pwd = ?";
			pstmt = con.prepareStatement(sql);
			//设值
			pstmt.setString(1, id);
			pstmt.setString(2, pwd);

			rs = pstmt.executeQuery();	
			
			if(rs.next()){
				System.out.println("登入成功");
			}else{
				System.out.println("账户或密码错误");
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally{
			//关闭连接
			if(rs != null){
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if(pstmt != null){
				try {
					pstmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				};
			}
			if(con != null){
				try {
					con.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

这样我们就有效的防止了注入攻击。

 

总结:

PreparedStatement的优点

1-参数设置
Statement 需要进行字符串拼接,可读性和维护性比较差
2-性能表现
PreparedStatement有预编译机制,性能比Statement更快

3-防止SQL注入式攻击
假设id是用户提交来的数据

使用Statement就需要进行字符串拼接
拼接出来的语句是:select * from test where id = '807156235'  OR 1=1

因为有OR 1=1,这是恒成立的
那么就会把所有的账户都查出来,而不只当前这一个账户
如果test表里的数据是海量的,比如几百万条,把这个表里的数据全部查出来
会让数据库负载变高,CPU100%,内存消耗光,响应变得极其缓慢
而PreparedStatement使用的是参数设置,就不会有这个问题。

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值