【发布时间】:2014-03-21 10:05:44
【问题描述】:
我有 2 张桌子: 沙龙库存 类别
salon_stock 表有一个类别表的 fk 列 cat_id
类别表有 2 列 cat_id 和标题
在下面的代码中,我试图插入属于类别名称的 cat_id 从 JComboBox 以及 salon_stock 表的数据中检索
private void AddStock() 抛出 SQLException,NullPointerException {
int catID = 0;
String cat=cat_list.getSelectedItem().toString();
if (cat.equals("Select a Category")) {
ResultSet rs = con.createStatement().executeQuery("select * from categories where title=' Default'");
if(rs.next()){
catID=rs.getInt("cat_id");
}
else{
throw new SQLException("The 'Default' category was not found in the Categories Table");
}
}
else{
ResultSet rs = con.createStatement().executeQuery("select * from categories where title=' "+cat+"'");
if(rs.next()){
catID=rs.getInt("cat_id");
}
}
ps1 = con.prepareStatement("insert into "+tableName+"(title,price,qty,cat_id) values( ?,?,?,?)");
ps1.setString(1,title_field.getText());
ps1.setInt(2,Integer.parseInt(price_field.getText()));
ps1.setInt(3,Integer.parseInt(qty_field.getText()));
ps1.setInt(4,catID);
ps1.executeUpdate();
ps1.closeOnCompletion();
}
代码运行良好,但在表中,它将“0”值插入 stock_table 的 cat_id 列。
我无法检测到错误.. 请帮帮我..
【问题讨论】: