Skip to content
This repository was archived by the owner on Jun 9, 2024. It is now read-only.

Commit 5ecffe2

Browse files
committed
Move Loan Report to stored proc
1 parent b4dd5e4 commit 5ecffe2

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

src/main/java/EJB/LoanOracleBean.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.mongodb.client.MongoDatabase;
77
import jakarta.ejb.EJB;
88
import jakarta.ejb.Stateless;
9+
import oracle.jdbc.OracleTypes;
910
import org.bson.Document;
1011
import org.bson.types.ObjectId;
1112

@@ -154,25 +155,21 @@ public ArrayList<loanModel> getActiveLoans() {
154155
}
155156

156157
public ArrayList<loanModel> getLoanReportForCustomer(int customerId, Date startDate, Date endDate) {
157-
String query = "SELECT l.LOAN_ID, l.BOOK_ID, l.USER_ID, l.RETURN_BY, l.RETURNED_ON, l.RETURNED, " +
158-
"u.FIRST_NAME, u.LAST_NAME, u.EMAIL, b.TITLE, b.ISBN, b.PAGES, " +
159-
"a.FIRST_NAME AS AUTHOR_FIRST_NAME, a.LAST_NAME AS AUTHOR_LAST_NAME " +
160-
"FROM loans l " +
161-
"INNER JOIN library_users u ON l.user_id = u.user_id " +
162-
"INNER JOIN books b ON b.book_id = l.book_id " +
163-
"INNER JOIN authors a ON b.author_id = a.author_id " +
164-
"WHERE l.USER_ID = ? AND l.RETURN_BY >= ? AND l.RETURN_BY <= ?";
158+
String query = "{CALL GET_LOAN_REPORT_FOR_CUSTOMER(?, ?, ?, ?)}";
165159

166160
ArrayList<loanModel> loansList = new ArrayList<>();
167161

168162
try (Connection con = oracleClientProviderBean.getOracleClient();
169-
PreparedStatement pstmt = con.prepareStatement(query)) {
163+
CallableStatement cstmt = con.prepareCall(query)) {
164+
165+
cstmt.setInt(1, customerId);
166+
cstmt.setObject(2, new java.sql.Date(startDate.getTime()));
167+
cstmt.setObject(3, new java.sql.Date(endDate.getTime()));
168+
cstmt.registerOutParameter(4, OracleTypes.CURSOR);
170169

171-
pstmt.setInt(1, customerId);
172-
pstmt.setObject(2, new java.sql.Date(startDate.getTime()));
173-
pstmt.setObject(3, new java.sql.Date(endDate.getTime()));
170+
cstmt.execute();
174171

175-
try (ResultSet rs = pstmt.executeQuery()) {
172+
try (ResultSet rs = (ResultSet) cstmt.getObject(4)) {
176173
while (rs.next()) {
177174
loanModel loan = new loanModel();
178175
loan.setLoanId(rs.getLong("LOAN_ID"));

0 commit comments

Comments
 (0)