Advertisement
Guest User

Untitled

a guest
Mar 16th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. package be.pxl.jdbc;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10.  
  11. public class BeerDao {
  12.  
  13. private String url;
  14. private String user;
  15. private String password;
  16.  
  17. public BeerDao() {
  18.  
  19. }
  20.  
  21. public BeerDao(String url, String user, String password) {
  22. this.url = url;
  23. this.user = user;
  24. this.password = password;
  25. }
  26.  
  27. public String getUrl() {
  28. return url;
  29. }
  30.  
  31. public void setUrl(String url) {
  32. this.url = url;
  33. }
  34.  
  35. public String getUser() {
  36. return user;
  37. }
  38.  
  39. public void setUser(String user) {
  40. this.user = user;
  41. }
  42.  
  43. public String getPassword() {
  44. return password;
  45. }
  46.  
  47. public void setPassword(String password) {
  48. this.password = password;
  49. }
  50.  
  51. public Beer getBeerById(int id) throws BeerException {
  52. try (Connection con = getConnection();
  53. PreparedStatement stmt = con.prepareStatement(
  54. "SELECT * FROM Beers WHERE Id = ?")) {
  55. stmt.setInt(1, id);
  56. try (ResultSet rs = stmt.executeQuery()) {
  57. if (rs.next()) {
  58. Beer beer = new Beer();
  59. beer.setId(id);
  60. beer.setName(rs.getString("Name"));
  61. beer.setPrice(rs.getFloat("Price"));
  62. beer.setAlcohol(rs.getFloat("Alcohol"));
  63. beer.setStock(rs.getInt("Stock"));
  64. return beer;
  65. }
  66. else {
  67. return null;
  68. }
  69. }
  70.  
  71. }
  72. catch (SQLException e) {
  73. throw new BeerException(e);
  74. }
  75. }
  76.  
  77. public void updateBeer(Beer beer) throws BeerException {
  78. try (Connection con = getConnection();
  79. PreparedStatement stmt = con.prepareStatement("UPDATE Beers SET Name = ?, Price = ?,"
  80. + "Alcohol = ?, Stock = ? WHERE Id = ?")) {
  81. stmt.setString(1, beer.getName());
  82. stmt.setFloat(2, beer.getPrice());
  83. stmt.setFloat(3, beer.getAlcohol());
  84. stmt.setInt(4, beer.getStock());
  85. stmt.setInt(5, beer.getId());
  86. stmt.executeUpdate();
  87. }
  88. catch (Exception e) {
  89. throw new BeerException(e);
  90. }
  91. }
  92.  
  93. public List<Beer> getBeerByAlcohol(float alcohol) throws BeerException {
  94. try (Connection con = getConnection();
  95. PreparedStatement stmt = con.prepareStatement(
  96. "SELECT * FROM Beers WHERE Alcohol = ?")) {
  97. stmt.setFloat(1, alcohol);
  98. try (ResultSet rs = stmt.executeQuery()) {
  99. if (rs.next()) {
  100. List beers = new ArrayList<Beer>();
  101. Beer beer = new Beer();
  102. beer.setId(rs.getInt("Id"));
  103. beer.setName(rs.getString("Name"));
  104. beer.setPrice(rs.getFloat("Price"));
  105. beer.setAlcohol(alcohol);
  106. beer.setStock(rs.getInt("Stock"));
  107. beers.add(beer);
  108. return beers;
  109. }
  110. else {
  111. return null;
  112. }
  113. }
  114.  
  115. }
  116. catch (SQLException e) {
  117. throw new BeerException(e);
  118. }
  119. }
  120.  
  121. public List<Beer> getBeerByName(String name) throws BeerException {
  122. try (Connection con = getConnection();
  123. PreparedStatement stmt = con.prepareStatement(
  124. "SELECT * FROM Beers WHERE Name = ?")) {
  125. stmt.setString(1, name);
  126. try (ResultSet rs = stmt.executeQuery()) {
  127. if (rs.next()) {
  128. List beers = new ArrayList<Beer>();
  129. Beer beer = new Beer();
  130. beer.setId(rs.getInt("Id"));
  131. beer.setName(name);
  132. beer.setPrice(rs.getFloat("Price"));
  133. beer.setAlcohol(rs.getFloat("Alcohol"));
  134. beer.setStock(rs.getInt("Stock"));
  135. beers.add(beer);
  136. return beers;
  137. }
  138. else {
  139. return null;
  140. }
  141. }
  142.  
  143. }
  144. catch (SQLException e) {
  145. throw new BeerException(e);
  146. }
  147. }
  148.  
  149. //private helper methods
  150. private Connection getConnection() throws SQLException {
  151. return DriverManager.getConnection(url,user,password);
  152. }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement