11package EJB ;
22
3- import Objects .loanModel ;
3+ import Objects .bookModel ;
44import jakarta .ejb .EJB ;
55import jakarta .ejb .Stateless ;
66
77import java .sql .*;
88import java .util .ArrayList ;
9- import java .util .Calendar ;
10- import java .util .Date ;
119
1210@ Stateless (name = "BookOracleEJB" )
1311public class BookOracleBean {
1412 @ EJB
1513 OracleClientProviderBean oracleClientProviderBean ;
1614
17- public void markAsBorrowed (int bookId ) {
18- Connection con = oracleClientProviderBean .getOracleClient ();
19- String insertLoan = "UPDATE books SET on_loan = 1 WHERE book_id = ?" ;
20-
21- try (PreparedStatement preparedStatement = con .prepareStatement (insertLoan )) {
22- preparedStatement .setInt (1 , bookId );
23- preparedStatement .executeUpdate ();
24- } catch (SQLException e ) {
25- // Handle any potential exceptions here
26- e .printStackTrace ();
15+ public ArrayList <bookModel > getBooks () {
16+ String query = "SELECT b.BOOK_ID BOOK_ID," +
17+ "b.TITLE TITLE," +
18+ "b.ISBN ISBN," +
19+ "b.PAGES PAGES," +
20+ "b.CREATED CREATED," +
21+ "b.ON_LOAN ON_LOAN," +
22+ "a.FIRST_NAME AUTHOR_FIRST_NAME," +
23+ "a.LAST_NAME AUTHOR_LAST_NAME FROM books b " +
24+ "INNER JOIN authors a ON b.author_id = a.author_id" ;
25+ ArrayList books_list = new ArrayList ();
26+ Statement stmt = null ;
27+
28+ try {
29+ Connection con = oracleClientProviderBean .getOracleClient ();
30+ stmt = con .createStatement ();
31+ ResultSet rs = stmt .executeQuery (query );
32+
33+ while (rs .next ()) {
34+ bookModel book = new bookModel ();
35+ book .setBookId (rs .getLong ("BOOK_ID" ));
36+ book .setOnLoan (rs .getBoolean ("ON_LOAN" ));
37+ book .setBookTitle (rs .getString ("TITLE" ));
38+ book .setBookIsbn (rs .getLong ("ISBN" ));
39+ book .setBookPages (rs .getInt ("PAGES" ));
40+ book .setCreated (rs .getDate ("CREATED" ));
41+ book .setAuthorFirstName (rs .getString ("AUTHOR_FIRST_NAME" ));
42+ book .setAuthorLastName (rs .getString ("AUTHOR_LAST_NAME" ));
43+ books_list .add (book );
44+ }
45+
46+ stmt .close ();
47+ return books_list ;
48+ } catch (SQLException e ) {
49+ e .printStackTrace ();
50+ }
51+
52+ return null ;
2753 }
54+
55+ public ArrayList <bookModel > getAvailableBooks () {
56+ String query = "SELECT b.BOOK_ID BOOK_ID," +
57+ "b.TITLE TITLE," +
58+ "b.ISBN ISBN," +
59+ "b.PAGES PAGES," +
60+ "b.CREATED CREATED," +
61+ "b.ON_LOAN ON_LOAN," +
62+ "a.FIRST_NAME AUTHOR_FIRST_NAME," +
63+ "a.LAST_NAME AUTHOR_LAST_NAME FROM books b " +
64+ "INNER JOIN authors a ON b.author_id = a.author_id " +
65+ "WHERE b.ON_LOAN = 0" ;
66+
67+ ArrayList books_list = new ArrayList ();
68+ Statement stmt = null ;
69+
70+ try {
71+ Connection con = oracleClientProviderBean .getOracleClient ();
72+ stmt = con .createStatement ();
73+ ResultSet rs = stmt .executeQuery (query );
74+
75+ while (rs .next ()) {
76+ bookModel book = new bookModel ();
77+ book .setBookId (rs .getLong ("BOOK_ID" ));
78+ book .setOnLoan (rs .getBoolean ("ON_LOAN" ));
79+ book .setBookTitle (rs .getString ("TITLE" ));
80+ book .setBookIsbn (rs .getLong ("ISBN" ));
81+ book .setBookPages (rs .getInt ("PAGES" ));
82+ book .setCreated (rs .getDate ("CREATED" ));
83+ book .setAuthorFirstName (rs .getString ("AUTHOR_FIRST_NAME" ));
84+ book .setAuthorLastName (rs .getString ("AUTHOR_LAST_NAME" ));
85+ books_list .add (book );
86+ }
87+
88+ stmt .close ();
89+ return books_list ;
90+ } catch (SQLException e ) {
91+ e .printStackTrace ();
92+ }
93+
94+ return null ;
2895 }
2996
3097 public void markAsReturned (int bookId ) {
@@ -39,4 +106,17 @@ public void markAsReturned(int bookId) {
39106 e .printStackTrace ();
40107 }
41108 }
109+
110+ public void markAsBorrowed (int bookId ) {
111+ Connection con = oracleClientProviderBean .getOracleClient ();
112+ String insertLoan = "UPDATE books SET on_loan = 1 WHERE book_id = ?" ;
113+
114+ try (PreparedStatement preparedStatement = con .prepareStatement (insertLoan )) {
115+ preparedStatement .setInt (1 , bookId );
116+ preparedStatement .executeUpdate ();
117+ } catch (SQLException e ) {
118+ // Handle any potential exceptions here
119+ e .printStackTrace ();
120+ }
121+ }
42122}
0 commit comments