我使用Java和Netbeans 7.2 这是一个简单的JFrame表单。它连接到SQLite数据库,从一个表中检索所有记录并将其放入JTable小部件(Table1)。它工作正常。
但是,我添加了4个文本字段,这些字段应填充在JTable中单击的行的值。
当click处理程序启动时,它只在第一个文本字段中插入第一个列值,然后弹出错误消息(java.lang.ClassCastException)。
我无法弄清楚哪一行是问题。调试器打印有关“无法编译代码”的错误(我不知道如何使用调试器)。
private void Table1MouseClicked(java.awt.event.MouseEvent evt) {
try{
int row = Table1.getSelectedRow();
String rowId = (Table1.getModel().getValueAt(row, 0).toString());
String sql = "select * from ljudi where ID='"+rowId+"';";
pst = conn.prepareStatement(sql);
rst = pst.executeQuery();
if(rst.next()){
String name = rst.getString("ime");
String surname = rst.getString("prezime");
String address = rst.getString("adresa");
String telephone = rst.getString("telefon");
txt_ime.setText(name);
txt_prezime.setText(surname);
txt_adresa.setText(address);
txt_telefon.setText(telephone);
}
}
catch(Exception e){
JOptionPane.showMessageDialog(null,e);
}
}
我甚至评论了方法中的所有代码,一旦我点击表格行,输出窗口就会显示错误消息。
run:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException
at java.lang.Class.cast(Class.java:2990)
at org.jdesktop.beansbinding.Binding.convertForward(Binding.java:1312)
at org.jdesktop.beansbinding.Binding.getSourceValueForTarget(Binding.java:844)
at org.jdesktop.beansbinding.Binding.refreshUnmanaged(Binding.java:1222)
at org.jdesktop.beansbinding.Binding.refresh(Binding.java:1207)
at org.jdesktop.beansbinding.AutoBinding.tryRefreshThenSave(AutoBinding.java:162)
at org.jdesktop.beansbinding.AutoBinding.sourceChangedImpl(AutoBinding.java:227)
at org.jdesktop.beansbinding.Binding.sourceChanged(Binding.java:1411)
at org.jdesktop.beansbinding.Binding.access$1200(Binding.java:38)
at org.jdesktop.beansbinding.Binding$PSL.propertyStateChanged(Binding.java:1618)
at org.jdesktop.beansbinding.PropertyHelper.firePropertyStateChange(PropertyHelper.java:212)
at org.jdesktop.beansbinding.ELProperty.notifyListeners(ELProperty.java:688)
at org.jdesktop.beansbinding.ELProperty.access$800(ELProperty.java:155)
at org.jdesktop.beansbinding.ELProperty$SourceEntry.processSourceChanged(ELProperty.java:312)
at org.jdesktop.beansbinding.ELProperty$SourceEntry.sourceChanged(ELProperty.java:326)
at org.jdesktop.beansbinding.ELProperty$SourceEntry.propertyChange(ELProperty.java:333)
at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:339)
at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:276)
at org.jdesktop.swingbinding.adapters.BeanAdapterBase.firePropertyChange(BeanAdapterBase.java:112)
at org.jdesktop.swingbinding.adapters.JTableAdapterProvider$Adapter$Handler.tableSelectionChanged(JTableAdapterProvider.java:81)
at org.jdesktop.swingbinding.adapters.JTableAdapterProvider$Adapter$Handler.valueChanged(JTableAdapterProvider.java:91)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:167)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:147)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:194)
at javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:388)
at javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:398)
at javax.swing.DefaultListSelectionModel.setSelectionInterval(DefaultListSelectionModel.java:442)
at javax.swing.JTable.changeSelectionModel(JTable.java:2352)
at javax.swing.JTable.changeSelection(JTable.java:2421)
at javax.swing.plaf.basic.BasicTableUI$Handler.adjustSelection(BasicTableUI.java:1085)
at javax.swing.plaf.basic.BasicTableUI$Handler.mousePressed(BasicTableUI.java:1008)
at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:263)
at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:262)
at java.awt.Component.processMouseEvent(Component.java:6264)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6032)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4235)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
BUILD SUCCESSFUL (total time: 8 seconds)
非常感谢任何帮助。
谢谢
P.S。正如你所问,完整的堆栈跟踪就在这里,我想就是这样。
P.P.S。 我以一种不同的方式解决了它:
try
{
int pozicija = jTable1.getSelectedRow();
String rowId = jTable1.getModel().getValueAt(pozicija, 0).toString();
String ime = jTable1.getModel().getValueAt(pozicija,1).toString();
String prezime = jTable1.getModel().getValueAt(pozicija,2).toString();
String adresa = jTable1.getModel().getValueAt(pozicija,3).toString();
String telefon = jTable1.getModel().getValueAt(pozicija,4).toString();
jTextField1.setText(ime);
jTextField2.setText(prezime);
jTextField3.setText(adresa);
jTextField4.setText(telefon);
jLabel2.setText(rowId);
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, e);
}
我没有在数据库上运行查询,而是使用了jTable模型中的相同字段值。工作正常,其目的是填充文本字段以编辑记录。
我认为没有必要再次查询数据库?
顺便说一下。我在“ProgrammingKnowledge”的youtube教程中看到了第一个例子。它解释了许多基本内容。
感谢你的想法。