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