有关JSP&的问题SQL。 Java Web应用程序 [英] Questions about JSP & SQL. Java web application

查看:84
本文介绍了有关JSP&的问题SQL。 Java Web应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近正在开发一个Java Web应用程序,该应用程序必须专注于CRUD(您知道)。但我对阅读&更新和删除操作(三个操作)。只有创建操作才能正常工作。详细地说,我正在使用的Java Web应用程序尚未完成。在我的代码内部,名为 findUsers的函数用于实现读取操作。顺便说一句,我已经检查了很长时间的代码。我猜问题可能出在findUsers函数中(不确定,只是一个假设)。每次我尝试键入R来调用该函数时,Netbeans都会返回用户不存在。我不知道为什么此外,数据库已成功连接。 在此处输入图片描述这张照片是我的数据库结构。

I am recently working on making a Java web application, which must focus on CRUD(You know). But I am stuck with the read & update & delete operations(Three operations). Only Create Operation works fine. In detailed, the Java web application I am working on is not completed so far. Inside my code, the function called "findUsers" is used to implement read operation. By the way, I already checked code for a long period of time. I guess the problem may be in findUsers function(not sure, just an assumption). Every time, I try to type R to invoke that function, Netbeans returns "User does not exist". I don't know why. Moreover, the database is connected successfully. enter image description here This photo is my database structure.

//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;
}



TestDB.java(我使用此类来测试DBManager)



TestDB.java(I use this class to test the 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获得的结果。它总是告诉我用户不存在。
在此处输入图像描述

This is the result I got from Netbeans. It always tells me "User does not exist. " enter image description here

推荐答案

在您的代码中,您有返回null 的功能,检查您的调用类 if(user!= null)将返回false,并且<$ c $中的 else 部分返回$ c> if语句将被执行。因此要克服以下更改:

In your code you have return null so when this get check at your calling class if( user != null) will return false and the else part of your if-statement will get executed.So to overcome do below changes :

您的方法将如下所示:

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;

这篇关于有关JSP&amp;的问题SQL。 Java Web应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