登录功能的设计

1 使用PreparedStatement语句求(账号)跟(密码)的设计


    @Test
    public void tast1() throws Exception {
        Connection conn = JdbcUtil.INSTANCE.getConn();
        String sql = "SELECT * FROM t_user where username = ? and password = ?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1, "admin");   //输入账号admin
        ps.setString(2, "1");       //输入密码1
        ResultSet rs = ps.executeQuery();
        if(rs.next()){
            System.out.println("ok啦....");
        }else{
            System.out.println("nook啦....");
        }
        JdbcUtil.INSTANCE.close(conn, ps, rs);
    }

2 使用Statement语句求(账号)跟(密码)的设计

@Test
    public void tast1_1() throws Exception {
        Connection conn = JdbcUtil.INSTANCE.getConn();
        Statement st = conn.createStatement();
        //直接输入账号admin 跟 密码1
        String sql = "SELECT * FROM t_user where username = 'admin' and password = '1'";
        ResultSet rs = st.executeQuery(sql);
        if(rs.next()){
            System.out.println("ok啦....");
        }else{
            System.out.println("nook啦....");
        }
        JdbcUtil.INSTANCE.close(conn, st, rs);
    }

3 使用PreparedStatement中 count(*) 列的设计

@Test
    public void tast2() throws Exception {
        Connection conn = JdbcUtil.INSTANCE.getConn();
        String sql = "SELECT count(*) FROM t_user where username = ? and password = ?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1, "admin");
        ps.setString(2, "1");
        ResultSet rs = ps.executeQuery();
        if(rs.next()){
            Long i = rs.getLong(1);
            if(i == 1){
                System.out.println("ok啦....");//此时查找出来的count(*)列的参数为1密码正确
            }else{
                System.out.println("nook啦....");//此时查询出来的count(*)参数为0,密码不正确
            }
        }
        JdbcUtil.INSTANCE.close(conn, ps, rs);
    }

4 使用PreparedStatement 告诉(账号)求(密码)列的设计

@Test
    public void tast3() throws Exception {
        Connection conn = JdbcUtil.INSTANCE.getConn();
        String sql = "SELECT password FROM t_user where username = ?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1, "admin");     //此处输入账号
        ResultSet rs = ps.executeQuery();
        if(rs.next()){
            String pwd = rs.getString(1);
            if(pwd.equals("1")){     //此处输入密码
                System.out.println("ok啦....");//密码正确
            }else{
                System.out.println("错误啦....");//密码错误
            }
        }else{
            System.out.println("没有该账号");
        }
        JdbcUtil.INSTANCE.close(conn, ps, rs);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值