我有一张如下表所示的表格:
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 |
我正在尝试将新行插入Popcorn Bucket,以便它看起来像这样:
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 |
但这是在更新现有行。
有人可以告诉我出了什么问题吗?
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);
}
答案 0 :(得分:2)
MySQL处理NULL in a rather special manner;来自文章:"您不能使用算术比较运算符,例如=,<或<>测试NULL"
尝试更改第一个SELECT语句:
/* 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=?