Advertisement
Guest User

Untitled

a guest
Nov 20th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. package demo;
  2.  
  3. import java.sql.*;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import java.util.Random;
  7.  
  8. /**
  9. * Created by chitboon on 10/23/15.
  10. */
  11. public class BookDAO {
  12.  
  13. Connection con;
  14. // Database configuration
  15. public static String url = "jdbc:mysql://localhost/test";
  16. public static String dbdriver = "com.mysql.jdbc.Driver";
  17. public static String username = "root";
  18. public static String password = "mysql";
  19. private Random random = new Random();
  20.  
  21. // constructor to load the jdbc driver, exception will be thrown if database driver is not found
  22. public BookDAO() {
  23. try {
  24. Class.forName(dbdriver);
  25. con = DriverManager.getConnection(url, username, password);
  26. } catch (Exception e) {
  27. e.printStackTrace();
  28. }
  29. }
  30. // this is to make sure that connection is not null when you used it
  31. public void getConnection() throws SQLException {
  32. if (con == null) con = DriverManager.getConnection(url, username, password);
  33. }
  34.  
  35. // Retrieve book details based on bookId, null is returned if book is not found
  36. public BookDetail getBookDetails(String bookId) {
  37. String sql = "select * from books where id = ?";
  38. BookDetail book = null;
  39. try {
  40. getConnection();
  41. PreparedStatement pstmt = con.prepareStatement(sql);
  42. pstmt.setString(1,bookId);
  43. ResultSet rs = pstmt.executeQuery();
  44. if (rs != null && rs.next()) {
  45. book = new BookDetail();
  46. book.setBookId(rs.getString("id"));
  47. book.setDescription(rs.getString("description"));
  48. book.setFirstName(rs.getString("first_name"));
  49. book.setInventory(rs.getInt("inventory"));
  50. book.setOnSale(rs.getBoolean("onSale"));
  51. book.setPrice(rs.getFloat("price"));
  52. book.setSurname(rs.getString("surname"));
  53. book.setTitle(rs.getString("title"));
  54. book.setYear(rs.getInt("calendar_year"));
  55. }
  56. pstmt.close();
  57. } catch (SQLException e) {
  58. e.printStackTrace();
  59. }
  60. return book;
  61. }
  62.  
  63. // Get a random book from database
  64. public BookDetail getBook() {
  65. List<BookDetail> list = getAllBook();
  66. int i = random.nextInt(list.size());
  67. return list.get(i);
  68. }
  69. // Retrieve all the books from database
  70. public List<BookDetail> getAllBook() {
  71. String sql = "select * from books";
  72. ArrayList<BookDetail> list = new ArrayList<BookDetail>();
  73. try {
  74. getConnection();
  75. PreparedStatement pstmt = con.prepareStatement(sql);
  76. ResultSet rs = pstmt.executeQuery();
  77. while (rs != null && rs.next()) {
  78. BookDetail book = new BookDetail();
  79. book.setBookId(rs.getString("id"));
  80. book.setDescription(rs.getString("description"));
  81. book.setFirstName(rs.getString("first_name"));
  82. book.setInventory(rs.getInt("inventory"));
  83. book.setOnSale(rs.getBoolean("onSale"));
  84. book.setPrice(rs.getFloat("price"));
  85. book.setSurname(rs.getString("surname"));
  86. book.setTitle(rs.getString("title"));
  87. book.setYear(rs.getInt("calendar_year"));
  88. list.add(book);
  89. }
  90. pstmt.close();
  91. } catch (SQLException e) {
  92. e.printStackTrace();
  93. }
  94. return list;
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement