SQL注入:
SQL存在漏洞,会被攻击,导致数据泄露;
SQL会被拼接 :or 1=1
代码案例:
import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; /** * 测试SQL注入问题: */ public class SQL注入 { public static void main(String[] args) { //login("demmo","123456"); //正常登录 login(" 'or '1=1"," 'or '1=1 ");//SQL注入 } //登录 public static void login(String username,String password){ Connection conn =null; Statement st=null; ResultSet rs=null; try { conn = JdbcUtils.getConnection();//获取连接 st=conn.createStatement();//创建SQL执行对象 //SELECT * FROM `users` WHERE `NAME`='demmo' AND `PASSWORD`='123456' //SELECT * FROM `users` WHERE `NAME`=' demmo'or '1=1 ' AND `PASSWORD`=' or '1=1 ' String sql="SELECT * FROM `users` WHERE `NAME`='"+ username+"'" + " AND `PASSWORD`='"+ password+"' "; rs=st.executeQuery(sql); while (rs.next()){ System.out.println("password="+rs.getObject("PASSWORD")); System.out.println("name="+rs.getObject("NAME")); System.out.println("================================"); } } catch (SQLException e) { e.printStackTrace(); }finally { JdbcUtils.release(conn,st,rs); } } }