質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
JSP

JSP(Java Server Pages)とは、ウェブアプリケーションの表示レイヤーに使われるサーバーサイドの技術のことです。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Eclipse

Eclipseは、IBM社で開発された統合開発環境のひとつです。2001年11月にオープンソース化されました。 たくさんのプラグインがあり自由に機能を追加をすることができるため、開発ツールにおける共通プラットフォームとして位置づけられています。 Eclipse自体は、Javaで実装されています。

servlet

Servletとは、Webページの動的な生成やデータ処理などをサーバ上で実行するために、Javaで作成されたプログラムです。 ショッピングサイトやオンラインバンキングといった、動的なウェブサイトの構築に用いられています。

Q&A

1回答

559閲覧

jspで表示されているDBの内、特定の1行のデータのみをDaoに送信したい

hagin0

総合スコア0

JSP

JSP(Java Server Pages)とは、ウェブアプリケーションの表示レイヤーに使われるサーバーサイドの技術のことです。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Eclipse

Eclipseは、IBM社で開発された統合開発環境のひとつです。2001年11月にオープンソース化されました。 たくさんのプラグインがあり自由に機能を追加をすることができるため、開発ツールにおける共通プラットフォームとして位置づけられています。 Eclipse自体は、Javaで実装されています。

servlet

Servletとは、Webページの動的な生成やデータ処理などをサーバ上で実行するために、Javaで作成されたプログラムです。 ショッピングサイトやオンラインバンキングといった、動的なウェブサイトの構築に用いられています。

0グッド

0クリップ

投稿2022/04/22 03:03

jspで作っている一覧画面から、更新画面への遷移をするプログラムを作っています。
更新画面には、更新前のデータをDBから呼び出し、一覧の表を抜き出すような形で表示したいと考えています。
その上で、2点わからないことがあります。

①jspで更新ボタンを押した際に、その1行のデータをDaoに送る方法
②①で送られたデータを、Dao内のSELECT構文に組み込む方法

現在書いているプログラムは以下の通りです。

List.jsp

1<body> 2 3 <h1>在庫管理システム</h1> 4 <input type="image" src="img/検索ボタン.png" class="search" alt="検索"> 5 <a href="/rsc.kana.sato/ProductRegistrationServlet"><input type="image" src="img/新規登録ボタン.png" class="register" alt="新規登録"></a> 6 <table border="1"> 7 <tr> 8 <th class="id">商品ID</th> 9 <th class="image">画像</th> 10 <th class="maker">メーカー名</th> 11 <th class="name">商品名</th> 12 <th class="genre">ジャンル</th> 13 <th class="stock">在庫数</th> 14 <th class="sales">販売個数</th> 15 <th class="amount">価格</th> 16 <th class="update">更新</th> 17 </tr> 18 <% String val =(String)session.getAttribute("prodList"); %> 19 <c:forEach var="prodData" items="${prodList}"> 20 <tr> 21 <td>${prodData.productID}</td> 22 <td> 23 <img src="img/Aandyou_SW_E07_一覧用.png" height="100px"> 24 </td> 25 <td>${prodData.productMaker}</td> 26 <td>${prodData.productName}</td> 27 <td><c:out value="${ProductGenre.getValueByCode(prodData.productGenreCode).getGenreName()}"/></td> 28 <td>${prodData.productStock}</td> 29 <td>${prodData.productSales}</td> 30 <td>${prodData.productPrice}</td> 31 <td> 32 <form action="/rsc.kana.sato/ProductEditServlet" method="post"> 33 <input type="image" src="img/更新ボタン.png" class="Update" alt="更新" name="prodID"> 34 </form> 35 </td> 36 </tr> 37 </c:forEach> 38 </table> 39 </body>

Dao.jsp

