javaJDBC

import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.*;
import java.sql.Connection;


public class Mtest2Demo {
	/*
	 * JDBC 是一种用于执行SQL语句的Java API
	 * JDBC 可以为多种关系数据库提供统一访问入口。
	 * JDBC 由一组java工具类接口组成。
	 */
	/*
	 * 注册驱动
	 * Class.forName("com.mysql.jdbc.Driver");
	 */
	
	public void Login(String username,String password) throws ClassNotFoundException, SQLException {
		//1.注册驱动
		Class.forName("com.mysql.jdbc.Driver");
		//2.获取连接
	    Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/study","root","root");
		//3.创建执行sql语句的对象
	    Statement statement=connection.createStatement();
	    //4书写sql语句;
	    String sqlString="select *from login where "+"username='"+username+"' and password='"+password+"'";
	    //目前这种方式并不是太好
	    //5执行sql语句;返回给结果集ResultSet;
		ResultSet rSet=statement.executeQuery(sqlString);
		//6.对结果集进行处理
		if(rSet.next()) {//如果存在sqlString的情况返回true否则返回false;
			System.out.println("恭喜你!"+username+"登录成功");
			System.out.println(sqlString);
		}
		else {
			System.out.println("抱歉!登录错误");
		}
		if(rSet!=null) rSet.close();
		if(statement!=null) statement.close();
		if(connection!=null) connection.close();
	}
	
	public static void main(String[] args) throws ClassNotFoundException, SQLException {
		Mtest2Demo mtest2Demo=new Mtest2Demo();
		String usernameString="zyz 'or'";//数据库的注入问题,数据库中没有这个记录,但是产生了数据库的注入问题
		String paString="1234567";
		mtest2Demo.Login(usernameString,paString);
	}
}

  

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


public class Mtes3Demo {
	public void Login(String username,String password) throws ClassNotFoundException, SQLException {
		//1.注册驱动
		Class.forName("com.mysql.jdbc.Driver");
		//2.获取连接
	    Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/study","root","root");
		//3.创建执行sql语句的对象
	    Statement statement=connection.createStatement();
	    /*
	     * PrepareStatement叫预编译
	     * Prepare'Statement是Statement的子接口,可以使用PrepareStatement来替换Statement
	     * 防止SQL攻击;
	     * 提高代码的可读性,以可维护性
	     * 提高效率
	     * 
	     */
	    //4书写sql语句;
	    String sqlString="select *from login where "+"username='"+username+"' and password='"+password+"'";
	    //目前这种方式并不是太好
	    //5执行sql语句;返回给结果集ResultSet;
		ResultSet rSet=statement.executeQuery(sqlString);
		//6.对结果集进行处理
		if(rSet.next()) {//如果存在sqlString的情况返回true否则返回false;
			System.out.println("恭喜你!"+username+"登录成功");
			System.out.println(sqlString);
		}
		else {
			System.out.println("抱歉!登录错误");
		}
		if(rSet!=null) rSet.close();
		if(statement!=null) statement.close();
		if(connection!=null) connection.close();
	}
	
	//PrepareStatement使用方法
	//推荐使用这种方式。
	public void Login2(String username,String password) throws SQLException, ClassNotFoundException {
			try {
				// TODO: handle exception
				//1.注册驱动
				Class.forName("com.mysql.jdbc.Driver");
				//2.获取连接
			    Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/study","root","root");
				//3.创建执行sql语句的对象
			    //3 书写sql语句;
			    String sqlString="select *from login where "+"username='"+username+"' and password='"+password+"'";
			    //目前这种方式并不是太好
			    //4创建预处理对象
			    PreparedStatement pstmt = connection.prepareStatement(sqlString);
			    //5 设置参数占位符
			    pstmt.setString(1, username);
			    pstmt.setString(2, password);
			    
			    //6执行查询语句
			    ResultSet rSet=pstmt.executeQuery();
			    
				if(rSet.next()) {//如果存在sqlString的情况返回true否则返回false;
					System.out.println("恭喜你!"+username+"登录成功");
					System.out.println(sqlString);
				}
				else {
					System.out.println("抱歉!登录错误");
				}
				if(rSet!=null) rSet.close();
				if(pstmt!=null) pstmt.close();
				if(connection!=null) connection.close();
			} catch (Exception e) {
				System.out.println("数据库正受到攻击!!!!");	
			}	
				
	}
	
	public static void main(String[] args) throws ClassNotFoundException, SQLException {
		Mtes3Demo mtes3Demo=new Mtes3Demo();
		String username="zyz 'or'";
		String password ="1234567";
		mtes3Demo.Login(username, password);//恭喜你!zyz 'or'登录成功。存在注入问题
		mtes3Demo.Login2(username, password);//解决注入问题
	}

}

  

一纸高中万里风,寒窗读破华堂空。 莫道长安花看尽,由来枝叶几相同?
原文地址:https://www.cnblogs.com/byczyz/p/11154969.html