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?
– user28595
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.
– André Rodrigues
A question, the table is already populated before the selection of the combobox, and that there only changes the order of its elements?
– user28595
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));
– André Rodrigues
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 ingetColumnClass
, if you are not usingDefaultTableModel
. Test there.– user28595
Thank you very much for the help , this simple function made me get to the intended
– André Rodrigues
Good :) You want me to transform as an answer?
– user28595
Yes can transform
– André Rodrigues