1public void editInformation(ProductDto prodDto) throws ClassNotFoundException, SQLException { 2 Connection conn = connToDb(); 3 PreparedStatement ps = conn.prepareStatement( 4 "SELECT product_ID,product_genre_code,product_name,product_maker,product_price,product_stock,product_sales,product_remarks FROM t_product;"); 5 conn.setAutoCommit(false); 6 ArrayList<ProductDto> prodList = new ArrayList<>(); 7 ResultSet rs = ps.executeQuery(); 8 rs.next(); 9 dto.ProductDto prodUpdateDto = new dto.ProductDto(); 10 prodUpdateDto.setProductID(rs.getInt("product_ID")); 11 prodUpdateDto.setProductGenreCode(rs.getString("product_genre_code")); 12 prodUpdateDto.setProductName(rs.getString("product_name")); 13 prodUpdateDto.setProductMaker(rs.getString("product_maker")); 14 prodUpdateDto.setProductPrice(rs.getInt("product_price")); 15 prodUpdateDto.setProductStock(rs.getInt("product_stock")); 16 prodUpdateDto.setProductSales(rs.getInt("product_sales")); 17 prodUpdateDto.setProductRemarks(rs.getString("product_remarks")); 18 19 prodList.add(prodUpdateDto); 20 21 conn.commit(); 22 } 23 24 public void updateInformation(ProductDto prodDto) throws ClassNotFoundException, SQLException { 25 Connection conn = connToDb(); 26 PreparedStatement ps = conn.prepareStatement( 27 "UPDATE t_product SET product_ID = ?, product_genre_code = ?, product_name = ?, product_maker = ?, product_price = ?, product_stock = ?, product_sales = ?, product_remarks = ? WHERE product_ID;"); 28 ps.setInt(1, prodDto.getProductID()); 29 ps.setString(2, prodDto.getProductGenreCode()); 30 ps.setString(3, prodDto.getProductName()); 31 ps.setString(4, prodDto.getProductMaker()); 32 ps.setInt(5, prodDto.getProductPrice()); 33 ps.setInt(6, prodDto.getProductStock()); 34 ps.setInt(7, prodDto.getProductSales()); 35 ps.setString(8, prodDto.getProductRemarks()); 36 37 ps.executeUpdate(); 38 39 }

EditServlet.java

