can't get the category ID ( java/sql ) -
i have 2 tables: salon_stock categories
the salon_stock table has fk column cat_id of categories table
categories table have 2 columns cat_id & title
in below code trying insert cat_id belong category name retrieved jcombobox along data of salon_stock table
private void addstock() throws sqlexception,nullpointerexception {int catid = 0;
string cat=cat_list.getselecteditem().tostring();
if (cat.equals("select category")) { resultset rs = con.createstatement().executequery("select * categories title=' default'"); if(rs.next()){ catid=rs.getint("cat_id"); } else{ throw new sqlexception("the 'default' category not found in categories table"); } } else{ resultset rs = con.createstatement().executequery("select * categories title=' "+cat+"'"); if(rs.next()){ catid=rs.getint("cat_id"); } } ps1 = con.preparestatement("insert "+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(); }
the code runs fine in table, inserts '0' value cat_id column of stock_table.
i unable detect error.. please me..
maybe space in query error:
"select * categories title=' "+cat+"'"
must be
"select * categories title='"+cat+"'"
Comments
Post a Comment