拼接带来的sql注入问题(拼接sql)


- package com.fs.db;
-
- import com.fs.util.JdbcUtil;
-
- import java.sql.*;
- import java.util.Date;
- import java.util.Scanner;
-
- /**
- * 模拟SQL攻击
- */
- public class Demo4 {
- public static void main(String[] args) {
- boolean flag = login2("zs", "zs");
- //SQL攻击 SQL注入
- //boolean flag = login2("asdasd' or '1' = '1", "zs' or '1'='1");
- if(flag){
- System.out.println("登录成功");
- }else{
- System.out.println("登录失败");
- }
-
- }
-
- /**
- * 登录方法
- * @param username 用户名
- * @param password 密码
- * @return true登录成功, false: 登录失败
- */
- public static boolean login(String username,String password){
- Connection conn = null;
- Statement statement = null;
- ResultSet resultSet = null;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- String url = "jdbc:mysql://localhost:3306/test2?useUnicode=true&characterEncoding=utf8&useSSL=false";
- conn = DriverManager.getConnection(url, "root", "123");
- statement = conn.createStatement();
-
- String sql = "select * from user where username = '"+username+"' and password='"+password+"'";
- System.out.println(sql);
- resultSet = statement.executeQuery(sql);
- return resultSet.next();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException throwables) {
- throwables.printStackTrace();
- }finally{
- //倒序关
- try {
- if(resultSet != null) {
- resultSet.close();
- }
- if(statement != null) {
- statement.close();
- }
- if(conn != null) {
- conn.close();
- }
- } catch (SQLException throwables) {
- throwables.printStackTrace();
- }
- }
- return false;
- }
-
-
- /**
- * 使用PreparedStatement
- * 登录方法
- * @param username 用户名
- * @param password 密码
- * @return true登录成功, false: 登录失败
- */
- public static boolean login2(String username,String password){
- Connection conn = null;
- PreparedStatement pstmt = null;
- ResultSet resultSet = null;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- String url = "jdbc:mysql://localhost:3306/test2?useUnicode=true&characterEncoding=utf8&useSSL=false";
- conn = DriverManager.getConnection(url, "root", "123");
- //SQL语句使用?占位
- String sql = "select * from user where username = ? and password=? ";
- pstmt = conn.prepareStatement(sql);
-
- //给?赋值setXxx(?序号,值) 从1开始 通用类型: setObject()
- pstmt.setString(1,username);
- pstmt.setString(2,password);
-
- //执行sql 调用无参的方法
- resultSet = pstmt.executeQuery();
- return resultSet.next();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException throwables) {
- throwables.printStackTrace();
- }finally{
- //倒序关
- try {
- if(resultSet != null) {
- resultSet.close();
- }
- if(pstmt != null) {
- pstmt.close();
- }
- if(conn != null) {
- conn.close();
- }
- } catch (SQLException throwables) {
- throwables.printStackTrace();
- }
- }
- return false;
- }
-
- /**
- * 使用PreparedStatement
- * 登录方法
- * @param username 用户名
- * @param password 密码
- * @return true登录成功, false: 登录失败
- */
- public static boolean login3(String username,String password){
- Connection conn = null;
- PreparedStatement pstmt = null;
- ResultSet resultSet = null;
- try {
- conn = JdbcUtil.getConnection();
- //SQL语句使用?占位
- String sql = "select * from user where username = ? and password=? ";
- pstmt = conn.prepareStatement(sql);
-
- //给?赋值setXxx(?序号,值) 从1开始 通用类型: setObject()
- pstmt.setString(1,username);
- pstmt.setString(2,password);
-
- //执行sql 调用无参的方法
- resultSet = pstmt.executeQuery();
- return resultSet.next();
- } catch (SQLException throwables) {
- throwables.printStackTrace();
- }finally{
- JdbcUtil.close(conn,pstmt,resultSet);
- }
- return false;
- }
-
- }
和com包同级
jdbc.driverclass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test2?useUnicode=true&characterEncoding=utf8&useSSL=false
jdbc.username=root
jdbc.password=123
jdbc的优化 代码重复问题 参数优化问题

第一步:把数据库四大参数放到properties文件
jdbc.driverclass=com.mysql.jdbc.Driver
jdbc.url=jdbc :mysql : / / localhost:3306/test2?
useUnicode=true&characterEncoding=utf8&useSSL=falsejdbc.username=root
jdbc.password=123
第二步:编写一个jdbc的工具类,封装重复代码 (工具类不要让别人可以new出来,所以搞成静态的,返回一个connection对象,直接用类型.属性调用方法和属性)
- package com.fs.util;
-
- import java.io.IOException;
- import java.io.InputStream;
- import java.sql.*;
- import java.util.Properties;
-
- /**
- * JDBC的工具类
- */
- public class JdbcUtil {
- private static Properties props = new Properties();
- static{
- //加载db.properties文件
- try {
- //写的绝对路径, 一旦项目拷贝到另外一台电脑,路径可能错误的
- //FileInputStream fis = new FileInputStream("C:\\java6\\jdbc\\code\\demo1\\src\\db.properties");
-
- //使用相对路径, 相对于src目录
- //getClassLoader()得到该类的类加载器
- InputStream in = JdbcUtil.class.getClassLoader().getResourceAsStream("db.properties");
- props.load(in);
- Class.forName(props.getProperty("jdbc.driverclass"));
- } catch (IOException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
-
- }
-
-
- //得到连接的方法
- public static Connection getConnection() throws SQLException {
- return DriverManager.getConnection(props.getProperty("jdbc.url"), props.getProperty("jdbc.username"), props.getProperty("jdbc.password"));
- }
-
- //关闭资源的方法
- public static void close(Connection conn, PreparedStatement pstmt, ResultSet resultSet){
- try {
- if(resultSet != null) {
- resultSet.close();
- }
- if(pstmt != null) {
- pstmt.close();
- }
- if(conn != null) {
- conn.close();
- }
- } catch (SQLException throwables) {
- throwables.printStackTrace();
- }
- }
-
- }