【发布时间】:2017-04-07 15:50:36
【问题描述】:
我有一个程序要求我输入产品的 ID,然后搜索数据库以显示有关该特定产品的详细信息。
该代码适用于数据库中除 productID 2 和 productID 4 之外的每个项目,因为它们没有数据存储在某一列 (PRODUCT_TYPE) 中。
我的代码中有一条 if 语句:
else if (result.getString("PRODUCT_TYPE") == null && result.getString("DESCRIPTION ") == null)
{
System.out.println("Item ID: " + result.getString("STOCKITEM") + ". \nProduct name: " + result.getString("PRODUCT") + ". \nCost: £" + result.getString("COST") + ". \nStock level: " + result.getString("STOCKLEVEL") + ". \nProduct type: null.");
}
但我不明白为什么如果产品类型为空,它无法找到产品,但如果描述为空,仍然可以找到它。
try{
String searchCode = "SELECT PRODUCTS.PRODUCT, PRODUCTS.COST, PRODUCTS.DESCRIPTION, PRODUCT_STOCK.STOCKITEM , PRODUCT_STOCK.STOCKLEVEL FROM PRODUCT_TYPE INNER JOIN (PRODUCTS FULL OUTER JOIN PRODUCT_STOCK ON PRODUCTS.PRODUCTID = PRODUCT_STOCK.STOCKITEM) ON PRODUCT_TYPE.PRODTYPEID = PRODUCTS.PRODUCT_TYPE WHERE PRODUCTID = " + inputValue + ";";
// sql statement searches the database for the required data
Statement statement = dbConnection.createStatement();
ResultSet result = statement.executeQuery(searchCode);
boolean found = false;
found = result.next();
if (!found) { //if no data is found then display the error message and restart the product search method
System.out.println("That product couldn't be found, please try again.");
searchProduct();
}
else if (result.getString("PRODUCT_TYPE") == null && result.getString("DESCRIPTION ") == null)
{
System.out.println("Item ID: " + result.getString("STOCKITEM") + ". \nProduct name: " + result.getString("PRODUCT") + ". \nCost: £" + result.getString("COST") + ". \nStock level: " + result.getString("STOCKLEVEL") + ". \nProduct type: null.");
}
else if (result.getString("PRODUCT_TYPE") == null && result.getString("DESCRIPTION") != null){
System.out.println("Item ID: " + result.getString("STOCKITEM") + ". \nProduct name: " + result.getString("PRODUCT") + ". \nCost: £" + result.getString("COST") + ". \nStock level: " + result.getString("STOCKLEVEL") + ". \nDescription: " + result.getString("DESCRIPTION") + ". \nProduct type: null.");
}
else if (result.getString("PRODUCT_TYPE") != null && result.getString("DESCRIPTION") == null){
System.out.println("Item ID: " + result.getString("STOCKITEM") + ". \nProduct name: " + result.getString("PRODUCT") + ". \nCost: £" + result.getString("COST") + ". \nStock level: " + result.getString("STOCKLEVEL") + ". \nDescription: null. \nProduct type: " + result.getString("PRODTYPE_DESC") + ".");
}
else if (result.getString("PRODUCT_TYPE") != null && result.getString("DESCRIPTION") != null){
System.out.println("Item ID: " + result.getString("STOCKITEM") + ". \nProduct name: " + result.getString("PRODUCT") + ". \nCost: £" + result.getString("COST") + ". \nStock level: " + result.getString("STOCKLEVEL") + ". \nDescription: " + result.getString("DESCRIPTION") + ". \nProduct type: " + result.getString("PRODTYPE_DESC") + ".");
}
}
catch (Exception e){ // if the product can't be found then display the error message and restart the product search method
System.out.println("Could not find product.");
System.out.println(e.toString());
searchProduct();
}
//goes back to menu once the method has finished
askUser();
}
任何帮助将不胜感激。谢谢。
【问题讨论】:
-
您的 SQL 正在对 PRODUCTS.PRODUCT_TYPE 执行 JOIN,它为空。这意味着 join 不会加入任何东西,因为没有任何东西可以与 null 匹配。
-
那么有什么办法可以解决这个问题吗?没有其他任何东西可以链接到另一个表,该表是表中关于另一个产品类型表的唯一列。