Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.33 KB | None | 0 0
  1. package buyme;
  2.  
  3. import java.sql.*;
  4.  
  5. public class Item {
  6. // allows you to search items while filtering by category, subcategory and
  7. // sorting by name, price, and date (TESTED)
  8. public ResultSet search(String item_query, String category, String subcategory, int sortNameOption,
  9. int sortPriceOption, int sortDateOption) throws SQLException {
  10. Connection con = null;
  11. PreparedStatement ps = null;
  12. ResultSet rs = null;
  13. try {
  14. String selectSQL = "SELECT * FROM buyme.Auction WHERE item_name LIKE ? AND category_name LIKE ? AND subcategory_name LIKE ?";
  15. con = DriverManager.getConnection("jdbc:mysql://cs336.c7mvfesixgy7.us-east-2.rds.amazonaws.com:3306/buyme",
  16. "cs336", "thisisareallysecurepassword551");
  17.  
  18. if (sortNameOption != 0 || sortPriceOption != 0 || sortDateOption != 0) {
  19. selectSQL += " ORDER BY";
  20. }
  21.  
  22. if (sortNameOption > 0) {
  23. selectSQL += " item_name";
  24. } else if (sortNameOption < 0) {
  25. selectSQL += " item_name DESC";
  26. }
  27.  
  28. if (sortPriceOption > 0) {
  29. selectSQL += " buy_at_price";
  30. } else if (sortPriceOption < 0) {
  31. selectSQL += " buy_at_price DESC";
  32. }
  33.  
  34. if (sortNameOption > 0) {
  35. selectSQL += " start_date";
  36. } else if (sortNameOption < 0) {
  37. selectSQL += " start_date DESC";
  38. }
  39. ps = con.prepareStatement(selectSQL);
  40. ps.setString(1, "%" + item_query + "%");
  41. ps.setString(2, "%" + category + "%");
  42. ps.setString(3, "%" + subcategory + "%");
  43. rs = ps.executeQuery();
  44. return rs;
  45. } catch (SQLException se) {
  46. throw se;
  47. } catch (Exception e) {
  48. throw e;
  49. } finally {
  50. try {
  51. rs.close();
  52. } catch (Exception e) {
  53. /* ignored */ }
  54. try {
  55. ps.close();
  56. } catch (Exception e) {
  57. /* ignored */ }
  58. try {
  59. con.close();
  60. } catch (Exception e) {
  61. /* ignored */ }
  62. }
  63. }
  64.  
  65. // retrieves all items in a category (TESTED)
  66. public ResultSet searchByCategory(String category) throws SQLException {
  67. String selectSQL = "SELECT * FROM buyme.Auction WHERE category_name = ?";
  68. Connection con = null;
  69. PreparedStatement ps = null;
  70. ResultSet rs = null;
  71. try {
  72. con = DriverManager.getConnection(
  73. "jdbc:mysql://cs336.c7mvfesixgy7.us-east-2.rds.amazonaws.com:3306/buyme", "cs336",
  74. "thisisareallysecurepassword551");
  75. ps = con.prepareStatement(selectSQL);
  76. ps.setString(1, category);
  77. rs = ps.executeQuery();
  78. return rs;
  79. } catch (SQLException se) {
  80. throw se;
  81. } catch (Exception e) {
  82. throw e;
  83. } finally {
  84. try {
  85. rs.close();
  86. } catch (Exception e) {
  87. /* ignored */ }
  88. try {
  89. ps.close();
  90. } catch (Exception e) {
  91. /* ignored */ }
  92. try {
  93. con.close();
  94. } catch (Exception e) {
  95. /* ignored */ }
  96. }
  97. }
  98.  
  99. // retrieves all items in a subcategory (TESTED)
  100. public ResultSet searchBySubcategory(String subcategory) throws SQLException {
  101. String selectSQL = "SELECT * FROM buyme.Auction WHERE subcategory_name = ?";
  102. Connection con = null;
  103. PreparedStatement ps = null;
  104. ResultSet rs = null;
  105. try {
  106. con = DriverManager.getConnection(
  107. "jdbc:mysql://cs336.c7mvfesixgy7.us-east-2.rds.amazonaws.com:3306/buyme", "cs336",
  108. "thisisareallysecurepassword551");
  109. ps = con.prepareStatement(selectSQL);
  110. ps.setString(1, subcategory);
  111. rs = ps.executeQuery();
  112. return rs;
  113. } catch (SQLException se) {
  114. throw se;
  115. } catch (Exception e) {
  116. throw e;
  117. } finally {
  118. try {
  119. rs.close();
  120. } catch (Exception e) {
  121. /* ignored */ }
  122. try {
  123. ps.close();
  124. } catch (Exception e) {
  125. /* ignored */ }
  126. try {
  127. con.close();
  128. } catch (Exception e) {
  129. /* ignored */ }
  130. }
  131. }
  132.  
  133. // retrieves a single item in database by id (TESTED)
  134. public ResultSet getByID(int itemID) throws SQLException {
  135. String selectSQL = "SELECT * FROM buyme.Auction WHERE auction_id = ?";
  136. Connection con = null;
  137. PreparedStatement ps = null;
  138. ResultSet rs = null;
  139. try {
  140. con = DriverManager.getConnection(
  141. "jdbc:mysql://cs336.c7mvfesixgy7.us-east-2.rds.amazonaws.com:3306/buyme", "cs336",
  142. "thisisareallysecurepassword551");
  143. ps = con.prepareStatement(selectSQL);
  144. ps.setInt(1, itemID);
  145. rs = ps.executeQuery();
  146. return rs;
  147. } catch (SQLException se) {
  148. throw se;
  149. } catch (Exception e) {
  150. throw e;
  151. } finally {
  152. try {
  153. rs.close();
  154. } catch (Exception e) {
  155. /* ignored */ }
  156. try {
  157. ps.close();
  158. } catch (Exception e) {
  159. /* ignored */ }
  160. try {
  161. con.close();
  162. } catch (Exception e) {
  163. /* ignored */ }
  164. }
  165. }
  166.  
  167. // creates an item(auction) in database (TESTED)
  168. public void create(String itemName, String seller, double buyAtPrice, double increment, String itemDescription,
  169. int userID, String subcategoryName, String image, Timestamp endDate, String categoryName)
  170. throws SQLException {
  171. String insertSQL = "INSERT INTO buyme.Auction"
  172. + "(item_name, current_bid, seller, buy_at_price, increment, auction_id,"
  173. + "item_description, user_id, subcategory_name, image, end_date, start_date, category_name)"
  174. + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
  175. Connection con = null;
  176. PreparedStatement ps = null;
  177. try {
  178. con = DriverManager.getConnection(
  179. "jdbc:mysql://cs336.c7mvfesixgy7.us-east-2.rds.amazonaws.com:3306/buyme", "cs336",
  180. "thisisareallysecurepassword551");
  181. ps = con.prepareStatement(insertSQL);
  182. ps.setString(1, itemName);
  183. ps.setDouble(2, buyAtPrice);
  184. ps.setString(3, seller);
  185. ps.setDouble(4, buyAtPrice);
  186. ps.setDouble(5, increment);
  187. ps.setInt(6, 0);
  188. ps.setString(7, itemDescription);
  189. ps.setInt(8, userID);
  190. ps.setString(9, subcategoryName);
  191. ps.setString(10, image);
  192. ps.setTimestamp(11, endDate);
  193. ps.setTimestamp(12, new Timestamp(System.currentTimeMillis()));
  194. ps.setString(13, categoryName);
  195. ps.executeUpdate();
  196. } catch (SQLException se) {
  197. throw se;
  198. } catch (Exception e) {
  199. throw e;
  200. } finally {
  201. try {
  202. ps.close();
  203. } catch (Exception e) {
  204. /* ignored */ }
  205. try {
  206. con.close();
  207. } catch (Exception e) {
  208. /* ignored */ }
  209. }
  210. }
  211.  
  212. // deletes an item by id
  213. public void deleteByID(int itemID) throws SQLException {
  214. String deleteSQL = "DELETE FROM buyme.Auction WHERE auction_id = ?";
  215. Connection con = null;
  216. PreparedStatement ps = null;
  217. try {
  218. con = DriverManager.getConnection(
  219. "jdbc:mysql://cs336.c7mvfesixgy7.us-east-2.rds.amazonaws.com:3306/buyme", "cs336",
  220. "thisisareallysecurepassword551");
  221. ps = con.prepareStatement(deleteSQL);
  222. ps.setInt(1, itemID);
  223. ps.executeUpdate();
  224. } catch (SQLException se) {
  225. throw se;
  226. } catch (Exception e) {
  227. throw e;
  228. } finally {
  229. try {
  230. ps.close();
  231. } catch (Exception e) {
  232. /* ignored */ }
  233. try {
  234. con.close();
  235. } catch (Exception e) {
  236. /* ignored */ }
  237. }
  238. }
  239.  
  240. public static void main(String[] args) {
  241. // TODO Auto-generated method stub
  242. Item item = new Item();
  243. try {
  244. // debugging
  245.  
  246. // inserting an item
  247. String itemName = "Test Item";
  248. String seller = "a";
  249. double buyAtPrice = 10.00;
  250. double increment = 1.00;
  251. String itemDescription = "This is a test item.";
  252. int userID = 1;
  253. String subcategoryName = "Phones";
  254. String image = "https://img.letgo.com/images/a9/c5/6e/fa/a9c56efa672d4ccd4ac5c1e4395b544d.jpeg?impolicy=img_600";
  255. Timestamp endDate = new Timestamp(System.currentTimeMillis() + 1000000000);
  256. String categoryName = "Electronics";
  257. item.create(itemName, seller, buyAtPrice, increment, itemDescription, userID, subcategoryName, image,
  258. endDate, categoryName);
  259.  
  260. // searching for items
  261. // ResultSet rs = item.search("", "Electronics", "Phones", 0, 0, 0);
  262. // ResultSet rs = item.searchByCategory("Electronics");
  263. // ResultSet rs = item.searchBySubcategory("Phones");
  264. // ResultSet rs = item.getByID(1);
  265. //
  266. // ResultSetMetaData rsmd = rs.getMetaData();
  267. // while(rs.next()) {
  268. // for (int i = 1; i <= rsmd.getColumnCount(); i++) {
  269. // if (i > 1) System.out.print(", ");
  270. // String columnValue = rs.getString(i);
  271. // System.out.print(columnValue + " " + rsmd.getColumnName(i));
  272. // }
  273. // System.out.println();
  274. // }
  275.  
  276. // deleting an item
  277. // int auctionID = 1;
  278. // item.deleteByID(auctionID);
  279.  
  280. } catch (SQLException se) {
  281. se.printStackTrace();
  282. } catch (Exception e) {
  283. e.printStackTrace();
  284. }
  285. }
  286.  
  287. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement