Guest User

Untitled

a guest
Dec 11th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.02 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 model;
  7.  
  8. import java.io.IOException;
  9. import java.sql.Connection;
  10. import java.sql.DatabaseMetaData;
  11. import java.sql.DriverManager;
  12. import java.sql.ResultSet;
  13. import java.sql.SQLException;
  14. import java.util.ArrayList;
  15. import java.util.List;
  16. import java.sql.PreparedStatement;
  17. import java.util.logging.Level;
  18. import java.util.logging.Logger;
  19.  
  20. /**
  21. * A mock implementation of the BooksDBInterface interface to demonstrate how to
  22. * use it together with the user interface.
  23. *
  24. * Your implementation should access a real database.
  25. *
  26. * @author [email protected]
  27. */
  28. public class BooksDb implements BooksDbInterface {
  29.  
  30. private final List<Book> books;
  31. private Connection con;
  32.  
  33. public BooksDb() {
  34. books = new ArrayList();
  35. con = null;
  36. }
  37.  
  38. @Override
  39. public boolean connect(String database, String user, String pwd) throws IOException, SQLException {
  40. String server = "jdbc:mysql://localhost:3306/"
  41. + database
  42. + "?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&UseClientEnc=UTF8";
  43.  
  44. con = DriverManager.getConnection(server, user, pwd);
  45.  
  46. return true;
  47. }
  48.  
  49. @Override
  50. public void disconnect() throws IOException, SQLException {
  51. // mock implementation
  52. con.close();
  53. }
  54.  
  55. /**
  56. *
  57. * @param searchTitle
  58. * @return
  59. * @throws IOException
  60. * @throws SQLException
  61. * @throws InvalidISBNException
  62. */
  63. @Override
  64. public ArrayList<Book> searchBooksByTitle(String searchTitle)
  65. throws IOException, SQLException, InvalidISBNException {
  66. PreparedStatement pstmt = null;
  67. ArrayList<Book> result = new ArrayList();
  68. try {
  69. String sql = "SELECT * FROM Book WHERE title LIKE ?";
  70. pstmt = con.prepareStatement(sql);
  71. pstmt.setString(1, "%" + searchTitle + "%");
  72. ResultSet rs = pstmt.executeQuery();
  73. while(rs.next()) {
  74. String isbn = rs.getString("isbn");
  75. String title = rs.getString("title");
  76. Book temp;
  77. temp = new Book(isbn, title);
  78. temp.setYear(rs.getInt("year"));
  79. temp.setRating(rs.getInt("rating"));
  80. temp.setPublisher(rs.getString("publisher"));
  81. temp.setUsername(rs.getString("username"));
  82. getAuthorByISBN(temp, isbn);
  83. getGenreByISBN(temp, isbn);
  84. result.add(temp);
  85. }
  86. } finally {
  87. pstmt.close();
  88. }
  89. return result;
  90. }
  91.  
  92. private void getAuthorByISBN(Book temp, String isbn) throws SQLException{
  93. PreparedStatement pstmt = null;
  94. try {
  95. String sql = "SELECT * FROM Author A, CreatedBy C, Book B WHERE A.creatorID = C.creatorID AND C.isbn = B.isbn AND C.isbn = ?";
  96. pstmt = con.prepareStatement(sql);
  97. pstmt.setString(1,isbn);
  98. ResultSet rs = pstmt.executeQuery();
  99. while(rs.next()) {
  100. temp.addAuthor(new Author(rs.getString("firstName") + " " + rs.getString("lastName"), rs.getDate("dob").toLocalDate(), rs.getInt("creatorID")));
  101. }
  102. } finally {
  103. pstmt.close();
  104. }
  105. }
  106.  
  107. private void getGenreByISBN(Book temp, String isbn) throws SQLException{
  108. PreparedStatement pstmt = null;
  109. try {
  110. String sql = "SELECT * FROM Genre WHERE isbn = ?";
  111. pstmt = con.prepareStatement(sql);
  112. pstmt.setString(1,isbn);
  113. ResultSet rs = pstmt.executeQuery();
  114. while(rs.next()) {
  115. temp.addGenre(rs.getString("genre"));
  116. }
  117. } finally {
  118. pstmt.close();
  119. }
  120. }
  121.  
  122. @Override
  123. public ArrayList<Book> searchBooksByAuthor(String searchAuthor)
  124. throws IOException, SQLException, InvalidISBNException {
  125. PreparedStatement pstmt = null;
  126. ArrayList<Book> result = new ArrayList();
  127. try {
  128. String sql = "SELECT * FROM Book B, CreatedBy C, Author A "
  129. + "WHERE A.creatorID = C.creatorID AND C.isbn = B.isbn AND UPPER(REPLACE(CONCAT_WS('',firstName,lastName), ' ', '')) LIKE ?";
  130. pstmt = con.prepareStatement(sql);
  131. pstmt.setString(1, "%" + searchAuthor + "%");
  132. ResultSet rs = pstmt.executeQuery();
  133. while(rs.next()) {
  134. String isbn = rs.getString("isbn");
  135. String title = rs.getString("title");
  136. Book temp;
  137. temp = new Book(isbn, title);
  138. temp.setYear(rs.getInt("year"));
  139. temp.setRating(rs.getInt("rating"));
  140. temp.setPublisher(rs.getString("publisher"));
  141. temp.setUsername(rs.getString("username"));
  142. getAuthorByISBN(temp, isbn);
  143. getGenreByISBN(temp, isbn);
  144. result.add(temp);
  145. }
  146. } finally {
  147. pstmt.close();
  148. }
  149. return result;
  150. }
  151.  
  152. @Override
  153. public ArrayList<Book> searchBooksByISBN(String searchISBN) throws IOException, SQLException, InvalidISBNException {
  154. ArrayList<Book> result = new ArrayList();
  155. PreparedStatement pstmt = null;
  156. try {
  157. String sql = "SELECT * FROM Book WHERE isbn LIKE ?";
  158. pstmt = con.prepareStatement(sql);
  159. pstmt.setString(1, "%" + searchISBN + "%");
  160. ResultSet rs = pstmt.executeQuery();
  161. while(rs.next()) {
  162. String isbn = rs.getString("isbn");
  163. String title = rs.getString("title");
  164. Book temp;
  165. temp = new Book(isbn, title);
  166. temp.setYear(rs.getInt("year"));
  167. temp.setRating(rs.getInt("rating"));
  168. temp.setPublisher(rs.getString("publisher"));
  169. temp.setUsername(rs.getString("username"));
  170. getAuthorByISBN(temp, isbn);
  171. getGenreByISBN(temp, isbn);
  172. result.add(temp);
  173. }
  174. } finally {
  175. pstmt.close();
  176. }
  177. return result;
  178. }
  179.  
  180. @Override
  181. public void insertNewBook(Book b) throws SQLException {
  182. PreparedStatement pstmt = null;
  183. try {
  184. String sql = "INSERT INTO Book VALUES(?,?,?,?,'0',?,CURDATE())";
  185. con.setAutoCommit(false);
  186. pstmt = con.prepareStatement(sql);
  187. pstmt.setString(1, b.getIsbn());
  188. pstmt.setString(2, b.getTitle());
  189. pstmt.setInt(3, b.getYear());
  190. pstmt.setString(4, b.getPublisher());
  191. pstmt.setString(5, b.getUsername());
  192. pstmt.executeUpdate();
  193.  
  194. for(Author a : b.getAuthors()){
  195. if(a.getName() != null){
  196. sql = "INSERT INTO Author VALUES(?,?,?,?)";
  197. pstmt = con.prepareStatement(sql);
  198. pstmt.setString(1, a.getName().split("\\s+")[0]);
  199. pstmt.setString(2, a.getName().split("\\s+")[1]);
  200. pstmt.setDate(3, java.sql.Date.valueOf(a.getDateOfBirth()));
  201. pstmt.setInt(4, a.getCreatorID());
  202. pstmt.executeUpdate();
  203. }
  204.  
  205. sql = "INSERT INTO CreatedBy VALUES(?,?,?,CURDATE())";
  206. pstmt = con.prepareStatement(sql);
  207. pstmt.setString(2, b.getIsbn());
  208. pstmt.setInt(1, a.getCreatorID());
  209. pstmt.setString(3, b.getUsername());
  210. pstmt.executeUpdate();
  211. }
  212.  
  213. for(String g : b.getGenres()){
  214. sql = "INSERT INTO Genre VALUES(?,?)";
  215. pstmt = con.prepareStatement(sql);
  216. pstmt.setString(1, b.getIsbn());
  217. pstmt.setString(2, g);
  218. pstmt.executeUpdate();
  219. }
  220.  
  221. con.commit();
  222. } catch (SQLException ex) {
  223. con.rollback();
  224. throw ex;
  225. } finally {
  226. pstmt.close();
  227. con.setAutoCommit(true);
  228. }
  229. }
  230.  
  231. @Override
  232. public void updateBookAuthor(Author a, String isbn, String username) throws SQLException{
  233. String sql = "";
  234. PreparedStatement pstmt = null;
  235. try {
  236. con.setAutoCommit(false);
  237. if(a.getName() != null){
  238. sql = "INSERT INTO Author VALUES(?,?,?,?)";
  239. pstmt = con.prepareStatement(sql);
  240. pstmt.setString(1, a.getName().split("\\s+")[0]);
  241. pstmt.setString(2, a.getName().split("\\s+")[1]);
  242. pstmt.setDate(3, java.sql.Date.valueOf(a.getDateOfBirth()));
  243. pstmt.setInt(4, a.getCreatorID());
  244. pstmt.executeUpdate();
  245. }
  246.  
  247. sql = "INSERT INTO CreatedBy VALUES(?,?,?,CURDATE())";
  248. pstmt = con.prepareStatement(sql);
  249. pstmt.setString(2, isbn);
  250. pstmt.setInt(1, a.getCreatorID());
  251. pstmt.setString(3, username);
  252. pstmt.executeUpdate();
  253. con.commit();
  254. } catch (SQLException ex) {
  255. con.rollback();
  256. throw ex;
  257. } finally {
  258. pstmt.close();
  259. con.setAutoCommit(true);
  260. }
  261. }
  262.  
  263. /**
  264. *
  265. * @param searchString
  266. * @param mode
  267. * @throws SQLException
  268. */
  269. @Override
  270. public void removeBooks(String searchString, SearchMode mode) throws SQLException{
  271. PreparedStatement pstmt = null;
  272. try {
  273. String sql = "DELETE FROM Book WHERE isbn = ?";
  274. con.setAutoCommit(false);
  275. pstmt = con.prepareStatement(sql);
  276. ArrayList<String> isbn = getIsbnFromSearch(searchString, mode);
  277. for(String s : isbn){
  278. pstmt.setString(1, s);
  279. pstmt.executeUpdate();
  280. }
  281. con.commit();
  282. } catch (SQLException ex) {
  283. con.rollback();
  284. throw ex;
  285. } finally {
  286. pstmt.close();
  287. con.setAutoCommit(true);
  288. }
  289. }
  290.  
  291. @Override
  292. public boolean checkPermission(String username) throws SQLException, IOException{
  293. PreparedStatement pstmt = null;
  294. try {
  295. connect("LABB1", "root", "basket97");
  296. String user = "";
  297. String sql = "SELECT Insert_priv FROM mysql.user WHERE user = ?";
  298. pstmt = con.prepareStatement(sql);
  299. pstmt.setString(1, username);
  300. ResultSet rs = pstmt.executeQuery();
  301. while(rs.next()){
  302. user = rs.getString("Insert_priv");
  303. System.out.println(user);
  304. }
  305.  
  306. if(user.equals("N")){
  307. return false;
  308. }
  309. return true;
  310. } finally {
  311. pstmt.close();
  312. }
  313. }
  314.  
  315. private ArrayList<String> getIsbnFromSearch(String searchString, SearchMode mode) throws SQLException{
  316. ArrayList<String> isbn = new ArrayList();
  317. PreparedStatement pstmt = null;
  318. try {
  319. switch(mode){
  320. case Title:
  321. String sql = "SELECT isbn FROM Book WHERE title LIKE ?";
  322. pstmt = con.prepareStatement(sql);
  323. pstmt.setString(1, "%" + searchString + "%");
  324. ResultSet rs = pstmt.executeQuery();
  325. while(rs.next()) {
  326. isbn.add(rs.getString("isbn"));
  327. }
  328. break;
  329. case ISBN:
  330. sql = "SELECT isbn FROM Book WHERE isbn LIKE ?";
  331. pstmt = con.prepareStatement(sql);
  332. pstmt.setString(1, "%" + searchString + "%");
  333. rs = pstmt.executeQuery();
  334. while(rs.next()) {
  335. isbn.add(rs.getString("isbn"));
  336. }
  337. break;
  338. case Author:
  339. sql = "SELECT DISTINCT B.isbn FROM Book B, CreatedBy C, Author A "
  340. + "WHERE A.creatorID = C.creatorID AND C.isbn = B.isbn AND (A.firstName LIKE ? OR A.lastName LIKE ?)";
  341. pstmt = con.prepareStatement(sql);
  342. pstmt.setString(1, "%" + searchString + "%");
  343. pstmt.setString(2, "%" + searchString + "%");
  344. rs = pstmt.executeQuery();
  345. while(rs.next()) {
  346. isbn.add(rs.getString("isbn"));
  347. }
  348. break;
  349. }
  350. } finally {
  351. pstmt.close();
  352. }
  353. return isbn;
  354. }
  355.  
  356. @Override
  357. public void addRatingToBook(Book b) throws SQLException {
  358. String sql = null;
  359. PreparedStatement pstmt = null;
  360. try {
  361. con.setAutoCommit(false);
  362. sql = "UPDATE Book SET rating = ? WHERE isbn = ?";
  363. pstmt = con.prepareStatement(sql);
  364. pstmt.setInt(1, b.getRating());
  365. pstmt.setString(2, b.getIsbn());
  366. pstmt.executeUpdate();
  367. con.commit();
  368. } catch (SQLException ex) {
  369. con.rollback();
  370. throw ex;
  371. } finally {
  372. pstmt.close();
  373. con.setAutoCommit(true);
  374. }
  375. }
  376.  
  377. @Override
  378. public void insertUserReview(String user, String isbn, String reviewText) throws SQLException{
  379. PreparedStatement pstmt = null;
  380. try {
  381. con.setAutoCommit(false);
  382. String sql = "INSERT INTO Reviews VALUES(?, ?, ?)";
  383. pstmt = con.prepareStatement(sql);
  384. pstmt.setString(1, user);
  385. pstmt.setString(2, isbn);
  386. pstmt.setString(3, reviewText);
  387. pstmt.executeUpdate();
  388. con.commit();
  389. } catch (SQLException ex) {
  390. con.rollback();
  391. throw ex;
  392. } finally {
  393. pstmt.close();
  394. con.setAutoCommit(true);
  395. }
  396. }
  397.  
  398. @Override
  399. public String getUserReviews(String isbn) throws SQLException{
  400. String reviews = "";
  401. PreparedStatement pstmt = null;
  402. try {
  403. String sql = "SELECT * FROM Reviews WHERE isbn = ?";
  404. pstmt = con.prepareStatement(sql);
  405. pstmt.setString(1, isbn);
  406. ResultSet rs = pstmt.executeQuery();
  407.  
  408. while(rs.next()) {
  409. reviews = reviews.concat(rs.getString("username"));
  410. reviews = reviews.concat(": ");
  411. reviews = reviews.concat(rs.getString("review"));
  412. reviews = reviews.concat("\n");
  413. }
  414. } finally {
  415. pstmt.close();
  416. }
  417. return reviews;
  418.  
  419. }
  420. }
Advertisement
Add Comment
Please, Sign In to add comment