2021-11-14

登录案例

注入问题的解决

package javaweb.com.JDBC;

import java.sql.*;
import java.util.Scanner;

//注入问题的解决
//利用PreparedStatement解决注入问题
public class Login2 {
    public static void main(String[] args) {
        Scanner sc =new Scanner(System.in);
        System.out.printf("用户名:");
        String username = sc.nextLine();
        System.out.printf("密码:");
        String password = sc.nextLine();

        Boolean flag = new Login2().login1(username,password);

        if (flag)
        {
            System.out.printf("登陆成功!");
        }
        else
        {
            System.out.printf("账号或密码输入错误!");
        }
    }

    public Boolean login1(String username,String password)
    {
        if ((username==null)||(password==null))
        {
            return false;
        }
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtilsDemo.getConnection();

            String sql = "select * from login where username = ? and password = ?";

            pstmt = conn.prepareStatement(sql);

//            给?号赋值
            pstmt.setString(1,username);
            pstmt.setString(2,password);

            rs = pstmt.executeQuery();

            return rs.next();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JdbcUtilsDemo.close(rs,pstmt,conn);
        }

        return false;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值