sql and&or用法 实现jsp邮箱或者用户名登录

本文介绍了一个使用Java进行数据库连接并验证用户登录信息的过程。通过加载MySQL驱动,建立连接,并使用PreparedStatement执行SQL语句来检查用户名和密码的有效性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

     try {
            //链接字符串
             String driver ="com.mysql.jdbc.Driver";
            //加载驱动
             Class.forName(driver);
             //建立数据库连接
             Connection   con = (Connection) DriverManager.getConnection(url,user, dbpassword);

            if(con==null){
                System.out.println("can't open JDBConnection");
            }
            //sql语句
            String sql = "select * from user.user where (username=? or email=? )and password=?";

            PreparedStatement pstmt=(PreparedStatement) con.prepareStatement(sql); 
            pstmt.setString(1,this.Name); //第1个?字符的替换
            pstmt.setString(2,this.Name); //第2个?字符的替换
            pstmt.setString(3,this.Password);//第3个?字符的替换
            //pstmt.setString(4,this.Password);//第4个?字符的替换
            System.out.println(sql);
            System.out.println(getName());
            System.out.println(getPassword());
            ResultSet rs=pstmt.executeQuery();//执行语句后,产生单个结果集的语句
            if(rs.next())
            {
                System.out.println(rs.getString("username"));
                System.out.println("PASS");
                result=true;
            }
            else
            {
                System.out.println("FALL");
                result=false;
            }
            pstmt.close();//3个关闭PreparedStatement,ResultSet,Connection
            rs.close();
            con.close();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;

总结:测试的时候看看是不是测试的用户名写错了= =

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值