mysql - Java Insert Or Update in SQL logic -
mysql - Java Insert Or Update in SQL logic -
i have table shown below:
class="lang-none prettyprint-override">mysql> select * category; +-------------+----------+------+------+------+------+------+------+------+------+------+-----------------+ | category_id | t1 | t2 | t3 | t4 | t5 | t6 | t7 | t8 | t9 | t10 | vendor_brand_id | +-------------+----------+------+------+------+------+------+------+------+------+------+-----------------+ | 12 | popcorn | rgp | null | null | null | null | null | null | null | null | 3000 |
i trying insert new row popcorn bucket looks way:
class="lang-none prettyprint-override">mysql> select * category; +-------------+----------+------+------+------+------+------+------+------+------+------+-----------------+ | category_id | t1 | t2 | t3 | t4 | t5 | t6 | t7 | t8 | t9 | t10 | vendor_brand_id | +-------------+----------+------+------+------+------+------+------+------+------+------+-----------------+ | 12 | popcorn | rgp | null | null | null | null | null | null | null | null | 3000 | | 13 | popcorn | bucket | null | null | null | null | null | null | null | null | 3000 |
but updating existing row.
could please tell me what's wrong?
class="lang-java prettyprint-override">string vendor_brand_id ="3000"; string reqstr ="popcorn"; string value="bucket"; sqlselect = "select category_id category t1 = ? , t2!=null , vendor_brand_id = ?"; selectpst = dbconnection.preparestatement(sqlselect); selectpst.setstring(1, valuess[0]); selectpst.setstring(2, vendor_brand_id); resultset = selectpst.executequery(); if(resultset.next()) { sql= "select category_id category t1 = ? , t2=? , vendor_brand_id = ?"; selectpst2 = dbconnection.preparestatement(sql); selectpst2.setstring(1, valuess[0]); selectpst2.setstring(2, value); selectpst2.setstring(3, vendor_brand_id); resultset2 = selectpst2.executequery(); if(resultset2.next()) { } else { sql = "insert category (t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,vendor_brand_id) values ('"+valuess[0]+"','"+value+"',null,null,null,null,null,null,null,null,null,"+vendor_brand_id+") "; updatestatement.executeupdate(sql); } } else { sql = "update category set t2 = '"+value+"' t1 ='"+valuess[0]+"' , vendor_brand_id = "+vendor_brand_id+" " ; updatestatement.executeupdate(sql); }
mysql handles null in rather special manner; article: "you cannot utilize arithmetic comparing operators such =, <, or <> test null"
try changing first select statement:
/* old */ select category_id category t1 = ? , t2!=null , vendor_brand_id = ? /* new */ select category_id category t1=? , t2 not null , vendor_brand_id=?
java mysql sql
Comments
Post a Comment