我的H2数据库中有一个表。
item_name | sac_hsn | price | Tax
我有JTextfield
和hsn_code
的字段。
现在我要做的是,当我从price
中选择item_name
时,该项目的JComboBox
和hsn_code
的数据也应该在文本字段中提取。
我已经做到了,但是没有用:-
当我运行代码时,在组合框中没有显示任何项目。它是空白。
price
然后..
Connection connection = null;
ResultSet rs;
public void commonMethodForSt(String query) {
try {
Statement st = connection.createStatement();
rs = st.executeQuery(query);
} catch (Exception e) {
// TODO Auto-generated catch block
}
}
然后,我在组合框中设置一个public void populateItemNameAndDetails() {
try {
Class.forName("org.h2.Driver");
con =
DriverManager.getConnection("jdbc:h2:C:/SimpleGST/GST","sa","");
String pname = itemcombo.getSelectedItem().toString();
commonMethodForSt("select * from additems where item_name='"+pname+"'");
if(rs.next()) {
// System.out.print(set_com);
sachsntext.setText(rs.getString("sac_hsn"));
pricetext.setText(rs.getString("price"));
taxtext.setText(rs.getString("TAX_RATE"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
并在其中调用方法。
ActionListener
答案 0 :(得分:0)
好的,我首先创建了一个方法,该方法仅将数据填充到comboBox中。
public void populateItemCombo() {
con = DriverManager.getConnection("jdbc:h2:C:/SimpleGST/GST","sa","");
itemcombo.addItem(" ");
commonMethodForSt("select * from additems");
while(rs.next()) {
itemcombo.addItem(rs.getString("item_name"));
}
}
并调用该方法。
和我作为问题询问的代码保持不变。