I have a table as shown below:
mysql> select * from 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 am trying to insert a new Row as Popcorn Bucket so that it looks this way:
mysql> select * from 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 this is updating the existing row.
Could anybody please tell me what's wrong?
String vendor_brand_id ="3000";
String reqstr ="Popcorn";
String value="Bucket";
sqlselect = "select category_id from category where T1 = ? AND T2!=NULL AND 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 from category where T1 = ? AND T2=? AND 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 into 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+"' where T1 ='"+valuess[0]+"' AND vendor_brand_id = "+vendor_brand_id+" " ;
updateStatement.executeUpdate(sql);
}
MySQL handles NULL in a rather special manner ; from the article: "You cannot use arithmetic comparison operators such as =, <, or <> to test for NULL"
Try changing your first SELECT statement:
/* Old */
select category_id from category where T1 = ? AND T2!=NULL AND vendor_brand_id = ?
/* New */
SELECT category_id FROM category WHERE T1=? AND T2 IS NOT NULL AND vendor_brand_id=?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.