From: Travis B. <tr...@us...> - 2003-03-17 02:42:56
|
Fred Cunningham (cun...@ho...) wrote: > > Can any provide me with some examples of how to use > JDBCComboBoxModel() and SQLComboBoxModel? It's hard to give a working example problem because it requires a connection to a database. But here's an example from my own code where I create the SQLComboBoxModel. Note that the two models can be used as either ComboBoxModels or ListModels. In the example below, I'm using it as a ListModel. SQLComboBoxModel fakeUsers = new SQLComboBoxModel ("select * from fakeusers order by name", db, new int[] {1}); JList userList = new JList(fakeUsers); userList.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { try { ResultSet rs = fakeUsers.rs(); rs.absolute(userList.getSelectedIndex()+1); System.out.println("Column three contained: "+rs.getString(3)); } catch (Exception e) { System.out.println("Error: "+e); } } } This creates a SQLComboBoxModel which will run the given sql statement on the db Connection. fakeUsers can then be used as the bodel for a combo box or JList. In the code example, I'm using it in a JList. The JList will display the contents of the first column of the result set it returns. The selection listener digs back into the model's result set and gets the value of column three. SQLComboBoxModel is really just a convienence wrapper for the JDBCComboBoxModel. I hope that helps. Just ask if you have more questions. Travis |