0

I have a jTextField and a jLabel. And I want to get data from database to my label automatically after user finished typing.

subcode_txt.addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent e) {
            Object obj=e.getSource();
            if(obj==subcode_txt){
            jst=subcode_txt.getText();
            try{

            rs=st.executeQuery("Select * from facultydtls where sub_code like '"+jst+"'");
            rs.next();
            faculty_lab.setText(rs.getString("f_name"));
            sem_lab.setText(rs.getString("sem"));
            subject_lab.setText(rs.getString("sub"));
            department_lab.setText(rs.getString("dept"));

            rs.close();

        }
        catch(Exception ewt){
            JOptionPane.showMessageDialog(null,"Invalid Subject Code");
        }
            }
        }
    });
Reimeus
  • 158,255
  • 15
  • 216
  • 276

2 Answers2

2
  1. If you want to react on user input, a DocumentListener is a better approach then a KeyListener
  2. You could opt for an ActionListener, which will be triggered when the user hits ENTER
  3. You should not perform database queries on the Event Dispatch Thread. Use a worker thread instead. See Concurrency in Swing for more information
  4. The close statement should be placed in a finally block

That being said, what exactly are you asking ?

Robin
  • 36,233
  • 5
  • 47
  • 99
  • I did that with DocumentListner. But showmessagedailog "Invalid Subject Code" each time when I typed a letter – Subrat Patnaik Nov 01 '12 at 19:00
  • @SubratPatnaik The `DocumentListener` is triggered on each update of the contents of the text field. If you want to only react when the user hits enter, you need an `ActionListener` – Robin Nov 01 '12 at 19:09
  • even after use DocumentListener I have the same problem. Can you please give any example related to my problem...Thank You – Subrat Patnaik Nov 02 '12 at 17:12
2

Using a DocumentListener is the preferred approach for handling text events for JTextComponents in Swing.

Database queries demand significant resources and should be done outside the EDT to prevent the UI from 'freezing'. Have a look at using SwingWorker in this example.

Note that loading data for every DocumentEvent could potentially send a large amount of unnecessary requests to the database. There are 2 possible solutions:

  • Data caching using a framework such as Ehcache - optimally loads data.
  • While somewhat different from your requirement, you could use a load JButton to load the data when the full String has been provided by the user.

Update:

In the short term:

Add SQL wildcards characters to your query to match partial strings:

st.executeQuery("Select * from facultydtls where sub_code like '%" + jst + "%'");

In the long term:

Use PreparedStatement. It has the following benefits:

  • Protects against SQL injection attacks, especially given that the user can enter a string that is part of the SQL query.
  • PreparedStatement placeholder characters will take of any required string quotes.
Community
  • 1
  • 1
Reimeus
  • 158,255
  • 15
  • 216
  • 276