回答編集履歴

5

コード修正

2022/04/06 10:09

投稿

jimbe
jimbe

スコア12648

test CHANGED
@@ -28,7 +28,7 @@
28
28
  "プロダクトコードから Product オブジェクト (1 件 ) を返す" ようなメソッドを作ります。(もしかしたら既にあるのかもしれませんので仮に) ProductDAO クラスとします。
29
29
  ```java
30
30
  public class ProductDAO {
31
- public Product search(String code) throws SQLException {
31
+ public Product getProduct(String code) throws SQLException {
32
32
  String sql = "SELECT product_name FROM m_product WHERE product_code = ?";
33
33
 
34
34
  try(Connection con = getConnection();
@@ -51,7 +51,7 @@
51
51
  String code = request.getParameter("product_inf");
52
52
  int quantity = Integer.parseInt(request.getParameter("quantity"));
53
53
 
54
- Product product = new ProductDAO().search(code);
54
+ Product product = new ProductDAO().getProduct(code);
55
55
  //product が null だったり search の例外の処理は省略
56
56
 
57
57
  HttpSession session = request.getSession();

4

コード修正

2022/04/06 10:07

投稿

jimbe
jimbe

スコア12648

test CHANGED
@@ -34,10 +34,11 @@
34
34
  try(Connection con = getConnection();
35
35
  PreparedStatement pstmt = con.prepareStatement(sql);) {
36
36
  pstmt.setString(1, code);
37
- ResultSet rs = pstmt.executeQuery();
37
+ try(ResultSet rs = pstmt.executeQuery();) {
38
- if(rs.next()) {
38
+ if(rs.next()) {
39
- String name = rs.getString("product_name");
39
+ String name = rs.getString("product_name");
40
- return new Product(code, name);
40
+ return new Product(code, name);
41
+ }
41
42
  }
42
43
  }
43
44
  return null;

3

修正

2022/04/06 08:06

投稿

jimbe
jimbe

スコア12648

test CHANGED
@@ -6,9 +6,9 @@
6
6
  Product クラスは例えば
7
7
  ```java
8
8
  public class Product {
9
- final int code;
9
+ final String code;
10
10
  final String name;
11
- Product(int code, String name) {
11
+ Product(String code, String name) {
12
12
  this.code = code;
13
13
  this.name = name;
14
14
  }
@@ -28,12 +28,12 @@
28
28
  "プロダクトコードから Product オブジェクト (1 件 ) を返す" ようなメソッドを作ります。(もしかしたら既にあるのかもしれませんので仮に) ProductDAO クラスとします。
29
29
  ```java
30
30
  public class ProductDAO {
31
- public Product search(int code) throws SQLException {
31
+ public Product search(String code) throws SQLException {
32
32
  String sql = "SELECT product_name FROM m_product WHERE product_code = ?";
33
33
 
34
34
  try(Connection con = getConnection();
35
35
  PreparedStatement pstmt = con.prepareStatement(sql);) {
36
- pstmt.setInt(1, code);
36
+ pstmt.setString(1, code);
37
37
  ResultSet rs = pstmt.executeQuery();
38
38
  if(rs.next()) {
39
39
  String name = rs.getString("product_name");

2

コード修正

2022/04/06 08:01

投稿

jimbe
jimbe

スコア12648

test CHANGED
@@ -6,9 +6,9 @@
6
6
  Product クラスは例えば
7
7
  ```java
8
8
  public class Product {
9
- final String code;
9
+ final int code;
10
10
  final String name;
11
- Product(String code, String name) {
11
+ Product(int code, String name) {
12
12
  this.code = code;
13
13
  this.name = name;
14
14
  }
@@ -17,7 +17,7 @@
17
17
  とし、本システムで入力したデータは SaleData クラスで表すとします。
18
18
  ```java
19
19
  public class SaleData {
20
- final Product product:
20
+ final Product product;
21
21
  final int quantity;
22
22
  SaleData(Product product, int quantity) {
23
23
  this.product = product;
@@ -28,11 +28,11 @@
28
28
  "プロダクトコードから Product オブジェクト (1 件 ) を返す" ようなメソッドを作ります。(もしかしたら既にあるのかもしれませんので仮に) ProductDAO クラスとします。
29
29
  ```java
30
30
  public class ProductDAO {
31
- public Product search(int code) {
31
+ public Product search(int code) throws SQLException {
32
32
  String sql = "SELECT product_name FROM m_product WHERE product_code = ?";
33
33
 
34
34
  try(Connection con = getConnection();
35
- PreparedStatement pstmt = conn.prepareStatement(sql);) {
35
+ PreparedStatement pstmt = con.prepareStatement(sql);) {
36
36
  pstmt.setInt(1, code);
37
37
  ResultSet rs = pstmt.executeQuery();
38
38
  if(rs.next()) {
@@ -42,7 +42,7 @@
42
42
  }
43
43
  return null;
44
44
  }
45
- //getConnextion メソッドとか他に必要/ありそうですが省略
45
+ //getConnection メソッドとか他に必要/ありそうですが省略
46
46
  }
47
47
  ```
48
48
  AddSalesServlet の doPost メソッドでは以下のようにできるのではないでしょうか。
@@ -51,7 +51,7 @@
51
51
  int quantity = Integer.parseInt(request.getParameter("quantity"));
52
52
 
53
53
  Product product = new ProductDAO().search(code);
54
- //product が null だったり search 例外を発した場合の処理は省略
54
+ //product が null だったり search 例外の処理は省略
55
55
 
56
56
  HttpSession session = request.getSession();
57
57
  List<SaleData> saleslist = session.getAttribute("saleslist");

1

修正

2022/04/06 07:37

投稿

jimbe
jimbe

スコア12648

test CHANGED
@@ -25,8 +25,7 @@
25
25
  }
26
26
  }
27
27
  ```
28
- "プロダクトコードから Product オブジェクト (1 件 ) を返す" ようなメソッドを作ります。(もしかしたら既にあるのかもしれません。)
28
+ "プロダクトコードから Product オブジェクト (1 件 ) を返す" ようなメソッドを作ります。(もしかしたら既にあるのかもしれませんので仮に) ProductDAO クラスとします
29
- (仮に) ProductDAO クラスとします。
30
29
  ```java
31
30
  public class ProductDAO {
32
31
  public Product search(int code) {