【发布时间】:2015-07-26 18:53:52
【问题描述】:
我知道如何将组合框值插入到 sql 中,但不知道如何将组合框值替换为 sql 中的数字。
这是我的组合框编码和处理按钮的一部分。
User.java
JComboBox comboBox_1 = new JComboBox();
comboBox_1.setBounds(126, 105, 140, 20);
contentPane.add(comboBox_1);
comboBox_1.addItem("RM100-RM200");
comboBox_1.addItem("RM200-RM300");
comboBox_1.addItem("RM300-RM400");
JButton btnNewButton = new JButton("Process");
btnNewButton.setBounds(360, 296, 89, 23);
contentPane.add(btnNewButton);
btnNewButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String a=(String)comboBox.getSelectedItem().toString();
Case ca= new Case();
try {
ca.addPlace(a);
LoginGUI um= new LoginGUI();
um.setVisible(true);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
Case.java
public void addPlace( String r) throws Exception{
DatabaseConnection db=new DatabaseConnection();
Connection connect=db.getConnection();
String sql="Insert into menu(Budget)VALUES (?)";
PreparedStatement ps=connect.prepareStatement(sql);
ps.setString(1,r);
ps.executeUpdate();
connect.close();
ps.close();
}
假设用户选择 RM100-RM200,值“1”将插入到 sql 中,而不是 RM100-RM200。有人可以帮忙吗?
【问题讨论】:
标签: java mysql sql-server combobox