Java-Fetch Database Data in JTextField and JCombobox Swing/AWT/SWT/Windows Builder

Last modified date

We need to have regular practice to in fetching data in JTextField or Combobox, the main thing is handling nulls values in fetching the Database data.

Before we begin with the fetching the data, we must be accurate in writing exact query. For example: are we trying to retrieve more than one row or single row. Depending on that we need to decide to show the data in JTextfield of JCombobox.

How to fetch data in JCombobox

Example Table (Emp) with some Values

Emp_id Emp_name Emp_salary
1111 Suryam 25000
2222 Swathi 20000
3333 Medha Saili 25000
4444 Naresh 20000

 

Considering the above table, if we want to show only the emp_name from table emp, the expected query will something be like.

Select emp_name from emp

Emp_name
Suryam
Swathi
Medha Saili
Naresh



Now using the above query I’ll write a method to display the list of names in Jcombobox.

Assuming an object Jcombobox combobox to display list of emp_name

public void showEmpNames()
{
  try {
    //combobox.removeAllItems();
    connection=ConnectionManager.getConnection();
    String query="Select emp_name from emp";
    Statement st=connection.createStatement();
    ResultSet set=st.executeQuery(query);
    while(set.next())
    {
      combobox.addItem(set.getString(1));
      // or you can also write 
      //combobox.addItem(set.getString("emp_name"));
    }
    
  } catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();
  } finally{
    try {
      connection.close();
    } catch (Exception e2) {
      // TODO: handle exception
    }
  }

}

We can call the method showEmpNames() directly in the constructor, so that the list of name are directly shown in combobox at the time of the application startup.

How to avoid duplicates names in JCombobox, this is also a common problem beginners will regularly face. To avoid duplicates in JCombobox list just call combobox.removeAllItems(); before the while loop statement.

How to fetch data in JTextField or JLabel

Example Table (Emp) with some Values

Using the above same table we will get single emp_name and emp_salary.

Query : "select emp_name,emp_salary from emp where emp_id='3333';

The above query returns

Medha Saili 25000

 

Assuming two  JTextField’s  field1 and field2, in field1 we will show emp_name and in field2 we will show emp_salary

try {
      
       connection=ConnectionManager.getConnection();
       String query="select emp_name,emp_salary from emp where emp_id='3333';"		 
       Statement st=connection.createStatement();
       ResultSet set=st.executeQuery(query);
       if(set.next())
      {
        field1=set.getString(1);
        field2=set.getString(2);
      }
          
      	} catch (Exception e) {
      // TODO: handle exception
      e.printStackTrace();
      } finally{
        try {
          connection.close();
        } catch (Exception e) {
          // TODO: handle exception
        }
      
      }

 

 

When to user while loop and if contition, in the above example for combobox we used while loop because, we are expecting more than one row to be returned and for field1 and field2 we are specifically mentioning to retrieve single emp_name and his salary with his emp_id. In this condition we surely know that only one row is returned so we use if condition.

intactabode