課題で売上を入力し登録するシステムを開発しています。
「取引先」と「商品名」は<select><option>を使ってドロップダウンになっています。
画面の方で表示はできているのですが、どちらも選択されたものを取得することができず最初に表示されている値しか取得することができません。
あと、選択された名前とそれに関連するデータも取得したいのですが、そこもご教授いただければ幸いです。
「取引先」の場合はコードも一緒に、「商品名」の場合はコードと商品単価も一緒に取得したいです。
【取引先を管理しているテーブル】
CLIENTCODE(取引先コード) | CLIENTNAME(取引先名) | |
---|---|---|
1 | 株式会社あいうえお | |
2 | かきくけこ商事 |
【商品を管理しているテーブル】
PRODUCTCODE(商品コード) | PRODUCTNAME(商品名) | PRODUCTORICE(商品単価) |
---|---|---|
1 | ボールペン | 100 |
2 | ノート | 200 |
<疑問点>
・ドロップダウンで選択された値を持ってくる方法
・選択された名前とそれと関連するデータも取得する方法(名前以外のコードや商品単価)
よろしくお願いします。
ClientInsert.jsp
1<!-- 売上登録画面 --> 2<table border="1" style="border-collapse: collapse"> 3 <tr style="background-color: #ffdab9;"> 4 <th>受注日</th> 5 <th>取引先</th> 6 <th>商品名</th> 7 <th>売上個数</th> 8 <th>売上金額</th> 9 </tr> 10 <tr> 11 <td> 12 <input type="text" name="orderDate" value="<%=src0101Dto.getOrderDate() %>"> <!-- 受注日 --> 13 </td> 14 <td> 15 <select name="clientCode"> 16 <% for(ClientDto dt : src0101Dto.getListClientNames()){ %> 17 <option value="<%=dt.getClientCode() %>"><%=dt.getClientName() %></option> <!-- 取引先 --> 18 <% } %> 19 </select> 20 </td> 21 <td> 22 <select name="productCode"> 23 <% for(ProductDto dt : src0101Dto.getListProductNames()){ %> 24 <option value="<%=dt.getProductCode() %>"><%=dt.getProductName() %></option> <!-- 商品名 --> 25 <% } %> 26 </select> 27 </td> 28 <td><input type="text" name="salesQuantity"></td> <!-- 売上個数 --> 29 <td><input type="text" name="salesPrice"></td> <!-- 売上金額 --> 30 </tr> 31</table> 32 33<!-- 売上登録ボタン --> 34<input type="button" onclick="salesInsert();" value="売上登録"><br>
ClientInsertServlet.java
1protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 2 3 ////////////////////////////////////////////////////////////////////////////////////////// 4 // 初期処理 5 SalesDto salesDto = new SalesDto(); 6 ClientInsertBsLogic bslogic = new ClientInsertBsLogic(); 7 8 // ---------- パラメータ取得 ---------- 9 // ---------- 画面情報設定 ---------- 10 // 受注日 11 String orderDate = request.getParameter("orderDate"); 12 if(orderDate == null) { 13 // 取得できない場合は現在時刻 14 LocalDateTime today = LocalDateTime.now(); 15 DateTimeFormatter dateformat = DateTimeFormatter.ofPattern("yyyy/MM/dd"); 16 orderDate = dateformat.format(today); 17 } 18 salesDto.setOrderDate(orderDate); 19 20 // 取引先コード 21 String clientCode = request.getParameter("clientCode"); 22 salesDto.setClientCode(clientCode); 23 24 // 取引先名 25 String clientName = request.getParameter("clientName"); 26 salesDto.setClientName(clientName); 27 28 // 商品コード 29 String productCode = request.getParameter("productCode"); 30 salesDto.setProductCode(productCode); 31 32 // 商品名 33 String productName = request.getParameter("productName"); 34 salesDto.setProductName(productName); 35 36 // 商品単価 37 String productPrice = request.getParameter("productPrice"); 38 salesDto.setProductPrice(productPrice); 39 40 // 売上個数 41 String salesQuantity = request.getParameter("salesQuantity"); 42 salesDto.setSalesQuantity(salesQuantity); 43 44 // 売上金額 45 String salesPrice = request.getParameter("salesPrice"); 46 salesDto.setSalesPrice(salesPrice); 47 48 SalesDto hantei = bslogic.salesInsert(salesDto); 49 50 if(orderDate != null) { 51 52 if(hantei != null) { 53 54 request.setAttribute("salesDto", salesDto); 55 request.getRequestDispatcher("ClientInsert.jsp").forward(request, response); 56 57 } 58 59 }else { 60 61 String view = "ClientInsert.jsp"; 62 RequestDispatcher dispatcher = request.getRequestDispatcher(view); 63 dispatcher.forward(request, response); 64 65 } 66 67 }
ClientInsertBsLogic.java
1public class ClientInsertBsLogic { 2 3 public SalesDto salesInsert(SalesDto dto) { 4 5 SalesDao dao = new SalesDao(); 6 return dao.salesInsert(dto); 7 8 } 9 10}
Dao.jsp
1public class SalesDao { 2 3 DBConnection dbcon = new DBConnection(); 4 Statement stmt = dbcon.connection(); 5 ResultSet rs = null; 6 7 /* 売上登録 */ 8 public SalesDto salesInsert(SalesDto dto) { 9 10 try { 11 12 String sql = "INSERT INTO TBLSALES VALUES (SEQ_ID, " + dto.getOrderDate() + ", " + dto.getClientCode() + ", " + dto.getClientName() + ", " + dto.getProductCode() + ", " + dto.getProductName() + ", " + dto.getProductPrice() + ", " + dto.getSalesQuantity() + ", " + dto.getSalesPrice() + ")"; 13 int count = stmt.executeUpdate(sql); 14 15 System.out.println(count + "件登録しました。"); 16 17 }catch(SQLException e) { 18 return null; 19 }finally { 20 21 try { 22 23 if(rs != null) { 24 rs.close(); 25 } 26 27 if(stmt != null) { 28 stmt.close(); 29 } 30 31 }catch(Exception e) { 32 return null; 33 } 34 35 } 36 return dto; 37 38 } 39 40}
あなたの回答
tips
プレビュー