Query ORDER BY dynamics from the selection of a combobox

Asked

Viewed 156 times

1

I’m developing an application on java managing a database. This application primarily performs a SELECT the database, then submitting the ResultSet for an editable table.

I tried to add a comboBox for the user to select the order in which he wants to list the data, but the segment of code I wrote for this purpose is not working.

My question is whether there is any way around this problem and make some kind of concatenation of strings and variables to which the ORDER BY is dynamic and dependent on what the user chooses .

else if (Item == 1)
{ /* Computer List) */
    comboBoxOrderBy.setModel(new DefaultComboBoxModel(new String[] { "computer_id", "cpu_model",
        "cpu_clock", "ram", "hdd", "os", "archit", "computer_name", "computer_type",
    "computer_brand", "computer_model", "serial_number", "status" }));
    comboBoxOrderBy.addActionListener(
    new ActionListener()
    { /*
        * ACÇAO DA CAIXA
        * ORDER BY
        */
        public void actionPerformed(ActionEvent arg0)
        {
            String teste = (String)comboBoxOrderBy.getSelectedItem();
            JOptionPane.showMessageDialog(frmInventoryDatabaseManager, teste);
            try
            {
                /*
                * BEGINING OF DATABASE REQUEST
                */
                Connection lig = DriverManager.getConnection(
                "jdbc:mysql://localhost/inventorydb", "root", "");
                PreparedStatement inst = lig.prepareStatement(
                "SELECT computer_id, cpu_model, cpu_clock, ram, hdd, "
                + "os, archit, computer_name, computer_type, computer_brand, "
                + "computer_model, serial_number, status FROM computerlist ORDER BY ? ASC");
                inst.setString(1, teste);
                ResultSet rs = inst.executeQuery();
                table1.setModel(DbUtils.resultSetToTableModel(rs));
                lig.close();
                /*
                * END OF DATABASE REQUEST
                * SERVICES
                */
            }
            catch (SQLException e1)
            {
                JOptionPane.showMessageDialog(frmInventoryDatabaseManager,
                "Impossivel ligar á base de dados: "
                + e1.getLocalizedMessage());
            }
        }
    });
}
  • Do you want, when changing the option in the combobox, the table to be changed according to the selected option? And what do you mean by not working?

  • Once the selection of the table is done , the data within the table is not ordered according to the selection of the combobox (nothing happens), in this case , each action supposedly refers to a different ORDER BY parameter, I think the problem is the statement is not well done, have already tested do a normal ORDER BY query specifying the desired field and works perfectly.

  • A question, the table is already populated before the selection of the combobox, and that there only changes the order of its elements?

  • Exact, the table was previously populated through a selection of another combobox in which the user chooses which table to present ; I then used an imported method that allows to popular the table according to the nature and layout of the tables of the database table1.setModel(Dbutils.resultSetToTableModel(rs));

  • Why then consult the bank again just to order, if the data are already there? If you do suaTable.setAutoCreateRowSorter(true), columns will already be sorted by clicking on the header. Remember that to work, your Tablemodel needs to have the column classes defined in getColumnClass, if you are not using DefaultTableModel. Test there.

  • Thank you very much for the help , this simple function made me get to the intended

  • Good :) You want me to transform as an answer?

  • Yes can transform

Show 3 more comments

1 answer

1

Since the goal is to sort the table according to the column, simply activate the rowSorter, through the command:

suaTable.setAutoCreateRowSorter(true);

With this, the columns become "organizable". Remembering that if you use a TableModel own, it is necessary to identify the data types of each column through the getColumnClass to work properly(if any column is of some kind of class created by you).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.