Java - импорт данных из БД в текстовые файлы при изменении значения JList - PullRequest
0 голосов
/ 19 февраля 2012

Я пытаюсь импортировать значения из базы данных в jTextFilelds, когда я нажимаю на конкретное значение в окне JList, все, что мне нужно, это получить выбранное значение из 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)

1 Ответ

1 голос
/ 19 февраля 2012

Проблема в том, что вы создаете новую DefaultListModel, а затем пытаетесь извлечь что-то из нее (в ней ничего не будет, так как она была только что создана). Вместо этого вы должны просто позвонить jlist1.getSelectedValue().

...