如何通过模型从 DAO 获取数据并将它们附加到视图中

How can I get data from DAO through the model and append them in the view

我是 Java 的新手,我正在尝试实现符合 MVC - DAO 的登录系统和用户配置文件。我想通过方法 addUserDatatoView() 启用控制器以从 DAO 检索用户凭据,以便将它们作为字符串添加到新的 JPanel(视图)中。无论如何,我不确定我的处理方式是否正确。首先,我一直收到 NullPointerException 事件,尽管 DAO 级别正在从数据库中正确获取数据:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "model.User.getUserName()" because "this.user" is null
    at controller.LoginController.addUserDatatoView(LoginController.java:75)
    at controller.LoginController.showHome(LoginController.java:65)
    at controller.LoginController$LoginListener.actionPerformed(LoginController.java:44)

我如何才能真正从 DAO 通过模型(用户 class)检索数据?将数据作为字符串从控制器部署到视图的最佳方式是什么?

我很困惑不同 classes 之间的通信以及正确的过程是什么,以免违反 MVC-DAO。

我不是要你解决问题,而是要得到提示,以便找到正确的方向。

DAO 实现:

package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.swing.JOptionPane;

import controller.HomeController;
import ds.OracleDsSingleton;
import model.Event;
import model.User;
import view.HomeView;
import view.LoginView;
import view.ProfileView;

public class DaoImpl implements DAO {

    LoginView view;
    ProfileView profView;
    ResultSet rs;

    public DaoImpl(LoginView view, ProfileView profView) {
        this.view = view;
        this.profView = profView;
    }
    @Override
    public ArrayList<User> getUserLogIn (String userName, String userPass) throws SQLException {
        OracleDsSingleton ora = OracleDsSingleton.getInstance();
        boolean controlRecords = false;
        try {
            //ArrayList type User
            ArrayList<User> user = new ArrayList<User>();
            Connection con = ora.getConnection();
            Statement stmt = con.createStatement();
            String addQuery = "SELECT * FROM UserList";
            ResultSet rs = stmt.executeQuery(addQuery);
            while (rs.next()) {
                userName = rs.getString("userName");
                userPass = rs.getString("userPass");
                if (userName.equals(view.getUserNameTextField().getText().toString())
                        && (userPass.equals(view.getUserPassTextField().getText().toString()))) {
                    {
                        controlRecords = true;
                        User u = new User(userName, userPass);
                        user.add(u);
                        for(User us : user) {
                            System.out.println("Directly from DAOImp: " + us);
                        }
                        return user;
                    }
                }
                else {
                    continue;
                }
            }
            if (!controlRecords) {
                JOptionPane.showMessageDialog(null, "Not successfully logged in!");
            };
            if (con != null)
                con.close();
            if (stmt != null)
                stmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }       
        return null;
    }
}

Class 用户:

public class User {
    String userName;
    String userPass;

    public User(String userName, String userPass) {
        this.userName = userName;
        this.userPass = userPass;
    }
    
    public User() throws SQLException { 
    }
    
    public String getUserName() {
        return userName;
    }
    
    public String getUserPass() {
        return userPass;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
    
    
    public String toString() {
        return userName + userPass;
    }
}

控制器

package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JComponent;

import controller.LoginController.LoginBACKListener;
import dao.DAO;
import dao.DaoImpl;
import model.User;
import view.HomeView;
import view.LoginView;
import view.ProfileView;
import view.StartView;

public class LoginController{
    
    private User user;
    private LoginView view;
    private ProfileView profView;
    
    public LoginController(User user, LoginView view) {
        this.user = user;
        this.view = view;
        addListener();
    }
    
    private void addListener() {
        this.view.setLoginListener(new LoginListener());
    }
    
    class LoginListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {    
            String name = view.getUserNameTextField().getText();
            String pass = view.getUserPassTextField().getText();        
            DAO myDAO = new DaoImpl(view, profView);    
            try {
                //when method from DAOImpl get filled, proceed to Home
                if(myDAO.getUserLogIn(name, pass) != null) {
                    showHome();
                }
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
    }

    public void showHome() {
        HomeView home = new HomeView();
        home.setVisible(true);
        HomeController h = new HomeController(home);
        try {
            addUserDatatoView();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    

    public ArrayList<User> addUserDatatoView() throws SQLException {
        DAO myDAO = new DaoImpl(view, profView);
        ArrayList<User> userCredentials = myDAO.getUserLogIn(user.getUserName(), user.getUserPass());
        for(User us : userCredentials) {
            System.out.println("Directly from Controller: " + us);
        }
        return userCredentials;
    }
}

从问题中的部分代码很难判断,但据我所知,只有登录成功后才能构造一个User对象。所以构造函数应该更改为:

public LoginController(LoginView view) {
    this.view = view;
    addListener();
}

class LoginListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {    
        String name = view.getUserNameTextField().getText();
        String pass = view.getUserPassTextField().getText();        
        DAO myDAO = new DaoImpl(view, profView);    
        try {
            user = myDAO.getUserLogIn(name, pass);//change getUserLogIn to return a single User, or null 
            //when method from DAOImpl get filled, proceed to Home
            if(user != null) {                
                showHome();
            }
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
    }
}