1protected void doPost(HttpServletRequest request, HttpServletResponse response) 2 throws ServletException, IOException { 3 // TODO Auto-generated method stub 4 request.setCharacterEncoding("UTF-8"); 5 dao.ProductDao prodDao = new dao.ProductDao(); 6 ProductDto prodDto = new ProductDto(); 7 try { 8 prodDao.editInformation(prodDto); 9 request.setAttribute("prodDto", prodDto); 10 } catch (ClassNotFoundException e) { 11 // TODO 自動生成された catch ブロック 12 e.printStackTrace(); 13 } catch (SQLException e) { 14 // TODO 自動生成された catch ブロック 15 e.printStackTrace(); 16 } 17 RequestDispatcher dispatch = request.getRequestDispatcher("WEB-INF/jsp/ProductUpdate.jsp"); 18 dispatch.forward(request, response); 19 }

UpdateServlet.java

1protected void doPost(HttpServletRequest request, HttpServletResponse response) 2 throws ServletException, IOException { 3 // TODO Auto-generated method stub 4 response.setCharacterEncoding("UTF-8"); 5 response.setContentType("texthtml ;charset=UTF-8"); 6 request.setCharacterEncoding("UTF-8"); 7 8 String prodId = request.getParameter("prodId"); 9 String prodGenreCode = request.getParameter("prodGenreCode"); 10 String prodName = request.getParameter("prodName"); 11 String prodMaker = request.getParameter("prodMaker"); 12 String prodAmount = request.getParameter("prodAmount"); 13 String prodStock = request.getParameter("prodStock"); 14 String prodSales = request.getParameter("prodSales"); 15 String prodRemarks = request.getParameter("prodRemarks"); 16 17 dto.ProductDto prodDto = new dto.ProductDto(); 18 prodDto.setProductID(Integer.parseInt(prodId)); 19 prodDto.setProductGenreCode(prodGenreCode); 20 prodDto.setProductName(prodName); 21 prodDto.setProductMaker(prodMaker); 22 prodDto.setProductPrice(Integer.parseInt(prodAmount)); 23 prodDto.setProductStock(Integer.parseInt(prodStock)); 24 prodDto.setProductSales(Integer.parseInt(prodSales)); 25 prodDto.setProductRemarks(prodRemarks); 26 request.setAttribute("prodDto", prodDto); 27 28 dao.ProductDao prodDao = new dao.ProductDao(); 29 try { 30 prodDao.updateInformation(prodDto); 31 } catch (SQLException | ClassNotFoundException e) { 32 try { 33 if (prodDao.getConnect() != null) { 34 prodDao.getConnect().rollback(); 35 } 36 } catch (SQLException ex) { 37 ex.printStackTrace(); 38 } 39 e.printStackTrace(); 40 } 41 42 String urlRedir = "/rsc.kana.sato/ProductListServlet"; 43 response.sendRedirect(urlRedir); 44 }

Update.jsp

1<body> 2 <table border="1"> 3 <tr> 4 <th class="id">商品ID</th> 5 <th class="image">画像</th> 6 <th class="maker">メーカー名</th> 7 <th class="name">商品名</th> 8 <th class="genre">ジャンル</th> 9 <th class="stock">在庫数</th> 10 <th class="sales">販売個数</th> 11 <th class="amount">価格</th> 12 </tr> 13 <% String val =(String)session.getAttribute("prodDto");%> 14 <tr> 15 <td>${prodDto.productID}</td> 16 <td> 17 <img src="img/Aandyou_SW_E07_一覧用.png" height="100px"> 18 </td> 19 <td>${prodDto.productMaker}</td> 20 <td>${prodDto.productName}</td> 21 <td><c:out value="${ProductGenre.getValueByCode(prodDto.productGenreCode).getGenreName()}"/></td> 22 <td>${prodDto.productStock}</td> 23 <td>${prodDto.productSales}</td> 24 <td>${prodDto.productPrice}</td> 25 </table> 26 <form class="UpdateForm" action="/rsc.kana.sato/ProductUpdateServlet" method="post"> 27 <div class="goodIDAndGenre"> 28 <div class="goodId"> 29 <p>商品ID</p> 30 <input type="text" name="prodId"> 31 </div> 32 <div> 33 <p>ジャンル</p> 34 <select name="prodGenreCode"> 35 <c:forEach var="genre" items="${ProductGenre.values()}"> 36 <option value="${genre.genreCode}" 37 <c:if test="${bean.product_genre_code == genre.genreCode}">selected</c:if>> 38 ${genre.genreName}</option> 39 </c:forEach> 40 </select> 41 </div> 42 </div> 43 <div class="goodNameAndMaker"> 44 <div class="goodName"> 45 <p>商品名</p> 46 <input type="text" name="prodName"> 47 </div> 48 <div class="Maker"> 49 <p>メーカー名</p> 50 <select name="prodMaker"> 51 <c:forEach var="maker" items="${ProductMaker.values()}"> 52 <option value="${maker.makerName}" 53 <c:if test="${bean.product_genre_maker == maker.makerName}">selected</c:if>> 54 ${maker.makerName}</option> 55 </c:forEach> 56 </select> 57 </div> 58 </div> 59 <div class="amountAndStock"> 60 <div class="amount"> 61 <p>金額</p> 62 <input type="text" name="prodAmount"> 63 </div> 64 <div class="stock"> 65 <p>在庫</p> 66 <input type="text" name="prodStock"> 67 </div> 68 </div> 69 <p>販売個数</p> 70 <input type="text" name="prodSales"> 71 <p>備考</p> 72 <textarea rows="10" cols="40" name="prodRemarks"></textarea> 73 <div class="registerBotten"> 74 <input type="image" src="img/更新ボタン.png" alt="更新"> 75 </div> 76 </form> 77 </body>

すみませんが、ご教示をお願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

①jspで更新ボタンを押した際に、その1行のデータをDaoに送る方法

通常はデータそのものではなくキーとなる情報だけリクエストに含めて送ります。
GETとかで良いでしょう(formで送信しなくてもクリエストリングで十分です)

②①で送られたデータを、Dao内のSELECT構文に組み込む方法

リクエストが取れればあとは渡すだけです。
キー=一意となる情報なので、where句で絞込を。


考え方はteratailの質問一覧→質問詳細 と同じです。

投稿2022/04/22 03:14

編集2022/04/22 03:25
m.ts10806

総合スコア80850

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問