Reputation: 27
I'm trying to bind a table from my db in a Jtable
in my desktop app.
I had follow the step in this guide:
https://netbeans.org/kb/docs/java/gui-binding.html
and everything is fine, but I can't change the query to show the data in a different order.
If I try to change the query the application does not work.
Netbeans had create the query so:
SELECT t FROM TbAzioni t
my table name is tb_azioni
and I would like to have a query like this:
select * from tb_azioni order by azcodaz
but if I change the query nothing works. Thank you
Upvotes: 1
Views: 314
Reputation: 11221
Go to the JTable properties in NetBeans and enable: autoCreateRowSorter.
Upvotes: 1
Reputation: 20755
public Vector get()throws Exception
{
Vector<Vector<String>> vector = new Vector<Vector<String>>();
Connection conn = dbConnection();
PreparedStatement pre = conn.prepareStatement("select * from tb_azioni order by azcodaz");
ResultSet rs = pre.executeQuery();
while(rs.next())
{
Vector<String> s = new Vector<String>();
s.add(rs.getString(4));
s.add(rs.getString(5));
s.add(rs.getString(6));
s.add(rs.getString(1));
s.add(rs.getString(7));
vector.add(s);
}
/*Close the connection after use (MUST)*/
if(conn!=null)
conn.close();
return vector;
}
private Vector<Vector<String>> data; //used for data from database
private Vector<String> header; //used to store data header
data = get();
JTable table5=new JTable(data,header);
for(int i2=0;i2<table5.getRowCount();i2++){
Object[] d={data.get(i2).get(0),data.get(i2).get(1),data.get(i2).get(2),data.get(i2).get(3),data.get(i2).get(4)};
model.addRow(d);
}
DefaultTableModel model=new DefaultTableModel(data,header);
JTable table=new JTable(model);
Upvotes: 0