无法在MVC框架中执行查询

时间:2013-01-20 13:59:57

标签: java swing model-view-controller jdbc jtable

来自班级模特:

    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

来自班级管理员:

@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
    } 

当我运行程序时,我有堆栈跟踪在Model类的行pst = conn.prepareStatement("SELECT * FROM Event WHERE eventID='"+tableClick+"' ");和Controller类的行model.getEvent(tableClick);显示错误。在NetbeanIDE中以调试模式运行带有断点的程序后,我只能看到e.setEventName(...)填充了数据库中的值,而Event bean类的所有其他值都为null。 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。不是第二次点击另一行。

2 个答案:

答案 0 :(得分:1)

使用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块的结尾。

答案 1 :(得分:1)

使用显示ListSelectionListenerhere来检测选择中的更改。使用JButtonCellEditor(如图所示here)来调用特定行的操作。