Java - 在 JList 值更改时将数据从数据库导入到文本字段
当我单击 JList 框中的特定值时,我试图将数据库中的值导入到 jTextFilelds 中,我想要的只是从 jlist 中获取选定的值,并使用它通过 select 语句从数据库中获取值。 我实际上正在做的是我的作业的地址簿 请帮帮我!
提前谢谢
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)
I am trying to import values from database to jTextFilelds when i click on a particular value in JList box , all i want is get the selected value from jlist and use that to obtain values from database using select statement..
What i m actually working on is address book for my assignment
Plz help me out!!
Thanks, in advance
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);
}
ERRORS:::
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您正在创建一个新的 DefaultListModel,然后尝试从中获取某些内容(因为它刚刚创建,所以其中不会有任何内容)。相反,您应该只调用
jlist1.getSelectedValue()
。The problem is that you are creating a new DefaultListModel, and then trying to get something out of it (nothing will be in it since it was just created). Instead, you should just call
jlist1.getSelectedValue()
.