dante dante - 4 years ago 107
SQL Question

Spring MVC, send data to MySQL with IF statement

I'm making an online shop, and... could somebody tell me why this code doesn't work?

If I write SQL statement without IF then it works fine, but I have to check which phone I chose.

PhoneController:

@RequestMapping(value="/cart.html", method = RequestMethod.POST)
public ModelAndView addToCart(@RequestParam String selectedPhone, Phone phone, DBConnection db) throws ClassNotFoundException{

String selectiPhone = "?selectedPhone=Phone1";

if (selectedPhone.equals(selectiPhone))
{
db.Query("INSERT INTO `onlineshop`.`cart` (`name`, `company`, `type`, `price`) VALUES ('"
+ phone.getName1() + "', '"+phone.getCompany1()+"', '" + phone.getType() + "', '"
+ phone.getPrice1() +"')");
}

ModelAndView cart = new ModelAndView("Cart");
return cart;

}


fragment of Phone1.jsp:

<form action="/OnlineShop/cart.html?selectedPhone=Phone1" method="post">
<div style="padding-right: 40px">
<input type="submit" value="Add to Cart"/>
</div>
</form>

Answer Source

selectedPhone will contain "Phone1" only, not the ? and the parameter name as well.

  if ("Phone1".equals(selectedPhone)) {

Should work better for you.

Good luck.

But I've no idea how and if the Values for Phone and DBConnection will make it into your method...

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download