关于jsp和sql的问题java web应用程序

2admgd59  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(360)

我最近正在制作一个javaweb应用程序,它必须关注crud(你知道的)。但是我被读、更新和删除操作(三个操作)困住了。只有创建操作才能正常工作。具体来说,我正在开发的javaweb应用程序到目前为止还没有完成。在我的代码中,名为“findusers”的函数用于实现读取操作。顺便说一下,我已经检查了很长一段时间的代码。我猜问题可能出在findusers函数中(不确定,只是一个假设)。每次我试图键入r来调用该函数时,netbeans都会返回“user does not exist”。我不知道为什么。数据库连接成功。在这里输入图片描述这张照片是我的数据库结构。

数据库管理器.java

//read operation
   public User findUsers(String email, String password) throws SQLException {
    String sqll = "SELECT * FROM XWB.USERS WHERE EMAIL = ' " + email + " ' AND PASSWORD = ' " + password + " ' ";
    // " SELECT * FROM XWB.USERS WHERE EMAIL = ' " + email + " ' AND PASSWORD = ' " + password + " ' "; 
    //select * from XWB.Users where EMAIL = 'TargetEmail' and PASSWORD = 'TargetPassword';
    ResultSet rs = st.executeQuery(sqll);

    while (rs.next()) {
        String UserEmail = rs.getString("EMAIL");
        String UserPassword = rs.getString("PASSWORD");
        if (UserEmail.equals(email) && UserPassword.equals(password)) {
            String UserName = rs.getString("NAME"); 
            String UserGender = rs.getString("GENDER");
            String UserColor = rs.getString("FAVOURITECOLOR");
            return new User(UserEmail, UserName, UserPassword, UserGender, UserColor);
        }
    }
    return null;
}

java(我使用这个类来测试dbmanager)

// findUsers()
    private void testRead() throws SQLException {
        System.out.print("User email: ");
        String email = in.nextLine();
        System.out.print("User password: ");
        String password = in.nextLine();
        User user = db.findUsers(email, password); // returns nothing
        //System.out.println(user);
        if( user != null) {
            System.out.println("User " + user.getName() + " exists in the database.");
        }else { //user == null
            System.out.println("User does not exit.");
        }
    }

这是我从netbeans得到的结果。它总是告诉我“用户不存在”在此处输入图像描述

t0ybt7op

t0ybt7op1#

我认为,如果您的电子邮件和密码的数据值是正确的,您应该删除[']字符前后的空格。您的代码:

String sqll = "SELECT * FROM XWB.USERS WHERE EMAIL = ' " + email + " ' AND PASSWORD = ' " + password + " ' ";

替换:

String sqll = "SELECT * FROM XWB.USERS WHERE EMAIL = '" + email + "' AND PASSWORD = '" + password + "' ";
ctehm74n

ctehm74n2#

在你的代码里 return null 所以当这个在你的呼叫课上收到支票的时候 if( user != null) 将返回false和 else 你生活的一部分 if-statement 将被执行。因此要克服以下更改:
您的方法如下所示:

User user; //create class object
String sqll = "SELECT * FROM XWB.USERS WHERE EMAIL = ? AND PASSWORD = ? ";
PreparedStatement ps = con.prepareStatement(
 sqll);
//setting value for "?" 
ps.setString(1, email);
ps.setString(2, password);
//execute query
ResultSet rs = ps.executeQuery();
//if found
if (rs.next()) {
 //fetch
 String UserEmail = rs.getString("EMAIL");
 String UserPassword = rs.getString("PASSWORD");
 String UserName = rs.getString("NAME");
 String UserGender = rs.getString("GENDER");
 String UserColor = rs.getString("FAVOURITECOLOR");
 //pass in constructor
 user = new User(UserEmail, UserName, UserPassword, UserGender, UserColor);
} else {
 //set the object to null (no match)
 user = null;
}
//send object back
return user;

相关问题