-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReviewDAO.java
More file actions
94 lines (84 loc) · 2.48 KB
/
Copy pathReviewDAO.java
File metadata and controls
94 lines (84 loc) · 2.48 KB
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Model;
import data.ReviewDTO;
import Utils.DBUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author thien
*/
public class ReviewDAO {
public List<ReviewDTO> listReview(int id) throws SQLException {
Connection conn = null;
PreparedStatement stm = null;
ResultSet rs = null;
List<ReviewDTO> tempList = new ArrayList<>();
ReviewDTO pro = null;
String sql = "";
try {
sql = "SELECT * FROM tblReview where product_id = ? \n";
conn = DBUtils.getConnection();
stm = conn.prepareStatement(sql);
stm.setInt(1, id);
rs = stm.executeQuery();
while (rs.next()) {
tempList.add(new ReviewDTO(rs.getInt("id"), rs.getInt("product_id"), rs.getInt("rating"), rs.getString("comment")));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null) {
rs.close();
}
if (stm != null) {
stm.close();
}
if (conn != null) {
conn.close();
}
}
return tempList;
}
public int rating(int id) {
double result = 0;
List<ReviewDTO> tempList = new ArrayList<>();
int count = 0;
try {
tempList = listReview(id);
for (ReviewDTO reviewDTO : tempList) {
count++;
result = result + reviewDTO.getRating();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
return (int) Math.floor(result / count);
}
}
public int Stock(int id) {
int result = 0;
List<ReviewDTO> tempList = new ArrayList<>();
try {
tempList = listReview(id);
result = tempList.size();
} catch (Exception e) {
e.printStackTrace();
} finally {
return result ;
}
}
public static void main(String[] args) {
ReviewDAO dao = new ReviewDAO();
int count = dao.Stock(3);
System.out.println(count);
}
}