Advertisement
Guest User

dao

a guest
May 14th, 2016
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 37.41 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package onlinelibrary.model;
  7.  
  8. import onlinelibrary.model.bean.Book;
  9. import onlinelibrary.model.bean.LentBook;
  10. import onlinelibrary.model.bean.User;
  11.  
  12.  
  13. import java.sql.Connection;
  14. import java.sql.DriverManager;
  15. import java.sql.PreparedStatement;
  16. import java.sql.ResultSet;
  17. import java.sql.SQLException;
  18. import java.sql.Statement;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. import java.util.logging.Level;
  22. import java.util.logging.Logger;
  23.  
  24. /**
  25. *
  26. * @author Horvath
  27. */
  28. public class Dao {
  29. /**
  30. * Sqlite Database file path
  31. */
  32.  
  33.  
  34.  
  35. private static final String DB_FILE_ABSOLUTE_PATH = System.getenv("OnlineLibraryDB");
  36. private static final String DB_CONNECTION_STRING = "jdbc:sqlite:" + DB_FILE_ABSOLUTE_PATH;
  37.  
  38. // SQLs
  39. private static final String SELECT_ADMIN_USER_SQL = "SELECT * FROM users WHERE email = ? AND password = ? AND is_admin = 1";
  40. private static final String SELECT_ALL_USER_SQL = "SELECT * FROM users WHERE is_admin = 0";
  41. private static final String SELECT_ALL_BOOK_SQL = "SELECT * FROM books";
  42. private static final String SELECT_ALL_LENTBOOK_SQL = "SELECT * FROM lentbooks";
  43. private static final String SELECT_USER_BY_ID = "SELECT * FROM users WHERE id = ?";
  44. private static final String SELECT_BOOK_BY_ID = "SELECT * FROM books WHERE id = ?";
  45. private static final String SELECT_BOOK_BY_NAME = "SELECT * FROM books WHERE name like '%?%' ";
  46. private static final String SELECT_BOOK_BY_AUTHOR = "SELECT * FROM books WHERE author like '%?%' ";
  47. private static final String SELECT_BOOK_BY_PUBLISHDATE = "SELECT * FROM books WHERE publishdate like '%?%' ";
  48. private static final String SELECT_BOOK_BY_IMEI = "SELECT * FROM books WHERE imei = ?";
  49. private static final String SELECT_FREE_BOOKS = "SELECT * FROM books WHERE piece > outpiece";
  50. private static final String SELECT_LENTBOOKS_BY_ID = "SELECT * FROM lentbooks WHERE id = ?";
  51. private static final String SELECT_USERNAME_AND_PASSWORD = "SELECT * FROM users WHERE email=? AND password=?";
  52. private static final String SELECT_USERNAME_AND_ADMIN = "SELECT * FROM users WHERE email=? AND is_admin=1";
  53. private static final String SELECT_EMAIL = "SELECT * FROM users WHERE email=?";
  54.  
  55.  
  56.  
  57.  
  58. // Insert SQLs
  59. private static final String INSERT_READER_SQL =
  60. "INSERT INTO users " +
  61. "(name, password, email, address, is_admin) " +
  62. "VALUES (?, ?, ?, ?, 0)"
  63. ;
  64.  
  65. private static final String INSERT_BOOK_SQL =
  66. "INSERT INTO books " +
  67. "(name, author, imei, publishercity, publishername, imageurl, piece, outpiece, publishdate, tag) " +
  68. "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
  69. ;
  70.  
  71. private static final String INSERT_LENTBOOK_SQL =
  72. "INSERT INTO lentbooks " +
  73. "(userid, bookid, startdate, deadlinedate) " +
  74. "VALUES (?, ?, ?, ?)"
  75. ;
  76.  
  77. // Update SQLs
  78. private static final String UPDATE_USER_SQL =
  79. "UPDATE users " +
  80. "SET name = ?, password = ?, email = ?, address = ? " +
  81. "WHERE id = ?"
  82. ;
  83.  
  84. private static final String UPDATE_BOOK_SQL =
  85. "UPDATE books " +
  86. "SET name = ?, author = ?, imei = ?, publishercity = ?, publishername = ?, piece = ?, outpiece = ?, publishdate = ? " +
  87. "WHERE id = ?"
  88. ;
  89.  
  90. // Delete Sql command
  91. private static final String DELETE_LENTBOOK = "DELETE FROM lentbooks WHERE id = ?";
  92.  
  93. List<User> users = new ArrayList<User>();
  94. List<Book> books = new ArrayList<Book>();
  95. List<LentBook> lentbooks = new ArrayList<LentBook>();
  96. List<Book> searchbook = new ArrayList<Book>();
  97.  
  98.  
  99. public Dao() {
  100. try {
  101. Class.forName("org.sqlite.JDBC");
  102. } catch (ClassNotFoundException ex) {
  103. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  104. }
  105. }
  106.  
  107. public List<Book> getSearchResult(int keres, String keyword){
  108.  
  109. List<Book> books = new ArrayList<Book>();
  110. switch(keres){
  111. case 1: books = getBookByName(keyword);
  112. break;
  113. case 2: books = getBookByAuthor(keyword);;
  114. break;
  115. case 3: books = getBookByPublishdate(keyword);
  116. break;
  117. }
  118. return books;
  119. }
  120.  
  121. public List<User> getUsers() {
  122. Connection conn = null;
  123. Statement st = null;
  124. ResultSet rs = null;
  125. users.clear();
  126.  
  127. try {
  128. conn = DriverManager.getConnection(DB_CONNECTION_STRING);
  129.  
  130. st = conn.createStatement();
  131. rs = st.executeQuery(SELECT_ALL_USER_SQL);
  132.  
  133. users = getUsersFromResultSet(rs);
  134. } catch (SQLException ex) {
  135. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  136. } finally {
  137. try {
  138. if (rs != null ) {
  139. rs.close ();
  140. }
  141.  
  142. } catch (SQLException ex) {
  143. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  144. }
  145.  
  146. try {
  147. if (st != null) {
  148. st.close();
  149. }
  150. } catch (SQLException ex) {
  151. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  152. }
  153.  
  154. try {
  155. if (conn != null) {
  156. conn.close();
  157. }
  158. } catch (SQLException ex) {
  159. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  160. }
  161. }
  162.  
  163. return users;
  164. }
  165.  
  166. public User getUserById(int id) {
  167. Connection conn = null;
  168. PreparedStatement pSt = null;
  169. ResultSet rs = null;
  170. User user = null;
  171.  
  172. try {
  173. conn = DriverManager.getConnection(DB_CONNECTION_STRING);
  174.  
  175. pSt = conn.prepareStatement(SELECT_USER_BY_ID);
  176. pSt.setInt(1, id);
  177. rs = pSt.executeQuery();
  178.  
  179. user = new User();
  180.  
  181. while (rs.next()) {
  182. user.setId(rs.getInt("id"));
  183. user.setName(rs.getString("name"));
  184. user.setEmail(rs.getString("email"));
  185. user.setPassword(rs.getString("password"));
  186. user.setAddress(rs.getString("address"));
  187. user.setAdmin(rs.getInt("is_admin") == 1);
  188. }
  189.  
  190. } catch (SQLException ex) {
  191. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  192. } finally {
  193. try {
  194. if (rs != null ) {
  195. rs.close ();
  196. }
  197.  
  198. } catch (SQLException ex) {
  199. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  200. }
  201.  
  202. try {
  203. if (pSt != null) {
  204. pSt.close();
  205. }
  206. } catch (SQLException ex) {
  207. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  208. }
  209.  
  210. try {
  211. if (conn != null) {
  212. conn.close();
  213. }
  214. } catch (SQLException ex) {
  215. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  216. }
  217. }
  218.  
  219. return user;
  220. }
  221.  
  222.  
  223. private List<User> getUsersFromResultSet(ResultSet rs) throws SQLException {
  224. List<User> rvUserList = new ArrayList<>();
  225.  
  226. while (rs.next()) {
  227.  
  228. User user = new User();
  229.  
  230. user.setId(rs.getInt("id"));
  231. user.setName(rs.getString("name"));
  232. user.setEmail(rs.getString("email"));
  233. user.setPassword(rs.getString("password"));
  234. user.setAddress(rs.getString("address"));
  235. user.setAdmin(rs.getInt("is_admin") == 1);
  236.  
  237. rvUserList.add(user);
  238. }
  239.  
  240. return rvUserList;
  241. }
  242.  
  243. public List<Book> getBooks() {
  244. Connection conn = null;
  245. Statement st = null;
  246. ResultSet rs = null;
  247.  
  248. books.clear();
  249.  
  250. try {
  251. conn = DriverManager.getConnection(DB_CONNECTION_STRING);
  252.  
  253. st = conn.createStatement();
  254. rs = st.executeQuery(SELECT_ALL_BOOK_SQL);
  255.  
  256. books = getBooksFromResultSet(rs);
  257. } catch (SQLException ex) {
  258. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  259. } finally {
  260. try {
  261. if (rs != null ) {
  262. rs.close ();
  263. }
  264.  
  265. } catch (SQLException ex) {
  266. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  267. }
  268.  
  269. try {
  270. if (st != null) {
  271. st.close();
  272. }
  273. } catch (SQLException ex) {
  274. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  275. }
  276.  
  277. try {
  278. if (conn != null) {
  279. conn.close();
  280. }
  281. } catch (SQLException ex) {
  282. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  283. }
  284. }
  285.  
  286. return books;
  287. }
  288.  
  289. public Book getBookById(int id) {
  290. Connection conn = null;
  291. PreparedStatement pSt = null;
  292. ResultSet rs = null;
  293. Book book = null;
  294.  
  295. try {
  296. conn = DriverManager.getConnection(DB_CONNECTION_STRING);
  297.  
  298. pSt = conn.prepareStatement(SELECT_BOOK_BY_ID);
  299. pSt.setInt(1, id);
  300. rs = pSt.executeQuery();
  301.  
  302. book = new Book();
  303.  
  304. while (rs.next()) {
  305.  
  306. book.setId(rs.getInt("id"));
  307. book.setName(rs.getString("name"));
  308. book.setAuthor(rs.getString("author"));
  309. book.setImei(rs.getString("imei"));
  310. book.setPublishercity(rs.getString("publishercity"));
  311. book.setPublishername(rs.getString("publishername"));
  312. book.setImageurl(rs.getString("imageurl"));
  313. book.setPiece(rs.getInt("piece"));
  314. book.setOutpiece(rs.getInt("outpiece"));
  315. book.setPublishdate(rs.getInt("publishdate"));
  316. book.setTag(rs.getInt("tag"));
  317. }
  318.  
  319. } catch (SQLException ex) {
  320. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  321. } finally {
  322. try {
  323. if (rs != null ) {
  324. rs.close ();
  325. }
  326.  
  327. } catch (SQLException ex) {
  328. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  329. }
  330.  
  331. try {
  332. if (pSt != null) {
  333. pSt.close();
  334. }
  335. } catch (SQLException ex) {
  336. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  337. }
  338.  
  339. try {
  340. if (conn != null) {
  341. conn.close();
  342. }
  343. } catch (SQLException ex) {
  344. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  345. }
  346. }
  347.  
  348. return book;
  349. }
  350.  
  351. public List<Book> getBookByName(String keyword) {
  352. Connection conn = null;
  353. PreparedStatement st = null;
  354. ResultSet rs = null;
  355.  
  356. searchbook.clear();
  357.  
  358. try {
  359. conn = DriverManager.getConnection(DB_CONNECTION_STRING);
  360.  
  361. st = conn.prepareStatement(SELECT_BOOK_BY_NAME);
  362. st.setString(1,keyword);
  363. rs = st.executeQuery();
  364.  
  365.  
  366. searchbook = getBooksFromResultSet(rs);
  367.  
  368. } catch (SQLException ex) {
  369. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  370. } finally {
  371. try {
  372. if (rs != null ) {
  373. rs.close ();
  374. }
  375.  
  376. } catch (SQLException ex) {
  377. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  378. }
  379.  
  380. try {
  381. if (st != null) {
  382. st.close();
  383. }
  384. } catch (SQLException ex) {
  385. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  386. }
  387.  
  388. try {
  389. if (conn != null) {
  390. conn.close();
  391. }
  392. } catch (SQLException ex) {
  393. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  394. }
  395. }
  396.  
  397. return searchbook;
  398. }
  399.  
  400. public List<Book> getBookByAuthor(String keyword) {
  401. Connection conn = null;
  402. PreparedStatement st = null;
  403. ResultSet rs = null;
  404.  
  405. searchbook.clear();
  406.  
  407. try {
  408. conn = DriverManager.getConnection(DB_CONNECTION_STRING);
  409.  
  410. st = conn.prepareStatement(SELECT_BOOK_BY_AUTHOR);
  411. st.setString(1,keyword);
  412. rs = st.executeQuery();
  413.  
  414. searchbook = getBooksFromResultSet(rs);
  415.  
  416. } catch (SQLException ex) {
  417. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  418. } finally {
  419. try {
  420. if (rs != null ) {
  421. rs.close ();
  422. }
  423.  
  424. } catch (SQLException ex) {
  425. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  426. }
  427.  
  428. try {
  429. if (st != null) {
  430. st.close();
  431. }
  432. } catch (SQLException ex) {
  433. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  434. }
  435.  
  436. try {
  437. if (conn != null) {
  438. conn.close();
  439. }
  440. } catch (SQLException ex) {
  441. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  442. }
  443. }
  444.  
  445. return searchbook;
  446. }
  447.  
  448. public List<Book> getBookByPublishdate(String keyword) {
  449. Connection conn = null;
  450. PreparedStatement st = null;
  451. ResultSet rs = null;
  452.  
  453. searchbook.clear();
  454.  
  455. try {
  456. conn = DriverManager.getConnection(DB_CONNECTION_STRING);
  457.  
  458. st = conn.prepareStatement(SELECT_BOOK_BY_PUBLISHDATE);
  459. st.setString(1,keyword);
  460. rs = st.executeQuery();
  461.  
  462. searchbook = getBooksFromResultSet(rs);
  463.  
  464. } catch (SQLException ex) {
  465. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  466. } finally {
  467. try {
  468. if (rs != null ) {
  469. rs.close ();
  470. }
  471.  
  472. } catch (SQLException ex) {
  473. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  474. }
  475.  
  476. try {
  477. if (st != null) {
  478. st.close();
  479. }
  480. } catch (SQLException ex) {
  481. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  482. }
  483.  
  484. try {
  485. if (conn != null) {
  486. conn.close();
  487. }
  488. } catch (SQLException ex) {
  489. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  490. }
  491. }
  492.  
  493. return searchbook;
  494. }
  495.  
  496. private List<Book> getBooksFromResultSet(ResultSet rs) throws SQLException {
  497. List<Book> rvBookList = new ArrayList<>();
  498.  
  499. while (rs.next()) {
  500.  
  501. Book book = new Book();
  502.  
  503. book.setId(rs.getInt("id"));
  504. book.setName(rs.getString("name"));
  505. book.setAuthor(rs.getString("author"));
  506. book.setImei(rs.getString("imei"));
  507. book.setPublishercity(rs.getString("publishercity"));
  508. book.setPublishername(rs.getString("publishername"));
  509. book.setImageurl(rs.getString("imageurl"));
  510. book.setPiece(rs.getInt("piece"));
  511. book.setOutpiece(rs.getInt("outpiece"));
  512. book.setPublishdate(rs.getInt("publishdate"));
  513. book.setTag(rs.getInt("tag"));
  514.  
  515. rvBookList.add(book);
  516. }
  517.  
  518. return rvBookList;
  519. }
  520.  
  521. public List<LentBook> getLentBooks() {
  522. Connection conn = null;
  523. Statement st = null;
  524. ResultSet rs = null;
  525. lentbooks.clear();
  526.  
  527. try {
  528. conn = DriverManager.getConnection(DB_CONNECTION_STRING);
  529.  
  530. st = conn.createStatement();
  531. rs = st.executeQuery(SELECT_ALL_LENTBOOK_SQL);
  532.  
  533. lentbooks = getLentbooksFromResultSet(rs);
  534. } catch (SQLException ex) {
  535. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  536. } finally {
  537. try {
  538. if (rs != null ) {
  539. rs.close ();
  540. }
  541.  
  542. } catch (SQLException ex) {
  543. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  544. }
  545.  
  546. try {
  547. if (st != null) {
  548. st.close();
  549. }
  550. } catch (SQLException ex) {
  551. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  552. }
  553.  
  554. try {
  555. if (conn != null) {
  556. conn.close();
  557. }
  558. } catch (SQLException ex) {
  559. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  560. }
  561. }
  562.  
  563. return lentbooks;
  564. }
  565.  
  566. private List<LentBook> getLentbooksFromResultSet(ResultSet rs) throws SQLException {
  567. List<LentBook> rvLentBookList = new ArrayList<>();
  568.  
  569. while (rs.next()) {
  570.  
  571. LentBook lentbook = new LentBook();
  572.  
  573. lentbook.setId(rs.getInt("id"));
  574. lentbook.setUser(this.getUserById(rs.getInt("userid")));
  575. lentbook.setBook(this.getBookById(rs.getInt("bookid")));
  576. lentbook.setStartdate(rs.getString("startdate"));
  577. lentbook.setDeadlinedate(rs.getString("deadlinedate"));
  578.  
  579. rvLentBookList.add(lentbook);
  580. }
  581.  
  582. return rvLentBookList;
  583. }
  584.  
  585. public boolean addNewUser(User user) {
  586. boolean rvSucceeded = false;
  587. Connection conn = null;
  588. PreparedStatement pst = null;
  589.  
  590. try {
  591. conn = DriverManager.getConnection(DB_CONNECTION_STRING);
  592.  
  593. pst = conn.prepareStatement(INSERT_READER_SQL);
  594. int index = 1;
  595. pst.setString(index++, user.getName());
  596. pst.setString(index++, user.getPassword());
  597. pst.setString(index++, user.getEmail());
  598. pst.setString(index++, user.getAddress());
  599.  
  600. int rowsAffected = pst.executeUpdate();
  601.  
  602. if (rowsAffected == 1) {
  603. rvSucceeded = true;
  604. }
  605.  
  606. } catch (SQLException ex) {
  607. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  608. } finally {
  609. try {
  610. if (pst != null) {
  611. pst.close();
  612. }
  613. } catch (SQLException ex) {
  614. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  615. }
  616.  
  617. try {
  618. if (conn != null) {
  619. conn.close();
  620. }
  621. } catch (SQLException ex) {
  622. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  623. }
  624. }
  625.  
  626. return rvSucceeded;
  627. }
  628.  
  629. public boolean updateUser(User user) {
  630. boolean rvSucceeded = false;
  631. Connection conn = null;
  632. PreparedStatement pst = null;
  633.  
  634. try {
  635. conn = DriverManager.getConnection(DB_CONNECTION_STRING);
  636.  
  637. pst = conn.prepareStatement(UPDATE_USER_SQL);
  638. int index = 1;
  639. pst.setString(index++, user.getName());
  640. pst.setString(index++, user.getPassword());
  641. pst.setString(index++, user.getEmail());
  642. pst.setString(index++, user.getAddress());
  643. pst.setInt(index++, user.getId());
  644.  
  645. int rowsAffected = pst.executeUpdate();
  646.  
  647. // csak akkor sikeres, ha valóban volt érintett sor
  648. if (rowsAffected == 1) {
  649. rvSucceeded = true;
  650. }
  651.  
  652. } catch (SQLException ex) {
  653. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  654. } finally {
  655. try {
  656. if (pst != null) {
  657. pst.close();
  658. }
  659. } catch (SQLException ex) {
  660. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  661. }
  662.  
  663. try {
  664. if (conn != null) {
  665. conn.close();
  666. }
  667. } catch (SQLException ex) {
  668. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  669. }
  670. }
  671.  
  672. return rvSucceeded;
  673. }
  674.  
  675. public boolean updateBook(Book book) {
  676. boolean rvSucceeded = false;
  677. Connection conn = null;
  678. PreparedStatement pst = null;
  679.  
  680. try {
  681. conn = DriverManager.getConnection(DB_CONNECTION_STRING);
  682.  
  683. pst = conn.prepareStatement(UPDATE_BOOK_SQL);
  684. int index = 1;
  685. pst.setString(index++, book.getName());
  686. pst.setString(index++, book.getAuthor());
  687. pst.setString(index++, book.getImei());
  688. pst.setString(index++, book.getPublishercity());
  689. pst.setString(index++, book.getPublishername());
  690. pst.setInt(index++, book.getPiece());
  691. pst.setInt(index++, book.getOutpiece());
  692. pst.setInt(index++, book.getPublishdate());
  693. pst.setInt(index++, book.getId());
  694.  
  695. int rowsAffected = pst.executeUpdate();
  696.  
  697. // csak akkor sikeres, ha valóban volt érintett sor
  698. if (rowsAffected == 1) {
  699. rvSucceeded = true;
  700. }
  701.  
  702. } catch (SQLException ex) {
  703. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  704. } finally {
  705. try {
  706. if (pst != null) {
  707. pst.close();
  708. }
  709. } catch (SQLException ex) {
  710. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  711. }
  712.  
  713. try {
  714. if (conn != null) {
  715. conn.close();
  716. }
  717. } catch (SQLException ex) {
  718. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  719. }
  720. }
  721.  
  722. return rvSucceeded;
  723. }
  724.  
  725. public boolean isBookUpdated(String imei) {
  726. boolean rvUploaded = false;
  727. Connection conn = null;
  728. PreparedStatement pSt = null;
  729. ResultSet rs = null;
  730.  
  731. try {
  732. conn = DriverManager.getConnection(DB_CONNECTION_STRING);
  733.  
  734. pSt = conn.prepareStatement(SELECT_BOOK_BY_IMEI);
  735. pSt.setString(1, imei);
  736. rs = pSt.executeQuery();
  737.  
  738. if (rs.next()) {
  739. rvUploaded = true;
  740. }
  741.  
  742. } catch (SQLException ex) {
  743. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  744. } finally {
  745. try {
  746. if (rs != null ) {
  747. rs.close ();
  748. }
  749.  
  750. } catch (SQLException ex) {
  751. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  752. }
  753.  
  754. try {
  755. if (pSt != null) {
  756. pSt.close();
  757. }
  758. } catch (SQLException ex) {
  759. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  760. }
  761.  
  762. try {
  763. if (conn != null) {
  764. conn.close();
  765. }
  766. } catch (SQLException ex) {
  767. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  768. }
  769. }
  770.  
  771. return rvUploaded;
  772. }
  773.  
  774. public boolean addNewBook(Book book) {
  775. boolean rvSucceeded = false;
  776. Connection conn = null;
  777. PreparedStatement pst = null;
  778.  
  779. try {
  780. conn = DriverManager.getConnection(DB_CONNECTION_STRING);
  781.  
  782. pst = conn.prepareStatement(INSERT_BOOK_SQL);
  783. int index = 1;
  784. pst.setString(index++, book.getName());
  785. pst.setString(index++, book.getAuthor());
  786. pst.setString(index++, book.getImei());
  787. pst.setString(index++, book.getPublishercity());
  788. pst.setString(index++, book.getPublishername());
  789. pst.setString(index++, book.getImageurl());
  790. pst.setInt(index++, book.getPiece());
  791. pst.setInt(index++, book.getOutpiece());
  792. pst.setInt(index++, book.getPublishdate());
  793. pst.setInt(index++, book.getTag());
  794.  
  795. int rowsAffected = pst.executeUpdate();
  796.  
  797. // csak akkor sikeres, ha valóban volt érintett sor
  798. if (rowsAffected == 1) {
  799. rvSucceeded = true;
  800. }
  801.  
  802. } catch (SQLException ex) {
  803. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  804. } finally {
  805. try {
  806. if (pst != null) {
  807. pst.close();
  808. }
  809. } catch (SQLException ex) {
  810. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  811. }
  812.  
  813. try {
  814. if (conn != null) {
  815. conn.close();
  816. }
  817. } catch (SQLException ex) {
  818. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  819. }
  820. }
  821.  
  822. return rvSucceeded;
  823. }
  824.  
  825. public List<Book> getFreeBooks() {
  826. Connection conn = null;
  827. Statement st = null;
  828. ResultSet rs = null;
  829.  
  830. books.clear();
  831.  
  832. try {
  833. conn = DriverManager.getConnection(DB_CONNECTION_STRING);
  834.  
  835. st = conn.createStatement();
  836. rs = st.executeQuery(SELECT_FREE_BOOKS);
  837.  
  838. books = getBooksFromResultSet(rs);
  839. } catch (SQLException ex) {
  840. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  841. } finally {
  842. try {
  843. if (rs != null ) {
  844. rs.close ();
  845. }
  846.  
  847. } catch (SQLException ex) {
  848. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  849. }
  850.  
  851. try {
  852. if (st != null) {
  853. st.close();
  854. }
  855. } catch (SQLException ex) {
  856. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  857. }
  858.  
  859. try {
  860. if (conn != null) {
  861. conn.close();
  862. }
  863. } catch (SQLException ex) {
  864. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  865. }
  866. }
  867.  
  868. return books;
  869. }
  870.  
  871. public boolean addLentBook(LentBook lentbook) {
  872. boolean rvSucceeded = false;
  873. Connection conn = null;
  874. PreparedStatement pst = null;
  875.  
  876. try {
  877. conn = DriverManager.getConnection(DB_CONNECTION_STRING);
  878.  
  879. pst = conn.prepareStatement(INSERT_LENTBOOK_SQL);
  880. int index = 1;
  881. pst.setInt(index++, lentbook.getUser().getId());
  882. pst.setInt(index++, lentbook.getBook().getId());
  883. pst.setString(index++, lentbook.getStartdate());
  884. pst.setString(index++, lentbook.getDeadlinedate());
  885.  
  886. int rowsAffected = pst.executeUpdate();
  887.  
  888. Book book = lentbook.getBook();
  889. book.incOutPiece();
  890.  
  891. if (rowsAffected == 1 && this.updateBook(book)) {
  892. rvSucceeded = true;
  893. }
  894.  
  895. } catch (SQLException ex) {
  896. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  897. } finally {
  898. try {
  899. if (pst != null) {
  900. pst.close();
  901. }
  902. } catch (SQLException ex) {
  903. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  904. }
  905.  
  906. try {
  907. if (conn != null) {
  908. conn.close();
  909. }
  910. } catch (SQLException ex) {
  911. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  912. }
  913. }
  914.  
  915. return rvSucceeded;
  916. }
  917.  
  918. public boolean deleteLentBook (LentBook lentbook) {
  919. boolean rvSucceeded = false;
  920. Connection conn = null;
  921. PreparedStatement pst = null;
  922.  
  923. try {
  924. conn = DriverManager.getConnection(DB_CONNECTION_STRING);
  925.  
  926. pst = conn.prepareStatement(DELETE_LENTBOOK);
  927. int index = 1;
  928. pst.setInt(index++, lentbook.getId());
  929.  
  930. int rowsAffected = pst.executeUpdate();
  931.  
  932. Book book = lentbook.getBook();
  933. book.decOutPiece();
  934.  
  935. if (rowsAffected == 1 && this.updateBook(book)) {
  936. rvSucceeded = true;
  937. }
  938.  
  939. } catch (SQLException ex) {
  940. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  941. } finally {
  942. try {
  943. if (pst != null) {
  944. pst.close();
  945. }
  946. } catch (SQLException ex) {
  947. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  948. }
  949.  
  950. try {
  951. if (conn != null) {
  952. conn.close();
  953. }
  954. } catch (SQLException ex) {
  955. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  956. }
  957. }
  958. return rvSucceeded;
  959. }
  960.  
  961. public LentBook getLentBookById(int lentbookId) {
  962. Connection conn = null;
  963. PreparedStatement pSt = null;
  964. ResultSet rs = null;
  965. LentBook lentbook = null;
  966.  
  967. try {
  968. conn = DriverManager.getConnection(DB_CONNECTION_STRING);
  969.  
  970. pSt = conn.prepareStatement(SELECT_LENTBOOKS_BY_ID);
  971. int index = 1;
  972. pSt.setInt(index++, lentbookId);
  973. rs = pSt.executeQuery();
  974.  
  975. lentbook = new LentBook();
  976.  
  977. while (rs.next()) {
  978. lentbook.setId(rs.getInt("id"));
  979. lentbook.setUser(this.getUserById(rs.getInt("userid")));
  980. lentbook.setBook(this.getBookById(rs.getInt("bookid")));
  981. lentbook.setStartdate(rs.getString("startdate"));
  982. lentbook.setDeadlinedate(rs.getString("deadlinedate"));
  983. }
  984.  
  985. } catch (SQLException ex) {
  986. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  987. } finally {
  988. try {
  989. if (rs != null ) {
  990. rs.close ();
  991. }
  992.  
  993. } catch (SQLException ex) {
  994. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  995. }
  996.  
  997. try {
  998. if (pSt != null) {
  999. pSt.close();
  1000. }
  1001. } catch (SQLException ex) {
  1002. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  1003. }
  1004.  
  1005. try {
  1006. if (conn != null) {
  1007. conn.close();
  1008. }
  1009. } catch (SQLException ex) {
  1010. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  1011. }
  1012. }
  1013.  
  1014. return lentbook;
  1015. }
  1016.  
  1017. public boolean checkLogin(String email, String password){
  1018. Connection conn = null;
  1019. PreparedStatement pS = null;
  1020. ResultSet rs = null;
  1021. boolean value = false;
  1022.  
  1023. try {
  1024. conn = DriverManager.getConnection(DB_CONNECTION_STRING);
  1025. pS = conn.prepareStatement(SELECT_USERNAME_AND_PASSWORD);
  1026.  
  1027. pS.setString(1, email);
  1028. pS.setString(2, password);
  1029.  
  1030. rs = pS.executeQuery();
  1031.  
  1032. if (rs.next()) {
  1033. value = true;
  1034. }
  1035.  
  1036. } catch (SQLException ex) {
  1037. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  1038. } finally {
  1039. try {
  1040. if (rs != null ) {
  1041. rs.close ();
  1042. }
  1043.  
  1044. } catch (SQLException ex) {
  1045. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  1046. }
  1047.  
  1048. try {
  1049. if (pS != null) {
  1050. pS.close();
  1051. }
  1052. } catch (SQLException ex) {
  1053. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  1054. }
  1055.  
  1056. try {
  1057. if (conn != null) {
  1058. conn.close();
  1059. }
  1060. } catch (SQLException ex) {
  1061. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  1062. }
  1063. }
  1064.  
  1065. return value;
  1066.  
  1067. }
  1068.  
  1069. public boolean checkEmail(String email){
  1070. Connection conn = null;
  1071. PreparedStatement pS = null;
  1072. ResultSet rs = null;
  1073. boolean value = false;
  1074.  
  1075. try {
  1076. conn = DriverManager.getConnection(DB_CONNECTION_STRING);
  1077. pS = conn.prepareStatement(SELECT_EMAIL);
  1078.  
  1079. pS.setString(1, email);
  1080.  
  1081. rs = pS.executeQuery();
  1082.  
  1083. if (rs.next()) {
  1084. value = true;
  1085. }
  1086.  
  1087. } catch (SQLException ex) {
  1088. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  1089. } finally {
  1090. try {
  1091. if (rs != null ) {
  1092. rs.close ();
  1093. }
  1094.  
  1095. } catch (SQLException ex) {
  1096. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  1097. }
  1098.  
  1099. try {
  1100. if (pS != null) {
  1101. pS.close();
  1102. }
  1103. } catch (SQLException ex) {
  1104. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  1105. }
  1106.  
  1107. try {
  1108. if (conn != null) {
  1109. conn.close();
  1110. }
  1111. } catch (SQLException ex) {
  1112. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  1113. }
  1114. }
  1115.  
  1116. return value;
  1117.  
  1118. }
  1119.  
  1120. public boolean isAdmin(String email){
  1121. Connection conn = null;
  1122. PreparedStatement pS = null;
  1123. ResultSet rs = null;
  1124. boolean value = false;
  1125.  
  1126. try {
  1127. conn = DriverManager.getConnection(DB_CONNECTION_STRING);
  1128. pS = conn.prepareStatement(SELECT_USERNAME_AND_ADMIN);
  1129.  
  1130. pS.setString(1, email);
  1131.  
  1132. rs = pS.executeQuery();
  1133.  
  1134. if (rs.next()) {
  1135. value = true;
  1136. }
  1137.  
  1138. } catch (SQLException ex) {
  1139. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  1140. } finally {
  1141. try {
  1142. if (rs != null ) {
  1143. rs.close ();
  1144. }
  1145.  
  1146. } catch (SQLException ex) {
  1147. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  1148. }
  1149.  
  1150. try {
  1151. if (pS != null) {
  1152. pS.close();
  1153. }
  1154. } catch (SQLException ex) {
  1155. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  1156. }
  1157.  
  1158. try {
  1159. if (conn != null) {
  1160. conn.close();
  1161. }
  1162. } catch (SQLException ex) {
  1163. Logger.getLogger(Dao.class.getName()).log(Level.SEVERE, null, ex);
  1164. }
  1165. }
  1166.  
  1167. return value;
  1168.  
  1169. }
  1170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement