Home How to assign values in the JList model from Oracle database
Reply: 0

How to assign values in the JList model from Oracle database

Sarah
1#
Sarah Published in 2018-02-14 04:33:48Z

I'm trying to fetch the data from oracle database (local machine) to populate Jlist model (lstCustomers) to be displayed on the GUI whenever a customer type is selected using JComboBox. i.e. "Silver", "Gold" and "Platinum". The connection works fine and run a simple test using System.out.print and able to get results.

However, whenever I call the populateJList() on the event handler nothing is fetched or displayed on the GUI. Note: (Silver customer type selected on this scenario). If other customer type was selected it showed the message "Test".

If customer type = 'Silver' it should assign values to the Jlist model from this query "Select FIRST_NAME, LAST_NAME from employees;

    private static void populateJList() throws SQLException 

{


   try 
   {
    //DefaultListModel customerList = new DefaultListModel(); //create a new list model

    Statement statement = connection.createStatement();
    ResultSet resultSet = statement.executeQuery("Select * from employees where job_id='ST_CLERK'"); //run your query

    while (resultSet.next()) //go through each row that your query returns
    {
        //System.out.println(resultSet.getString(2));
        String FNameList = resultSet.getString("FIRST_NAME"); //get the element in column "item_code"
        customerList.addElement(FNameList); //add each item to the model

        //if (resultSet != null) 
         //   System.out.println(resultSet.getString(2)););

    }
    lstCustomers.setModel(customerList);


   }finally 
   {
       try { resultSet.close();
             statement.close();
             connection.close();
       }catch (SQLException e) {
           e.printStackTrace();   
   }
   }

}  

/*********************************************************************************************
 * This code contains for creating events 
 *********************************************************************************************/
private void createEvent() 
{

    cbCustomertype.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) 

        {
            if (cbCustomertype.getSelectedItem().equals("Silver"))
            {
                try {
                    WorkGUI.populateJList();
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            } else JOptionPane.showMessageDialog(null, "Test!");    


        }
    });

    btnOrder.addActionListener(new ActionListener() 
    {
        public void actionPerformed(ActionEvent e) 
        {

            if (txtFirstName.getText().equals(""))
                JOptionPane.showMessageDialog(null, "Please Enter First Name!");
            else if (txtLastName.getText().equals(""))
                    JOptionPane.showMessageDialog(null, "Please Enter Last Name!"); 
            else if (!lstCustomers.getSelectedValue().equals((txtFirstName.getText() + " " + txtLastName.getText())))
                JOptionPane.showMessageDialog(null, "Invalid Selected Customer Name!");
            else        
                JOptionPane.showMessageDialog(null, "Thank you for your Order " + txtFirstName.getText() + "!");


        }
    });
}

}

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.383265 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO