当我点击JList框中的特定值时,我试图将值从数据库导入到jTextFilelds,我想要的是从jlist中获取所选值并使用它来使用select语句从数据库中获取值。 我实际工作的是我的任务的地址簿 Plz帮助我!!
提前致谢
private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt)
// String lfn = evt.toString();
DefaultListModel listModel = new DefaultListModel();
jList1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
String sel = listModel.get(jList1.getSelectedIndex()).toString();
try {
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@//localhost:1521/XE ", "system", "sruthi");
Statement stmt = con.createStatement();
ResultSet rs = null;
//String sql = "select FNAME,LNAME,EMAIL,CITY,STATE,PNUM from PERSON where FNAME=lfn";
jList1.setSelectedIndex(0);
rs = stmt.executeQuery("select FNAME,LNAME,EMAIL,CITY,STATE,PNUM from PERSON where FNAME='%"+sel+ "%'");
String fn = rs.getString("FNAME");
String ln = rs.getString("LNAME");
String em = rs.getString("EMAIL");
String ct = rs.getString("CITY");
String st = rs.getString("STATE");
int no =Integer.parseInt(rs.getString("PNUM"));
jTextField1.setText(fn);
jTextField2.setText(em);
jTextField3.setText(String.valueOf(no));
jTextField4.setText(ct);
jTextField5.setText(st);
jTextField7.setText(ln);
} catch (SQLException ex) {
Logger.getLogger(addbook.class.getName()).log(Level.SEVERE, null, ex);
}
错误:::
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
at java.util.Vector.elementAt(Vector.java:427)
at javax.swing.DefaultListModel.get(DefaultListModel.java:428)
at project2.addbook.jList1ValueChanged(addbook.java:356)
at project2.addbook.access$000(addbook.java:18)
at project2.addbook$1.valueChanged(addbook.java:108)
at javax.swing.JList.fireSelectionValueChanged(JList.java:1765)
at javax.swing.JList$ListSelectionHandler.valueChanged(JList.java:1779)
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.JList.setSelectionInterval(JList.java:2035)
at javax.swing.plaf.basic.BasicListUI$Handler.adjustSelection(BasicListUI.java:2727)
at javax.swing.plaf.basic.BasicListUI$Handler.mousePressed(BasicListUI.java:2683)
at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:263)
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)
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
at java.util.Vector.elementAt(Vector.java:427)
at javax.swing.DefaultListModel.get(DefaultListModel.java:428)
at project2.addbook.jList1ValueChanged(addbook.java:356)
at project2.addbook.access$000(addbook.java:18)
at project2.addbook$1.valueChanged(addbook.java:108)
at javax.swing.JList.fireSelectionValueChanged(JList.java:1765)
at javax.swing.JList$ListSelectionHandler.valueChanged(JList.java:1779)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:167)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:137)
at javax.swing.DefaultListSelectionModel.setValueIsAdjusting(DefaultListSelectionModel.java:668)
at javax.swing.JList.setValueIsAdjusting(JList.java:2110)
at javax.swing.plaf.basic.BasicListUI$Handler.mouseReleased(BasicListUI.java:2784)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:273)
at java.awt.Component.processMouseEvent(Component.java:6267)
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:4238)
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)
答案 0 :(得分:1)
问题是你正在创建一个新的DefaultListModel,然后尝试从中获取一些东西(因为它刚刚创建,所以不会有任何内容)。相反,您应该致电jlist1.getSelectedValue()
。