UPDATEしたはずのデータがデータベースで更新されていないです。
最初試しにSQL文のUPDATEが動くか確かめて、その時には動きました。
しかし、更新画面から更新ボタンを押して(そこでUpdateControllerが呼び出されて、SQL文のUPDATEが実施される)、
商品一覧画面に戻る時にはDBの情報が更新されていません。
どこを直せばいいのでしょうか?
画面の遷移時でおかしくなっているのでしょうか?
UpdateControllerには
public class UpdateController extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public UpdateController() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); String name = request.getParameter("name"); String code = request.getParameter("code"); String price = request.getParameter("unitPrice"); String count = request.getParameter("count"); String ispr = request.getParameter("ispr"); String image = request.getParameter("image"); String recodeDate = request.getParameter("recodeDate"); String sysdate = request.getParameter("sysdate"); request.setAttribute("searchingName", name); request.setAttribute("searchingCode", code); request.setAttribute("searchingPrice", price); request.setAttribute("searchingCount", count); T001ItemDao itemDao; String nextPage = ""; try { itemDao = new T001ItemDao(); int itembean = itemDao.update(code, name, price, count); request.setAttribute("itembean", itembean); String updateRecodeDate = itemDao.currentTimeGet(sysdate); if (updateRecodeDate.equals("") || updateRecodeDate == null) { request.setAttribute("errorMessage", "削除済みエラー"); nextPage = "/list.jsp"; } else { String editRecodeDate = (String) request.getSession() .getAttribute("editRecodeDate"); if (editRecodeDate.equals(updateRecodeDate)) { itemDao = new T001ItemDao(); int result = itemDao.update(code, name, price, count); nextPage = "/list.jsp"; if (result == 0) { request.setAttribute("errorMessage", "更新失敗エラー"); nextPage = "/edit.jsp"; } } } } catch (SQLException e) { // TODO 自動生成された catch ブロック e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO 自動生成された catch ブロック e.printStackTrace(); } ServletContext application = getServletContext(); application.getRequestDispatcher(nextPage).forward(request, response); } }
Daoクラスには
public class T001ItemDao { //引数:商品コード、商品名、金額、数量 //戻り値:結果更新できたか否か YES or NO public int update(String code,String name,String price,String count) { int result = 0; try { Statement stmt = conn.createStatement(); String sql = "UPDATE T001_ITEM SET" + " ITEM_NM ='" + name + "'," + " UNIT_PRICE = '" + price + "', " + " STOCK_COUNT = '" + count + "'," + " RECORD_DATE = CURRENT_TIMESTAMP"+ " WHERE ITEM_NO = " + code; result = stmt.executeUpdate(sql); }catch (SQLException e){ System.out.println("SQLException:" + e.getMessage()); } return result; } public ItemBean getItemInfo(String code){ String sql = "SELECT ITEM_NO,ITEM_NM,UNIT_PRICE,STOCK_COUNT FROM T001_ITEM WHERE ITEM_NO ='" + code + "'"; ItemBean ItemInfo = new ItemBean(); try { Statement statement = conn.createStatement(); ResultSet resultSet = statement.executeQuery(sql); resultSet.next(); ItemInfo.setCode(resultSet.getString("ITEM_NO")); ItemInfo.setName(resultSet.getString("ITEM_NM")); ItemInfo.setPrice(resultSet.getString("UNIT_PRICE")); ItemInfo.setCount(resultSet.getString("STOCK_COUNT")); System.out.println(ItemInfo.getCode()); System.out.println(ItemInfo.getName()); System.out.println(ItemInfo.getCount()); System.out.println(ItemInfo.getPrice()); } catch (SQLException e) { // TODO 自動生成された catch ブロック e.printStackTrace(); } return ItemInfo; } }
edit.jsp(商品更新の画面)には
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <html xml:lang="ja" lang="ja"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <!-- <link type="text/css" rel="stylesheet" href="exValidation/css/style.css" /> --> <link type="text/css" rel="stylesheet" href="exValidation/css/exvalidation.css" /> <title>CRUDサンプル</title> </head> <body> <h1>商品変更</h1> > <a href="./list.html">一覧</a><br><br> <form action="/edit/{{ person.key }}" method="post"> <table cellspacing="1" cellpadding="8" border="0" bgcolor="#999999"> <tbody><tr> <th width="100" bgcolor="#EBEBEB">商品コード</th> <td width="250" bgcolor="#FFFFFF"><input type="text" id="code" name="code" readonly="readonly" value=""></td> </td></tr> <tr> <th width="100" bgcolor="#EBEBEB">商品名<sup><font color="#FF0000">*</font></sup></th> <td width="250" bgcolor="#FFFFFF"><input type="text" id="name" name="name" value=""> </td> </tr> <tr> <th width="100" bgcolor="#EBEBEB">金額<sup><font color="#FF0000">*</font></sup></th> <td width="250" bgcolor="#FFFFFF"><input type="text" id="unitPrice" name="unitPrice" value=""> </td> </tr> <tr> <th width="100" bgcolor="#EBEBEB">数量<sup><font color="#FF0000">*</font></sup></th> <td width="250" bgcolor="#FFFFFF"><input type="text" id="count" name="count" value=""> </td> </tr> <tr> <th width="100" bgcolor="#EBEBEB">商品画像</th> <td width="250" bgcolor="#FFFFFF"><input type="file" id="image" name="image"> </td> </tr> <tr> <th bgcolor="#EBEBEB">おすすめ商品</th> <td bgcolor="#FFFFFF"><input type="checkbox" id="isPR" name="isPR" value="True"{% if item.isPR %} checked{% endif %}>おすすめ商品棚に並べる</td> </tr> </tbody></table><br> <input type="submit" value="変更する"> </form> <br> <font color="#FF0000">*</font>は必須項目 </body> var validation = $("form") .exValidation({ rules: { name: "chkrequired chkmax200", unitPrice: "chkrequired chknumonly chkmin1 chkmax4", count: "chkrequired chknumonly chkmin1 chkmax2", // image: "chkfile" }, stepValidation: true }); [removed] </html>
と書きました。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/12/18 04:22
2016/12/18 07:54