Reputation: 1
I would to get data from selected value on the ComboBox by using MySQL. However, in the JList, it shows all the data form songpick
column without get the value from the ComboBox. Is there anything wrong with my sql command?
FillCombo()
method:
PreparedStatement st = con.prepareStatement("select * from customer");
ResultSet rs = st.executeQuery();
while(rs.next()){
String CustomerName= rs.getString("customername");
jComboBox1.addItem(CustomerName);
}
FillList()
method:
DefaultListModel dlm = new DefaultListModel();
PreparedStatement st = con.prepareStatement("select songpick from customer");
ResultSet rs = st.executeQuery();
if(rs.next()){
String songpick=rs.getString("songpick");
dlm.addElement(songpick);
jList1.setModel(dlm);
}
Upvotes: 0
Views: 132
Reputation: 6128
You need to have a WHERE
clause and set the value that you want to get from
E.g. SELECT songpick FROM customer WHERE <columnName> = ?
and set the value of the that you need before the executeQuery
statement with st.setString(1, "Foo");
Upvotes: 1
Reputation: 347184
It would be helpful if your filtered the result set from the database by the customer's name, for example...
DefaultListModel dlm = new DefaultListModel();
try (PreparedStatement st = con.prepareStatement("select songpick from customer where customername=?")) {
String customerName = (String)jComboBox1.getSelectedItem();
st.setString(1, customerName);
try (ResultSet rs = st.executeQuery()) {
if (rs.next()) {
String songpick = rs.getString("songpick");
dlm.addElement(songpick);
}
jList1.setModel(dlm);
}
} catch (SQLException exp) {
exp.printStackTrace();
}
ps: I don't know you database structure, so I'm only guessing at the relationships
You should also take a look at The try-with-resources Statement for more details about how to manage your resources and the SQL Where Clause
Upvotes: 2