jdbc入门(三) Statement 和 prepareStatement

版权声明:https://blog.csdn.net/qq_37618797 https://blog.csdn.net/qq_37618797/article/details/82180023

我们前面使用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使用的是参数设置,就不会有这个问题。

猜你喜欢

转载自blog.csdn.net/qq_37618797/article/details/82180023