Advertisement
Guest User

Database

a guest
Apr 24th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. // ALLT TILL PRODUCTIONPANE
  2.  
  3. public List<String> fetchProducts() {
  4.  
  5. List<String> products = new LinkedList<String>();
  6. String query = "SELECT product_name FROM cookies";
  7. try (PreparedStatement ps = conn.prepareStatement(query)) {
  8. ResultSet rs = ps.executeQuery();
  9. while (rs.next()) {
  10. products.add(rs.getString("product_name"));
  11. }
  12. } catch (SQLException e) {
  13. e.printStackTrace();
  14. }
  15. return products;
  16. }
  17.  
  18. public boolean isBlocked(String Product) {
  19.  
  20. boolean isBlocked = false;
  21. String query = "SELECT product_name FROM pallets\n" + "WHERE blocked LIKE ('Y')\n";
  22. try (PreparedStatement ps = conn.prepareStatement(query)) {
  23. ResultSet rs = ps.executeQuery();
  24. while (rs.next()) {
  25. if (rs.getString("product_name").equals(Product)) {
  26. isBlocked = true;
  27. break;
  28.  
  29. }
  30. }
  31. } catch (SQLException e) {
  32. e.printStackTrace();
  33. }
  34. return isBlocked;
  35. }
  36.  
  37. public int maxPalletId() {
  38. int max = 0;
  39. String query = "SELECT MAX(pallet_id) FROM pallets";
  40.  
  41. try (PreparedStatement ps = conn.prepareStatement(query)) {
  42. ResultSet rs = ps.executeQuery();
  43. max = rs.getInt("pallet_id");
  44. }
  45. catch (SQLException e) {
  46. e.printStackTrace();
  47. }
  48.  
  49. return max;
  50. }
  51.  
  52.  
  53. public void updateDb(String date, String product) {
  54. String query = "INSERT INTO pallets(pallet_id, produced_when, product_name)\n" + "VALUES (?, ?, ?)\n";
  55.  
  56. try (PreparedStatement ps = conn.prepareStatement(query)) {
  57.  
  58. ps.setInt(1, 1+maxPalletId());
  59. ps.setString(2, date);
  60. ps.setString(3, product);
  61. ps.executeUpdate();
  62. } catch (SQLException e) {
  63. e.printStackTrace();
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement