我在mysql中有一个非常简单的表,我能够将表中的所有名称显示在组合框中,但我想在字段上显示等效数量。似乎没有运行时错误,但没有任何反应,请帮助。
我在点击添加项目时添加了该方法。以下是我添加项目的代码以及从组合框中获取金额的方法。
private void InsertbtActionPerformed(java.awt.event.ActionEvent evt) {
try {
String sql = "Insert into item (NAME,AMOUNT) values (?,?)";
pst = con.prepareStatement(sql);
String value = INSERTdrp.getSelectedItem().toString();
pst.setString(1,value);
pst.setString(2, AMOUNTfld.getText());
pst.execute();
} catch (SQLException ex ) {
System.out.println("error connecting" + ex );
JOptionPane.showMessageDialog(null,"Query Failed");
}
AMOUNTfld.setText("");
updateCombo();
amountPull();
update_Table();
提取金额
private void amountPull() {
try {
String query = "select * from item";
st = con.prepareStatement(query);
String value = INSERTdrp.getSelectedItem().toString();
pst.setString(1,value);
rs = st.executeQuery(query);
while(rs.next()) {
String amount = rs.getString("AMOUNT");
amountDisplay.setText(amount);
}
} catch(SQLException ex) {
JOptionPane.showMessageDialog(null,ex);
}}
以下是我的代码的另一部分
private void fillCombo() {
try {
String query = "select * from item";
st = con.prepareStatement(query);
rs = st.executeQuery(query);
while(rs.next()) {
String name = rs.getString("NAME");
INSERTdrp.addItem(name);
}
} catch(Exception ex) {
}}
private void updateCombo() {
INSERTdrp.removeAllItems();
try {
String query = "select * from item";
st = con.prepareStatement(query);
rs = st.executeQuery(query);
while(rs.next()) {
String name = rs.getString("NAME");
INSERTdrp.addItem(name);
}
} catch(SQLException ex) {
JOptionPane.showMessageDialog(null,ex);
}
}
private void update_Table() {
try {
String query = "select * from item";
st = con.prepareStatement(query);
rs = st.executeQuery(query);
tableList.setModel(DbUtils.resultSetToTableModel(rs));
} catch(SQLException ex) {
JOptionPane.showMessageDialog(null,ex);
}
}