gpt4 book ai didi

java - 无法在 MVC 框架中执行查询

转载 作者:行者123 更新时间:2023-11-30 09:27:41 24 4
gpt4 key购买 nike

来自类模型:

    public TableModel getTableData() {
TableModel model=null;
try {
String sql = "SELECT eventID as 'Event ID', date as 'Date',eventName as 'Name', time as 'Start Time' FROM Event";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
model = (DbUtils.resultSetToTableModel(rs));
}
catch(SQLException e){
e.printStackTrace();
}
finally {
try {rs.close(); pst.close(); }
catch(SQLException e){} }
return model;
}

public void getEvent(String tableClick) {
Events e = new Events();
try {
pst = conn.prepareStatement("SELECT * FROM Event WHERE eventID='"+tableClick+"' ");
rs = pst.executeQuery();
while(rs.next()){
e.setEventName(rs.getString(2));
e.setEventDate(rs.getDate(3));
e.setEventTime(rs.getString(4));
e.setEventVenue(rs.getString(5));
e.setEventDetail(rs.getString(6));
e.setEventOpportunity(rs.getString(7));
e.setEventMoreDetails(rs.getString(8));
e.setEndTime(rs.getString(9));
}
}
catch(SQLException ex){
ex.printStackTrace();
} finally {
try {
rs.close();pst.close();conn.close();}
catch (Exception ex){}
}
} //end getEvent

来自类 Controller :

@Override
public void valueChanged(ListSelectionEvent event) {
int rowSelected = view.tableEvent.getSelectedRow();
String tableClick = view.tableEvent.getModel().getValueAt(view.tableEvent.convertRowIndexToModel(rowSelected), 0).toString();
model.getEvent(tableClick); //tell model to change its state based on user input on views - model is instance of class model
}

当我运行我的程序时,堆栈跟踪在模型类的 pst = conn.prepareStatement("SELECT * FROM Event WHERE eventID='"+tableClick+"' "); 行显示错误,和 Controller 类的 model.getEvent(tableClick); 行。在 NetbeanIDE 中以 Debug模式运行带有断点的程序后,我只能看到 e.setEventName(...) 被数据库中的值填充,而 的所有其他值事件 bean 类为空。 e.setEventDate(...) 将日期设置为 #2222。如果您需要更多信息,我将编辑我的帖子。但这真的很烦我。

是不是 ListSelectionListener 引起了问题,也许 mouseListener 更合适?

进一步补充,如果我将 getEvent() 更改为

public void getEvent(String tableClick) {
Events e = new Events();
try {
pst = conn.prepareStatement("SELECT * FROM Event WHERE eventID='"+tableClick+"' ");
rs = pst.executeQuery();
while(rs.next()){
JOptionPane.showMessageDialog(null, "Yess");

}
}

它只在第一次单击一行时显示 Yes。不是第二次点击另一行。

最佳答案

使用 MouseListener 检测点击的表格行。

@Override
public void mouseClicked(java.awt.event.MouseEvent event) {
int rowSelected = view.tableEvent.rowAtPoint(event.getPoint());
String tableClick = view.tableEvent.getModel().getValueAt(view.tableEvent.convertRowIndexToModel(rowSelected), 0).toString();
model.getEvent(tableClick); //tell model to change its state based on user input on views*/

rs.close();pst.close(); 也是方法 getEvent() 的 try block 的结尾。

关于java - 无法在 MVC 框架中执行查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14425226/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com