我还是一个 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();
}
}
我的 Controller :
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);
}
}
有人能告诉我我在这里做错了什么吗?就像我说的,搜索本身有效,所以我假设我的错误与我的 B 的 Actionlistener 有关
编辑:我的 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;
}
}
最佳答案
这部分有问题:
ResultsDaoImpl result1 = new ResultDaoImpl();
请提供代码。调用堆栈就可以了。
关于Java MVC 按钮不调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33563624/