Java MVC按钮没有调用函数

时间:2015-11-06 09:36:48

标签: java model-view-controller jbutton

我还是Java的初学者,这是我第一次尝试使用MVC模型。到目前为止一切正常,我已经成功完成了两个小例子。

但是现在我在我当前的项目中遇到了一个问题,即按钮单击应该在数据库中开始搜索。我已经测试了所有并调用方法来搜索我的主类中的数据库,但是尝试让按钮调用所述函数并不会返回任何结果或错误。

我的观点:

import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;

public class View extends JFrame{

private static final long serialVersionUID = 1L;

public static final String SEARCH = "SEARCH";

private JButton searchbutton = new JButton();

public View() {
    this.setTitle("Betriebsnummersuche TBBBST");

    this.setDefaultCloseOperation(EXIT_ON_CLOSE);

    this.setLayout(new FlowLayout());

    searchbutton.setText("Search");
    searchbutton.setActionCommand(View.SEARCH);
    this.add(searchbutton);

    this.setSize(600, 400);
    setResizable(false);
    //this.pack();
}

public JButton getButton() {
    return searchbutton;
}

}

我的模特:

import java.io.IOException;
import java.sql.SQLException;
import java.util.Observable;
import default.dbconnect.dao.impl.ResultsDaoImpl;

public class Model extends Observable{

public void search() throws SQLException, IOException {
    ResultsDaoImpl result1 = new ResultDaoImpl();
    result1.getResults();
}
}

我的控制器:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Observable;
import java.util.Observer;

public class Controller implements Observer, ActionListener{

private Model model;
@SuppressWarnings("unused")
private View view;

public Controller(Model model, View view) {
    this.model = model;
    this.view = view;

    model.addObserver(this);

    view.getButton().addActionListener(this);

    view.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
    switch (e.getActionCommand()) {
    case View.SEARCH:
        try {
            model.search();
        } catch (SQLException e1) {
            System.out.println("A SQL-error occured: ");
            e1.printStackTrace();
        } catch (IOException e1) {
            System.out.println("An error occured: ");
            e1.printStackTrace();
        }
        break;

    default:
        System.out.println("Search error: " + e.getActionCommand());
        break;
    }

}

@Override
public void update(Observable o, Object arg) {
    // TODO Auto-generated method stub

}

}

最后我的主要:

import java.io.IOException;
import java.sql.SQLException;
import default.mvc.Controller;
import default.mvc.Model;
import default.mvc.View;

public class Main {

public static void main(String[] args) throws SQLException, IOException {

    Model model = new Model();
    View view = new View();
    Controller controller = new Controller(model, view);
}

}

有谁能告诉我这里我做错了什么?就像我说的,搜索本身有效,所以我假设我的错误与我的Actionlistener为我的B

编辑:我的ResultsDaoImpl类的代码:

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import de.drv.dsrv.betriebsnumemrsuchetbbbst.dao.ResultsDao;
import de.drv.dsrv.betriebsnummersuchetbbbst.business.ResultsBean;

public class ResultsDaoImpl extends AbstractDao implements ResultsDao {
//Only show first 10 results, change later!!!
private static final String AllResults = "SELECT BBSTBBNR, BBSTPLZ, BBSTNABEG FROM BP.TBBBST FETCH FIRST 10 ROWS ONLY";

public Collection<ResultsBean> getResults() throws SQLException,
        IOException {
    final Collection<ResultsBean> endresult = new ArrayList<ResultsBean>();

    ResultSet resultset = null;
    try {
        resultset = getResultset(AllResults);

        // while loop for showing all data
        while (resultset.next()) {
            ResultsBean results = new ResultsBean();
            int resultid = resultset.getInt(1);
            String resultplz = resultset.getString(2);
            String resultname = resultset.getString(3);
            ergebnis.add(results);
            System.out.println("Results: " + resultid + " " + resultplz + " " + resultname);
        }

    } catch (SQLException e) {

        e.printStackTrace();
        System.out.println("An error occurred processing SQL statement (getResults)");
    } catch (NullPointerException npe) {
        System.out.println("NullPointerException");
    } finally {

        closeConnection(resultset);

    }

    return endresult;
}

}

1 个答案:

答案 0 :(得分:1)

这部分出了点问题:

ResultsDaoImpl result1 = new ResultDaoImpl();

请提供相关代码。调用堆栈是可以的。