zoukankan      html  css  js  c++  java
  • JDBC的SQL注入漏洞分析和解决

    1.1 JDBC的SQL注入漏洞分析和解决

    1.1.1 SQL注入漏洞分析

    在这里插入图片描述

    1.1.2 SQL注入漏洞解决

    需要采用PreparedStatement对象解决SQL注入漏洞。这个对象将SQL预先进行编译,使用?作为占位符。? 所代表内容是SQL所固定。再次传入变量(包含SQL的关键字)。这个时候也不会识别这些关键字。

    public class UserDao {
    	
    	public boolean login(String username,String password){
    		Connection conn = null;
    		PreparedStatement pstmt = null;
    		ResultSet rs = null;
    		// 定义一个变量:
    		boolean flag = false;
    		try{
    			// 获得连接:
    			conn = JDBCUtils.getConnection();
    			// 编写SQL语句:
    			String sql = "select * from user where username = ? and password = ?";
    			// 预编译SQL
    			pstmt = conn.prepareStatement(sql);
    			// 设置参数:
    			pstmt.setString(1, username);
    			pstmt.setString(2, password);
    			// 执行SQL语句:
    			rs = pstmt.executeQuery();
    			if(rs.next()){
    				// 说明根据用户名和密码可以查询到这条记录
    				flag = true;
    			}
    		}catch(Exception e){
    			e.printStackTrace();
    		}finally{
    			JDBCUtils.release(rs, pstmt, conn);
    		}
    		return flag;
    	}
    
    

    本文来自博客园,作者:兮动人,转载请注明原文链接:https://www.cnblogs.com/xdr630/p/15254872.html

  • 相关阅读:
    rpc rmi http
    理解Global interpreter lock
    maven scope含义的说明
    实现图片缩放
    实现在edittext中任意插入图片
    上传图片或文件到服务器端
    onResume
    关于Context
    android bitmap compress
    saveFile()方法
  • 原文地址:https://www.cnblogs.com/xdr630/p/15254872.html
Copyright © 2011-2022 走看看