-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProductInfoDAO.java
242 lines (233 loc) · 9.49 KB
/
ProductInfoDAO.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package com.internousdev.gerbera.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.internousdev.gerbera.dto.ProductInfoDTO;
import com.internousdev.gerbera.util.DBConnector;
public class ProductInfoDAO {
public List<ProductInfoDTO> getProductInfoList(){
DBConnector db=new DBConnector();
Connection con=db.getConnection();
List<ProductInfoDTO> productInfoDtoList=new ArrayList<ProductInfoDTO>();
//prosuct_infoからの情報をproduct_id昇格順に並べる
String sql="select * from product_info order by product_id asc";
try{
PreparedStatement ps=con.prepareStatement(sql);
ResultSet rs=ps.executeQuery();
while(rs.next()){
ProductInfoDTO productInfoDto=new ProductInfoDTO();
productInfoDto.setId(rs.getInt("id"));
productInfoDto.setProductId(rs.getInt("product_id"));
productInfoDto.setProductName(rs.getString("product_name"));
productInfoDto.setProductNameKana(rs.getString("product_name_kana"));
productInfoDto.setProductDescription(rs.getString("product_description"));
productInfoDto.setCategoryId(rs.getInt("category_id"));
productInfoDto.setPrice(rs.getInt("price"));
productInfoDto.setImageFilePath(rs.getString("image_file_path"));
productInfoDto.setImageFileName(rs.getString("image_file_name"));
productInfoDto.setReleaseDate(rs.getDate("release_date"));
productInfoDto.setReleaseCompany(rs.getString("release_company"));
productInfoDto.setStatus(rs.getInt("status"));
productInfoDto.setRegistDate(rs.getDate("regist_date"));
productInfoDto.setUpdateDate(rs.getDate("update_date"));
productInfoDtoList.add(productInfoDto);
}
}catch(SQLException e){
e.printStackTrace();
}
finally{
try{
con.close();
}catch(SQLException e){
e.printStackTrace();
}
}
return productInfoDtoList;
}
public ProductInfoDTO getProductInfo(int productId){
DBConnector db=new DBConnector();
Connection con=db.getConnection();
ProductInfoDTO productInfoDTO=new ProductInfoDTO();
//product_idを指定する
String sql="select * from product_info where product_id=?";
try{
PreparedStatement ps=con.prepareStatement(sql);
ps.setInt(1, productId);
ResultSet rs=ps.executeQuery();
while(rs.next()){
productInfoDTO.setId(rs.getInt("id"));
productInfoDTO.setProductId(rs.getInt("product_id"));
productInfoDTO.setProductName(rs.getString("product_name"));
productInfoDTO.setProductNameKana(rs.getString("product_name_kana"));
productInfoDTO.setProductDescription(rs.getString("product_description"));
productInfoDTO.setCategoryId(rs.getInt("category_id"));
productInfoDTO.setPrice(rs.getInt("price"));
productInfoDTO.setImageFilePath(rs.getString("image_file_path"));
productInfoDTO.setImageFileName(rs.getString("image_file_name"));
productInfoDTO.setReleaseDate(rs.getDate("release_date"));
productInfoDTO.setReleaseCompany(rs.getString("release_company"));
productInfoDTO.setStatus(rs.getInt("status"));
productInfoDTO.setUpdateDate(rs.getDate("regist_date"));
productInfoDTO.setUpdateDate(rs.getDate("update_date"));
}
}catch(SQLException e){
e.printStackTrace();
}
finally{
try{
con.close();
}catch(SQLException e){
e.printStackTrace();
}
}
return productInfoDTO;
}
public List<ProductInfoDTO> getProductInfoListByCategoryId(int categoryId,int productId,int limitOffset,int limitRowCount){
DBConnector db=new DBConnector();
Connection con=db.getConnection();
List<ProductInfoDTO> productInfoDtoList=new ArrayList<ProductInfoDTO>();
//not in と <> は同じ意味
//rand() はランダム
//limit [開始位置] , [取得する行数]
//詳細画面でproduct_idが異なる商品を3商品ランダムで出力に使用
String sql="select * from product_info where category_id=? and product_id <> ? order by rand() limit ?,?";
try{
PreparedStatement ps=con.prepareStatement(sql);
ps.setInt(1,categoryId);
ps.setInt(2, productId);
ps.setInt(3,limitOffset);
ps.setInt(4, limitRowCount);
ResultSet rs=ps.executeQuery();
while(rs.next()){
ProductInfoDTO productInfoDTO=new ProductInfoDTO();
productInfoDTO.setId(rs.getInt("id"));
productInfoDTO.setProductId(rs.getInt("product_id"));
productInfoDTO.setProductName(rs.getString("product_name"));
productInfoDTO.setProductNameKana(rs.getString("product_name_kana"));
productInfoDTO.setProductDescription(rs.getString("product_description"));
productInfoDTO.setCategoryId(rs.getInt("category_id"));
productInfoDTO.setPrice(rs.getInt("price"));
productInfoDTO.setImageFilePath(rs.getString("image_file_path"));
productInfoDTO.setImageFileName(rs.getString("image_file_name"));
productInfoDTO.setReleaseDate(rs.getDate("release_date"));
productInfoDTO.setReleaseCompany(rs.getString("release_company"));
productInfoDTO.setStatus(rs.getInt("status"));
productInfoDTO.setUpdateDate(rs.getDate("regist_date"));
productInfoDTO.setUpdateDate(rs.getDate("update_date"));
productInfoDtoList.add(productInfoDTO);
}
}catch(SQLException e){
e.printStackTrace();
}
finally{
try{
con.close();
}catch(SQLException e){
e.printStackTrace();
}
}
return productInfoDtoList;
}
public List<ProductInfoDTO> getProductInfoListAll(String[] keywordsList){
DBConnector db=new DBConnector();
Connection con=db.getConnection();
List<ProductInfoDTO> productInfoDtoList=new ArrayList<ProductInfoDTO>();
String sql="select * from product_info where";
boolean initializeFlag=true;
for(String keyword : keywordsList){
if(initializeFlag){
//検索ワードの最初を指定
sql += " (product_name like '%" + keyword + "%' or product_name_kana like '%" + keyword + "%')";
initializeFlag=false;
}else{
//検索ワードの2つ目以降を指定
sql += " or (product_name like '%" + keyword + "%' or product_name_kana like '%" + keyword + "%')";
}
}
try{
PreparedStatement ps=con.prepareStatement(sql);
ResultSet rs=ps.executeQuery();
while(rs.next()){
ProductInfoDTO productInfoDTO=new ProductInfoDTO();
productInfoDTO.setId(rs.getInt("id"));
productInfoDTO.setProductId(rs.getInt("product_id"));
productInfoDTO.setProductName(rs.getString("product_name"));
productInfoDTO.setProductNameKana(rs.getString("product_name_kana"));
productInfoDTO.setProductDescription(rs.getString("product_description"));
productInfoDTO.setCategoryId(rs.getInt("category_id"));
productInfoDTO.setPrice(rs.getInt("price"));
productInfoDTO.setImageFilePath(rs.getString("image_file_path"));
productInfoDTO.setImageFileName(rs.getString("image_file_name"));
productInfoDTO.setReleaseDate(rs.getDate("release_date"));
productInfoDTO.setReleaseCompany(rs.getString("release_company"));
productInfoDTO.setStatus(rs.getInt("status"));
productInfoDTO.setUpdateDate(rs.getDate("regist_date"));
productInfoDTO.setUpdateDate(rs.getDate("update_date"));
productInfoDtoList.add(productInfoDTO);
}
}catch(SQLException e){
e.printStackTrace();
}
finally{
try{
con.close();
}catch(SQLException e){
e.printStackTrace();
}
}
return productInfoDtoList;
}
public List<ProductInfoDTO> getProductInfoListByKeywords(String[] keywordsList,String categoryId){
DBConnector db=new DBConnector();
Connection con=db.getConnection();
List<ProductInfoDTO> productInfoDtoList=new ArrayList<ProductInfoDTO>();
String sql="select * from product_info where";
boolean initializeFlag=true;
for(String keyword : keywordsList){
if(initializeFlag){
//カテゴリーIDの指定と検索ワードの最初を指定
sql += " category_id=" + categoryId + " and ((product_name like '%" + keyword + "%' or product_name_kana like '%" + keyword + "%')";
initializeFlag=false;
}else{
//検索ワードの2つ目以降を指定
sql += " or (product_name like '%" + keyword + "%' or product_name_kana like '%" + keyword + "%')";
}
}
sql += ")";
try {
PreparedStatement ps=con.prepareStatement(sql);
ResultSet rs=ps.executeQuery();
while (rs.next()) {
ProductInfoDTO productInfoDTO=new ProductInfoDTO();
productInfoDTO.setId(rs.getInt("id"));
productInfoDTO.setProductId(rs.getInt("product_id"));
productInfoDTO.setProductName(rs.getString("product_name"));
productInfoDTO.setProductNameKana(rs.getString("product_name_kana"));
productInfoDTO.setProductDescription(rs.getString("product_description"));
productInfoDTO.setCategoryId(rs.getInt("category_id"));
productInfoDTO.setPrice(rs.getInt("price"));
productInfoDTO.setImageFilePath(rs.getString("image_file_path"));
productInfoDTO.setImageFileName(rs.getString("image_file_name"));
productInfoDTO.setReleaseDate(rs.getDate("release_date"));
productInfoDTO.setReleaseCompany(rs.getString("release_company"));
productInfoDTO.setStatus(rs.getInt("status"));
productInfoDTO.setUpdateDate(rs.getDate("regist_date"));
productInfoDTO.setUpdateDate(rs.getDate("update_date"));
productInfoDtoList.add(productInfoDTO);
}
}catch(SQLException e){
e.printStackTrace();
}
finally{
try{
con.close();
}catch(SQLException e){
e.printStackTrace();
}
}
return productInfoDtoList;
}
}