Advertisement
HarrJ

Day 25 Discussion

Jul 20th, 2023
1,075
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.91 KB | None | 0 0
  1. package week4training;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Day25A {
  6.     public static void main(String[] args) {
  7.         Scanner sc = new Scanner(System.in);
  8.         Day25ACodeBehind callQuery = new Day25ACodeBehind();
  9.         //variable ng table
  10.         String prodName,prodCat;
  11.         double prodPrice;
  12.         int pID = 0, proQty;
  13.         String[] prodArray;
  14.         //variable ng java code
  15.         String opt, confirm;
  16.         int rowsAffected = 0;
  17.        
  18.         System.out.println("Options:");
  19.         System.out.println("1 - View all items");
  20.         System.out.println("2 - Search by product name");
  21.         System.out.println("3 - Search by category");
  22.         System.out.println("4 - Add new item");
  23.         System.out.println("5 - Update item by Product ID");
  24.         System.out.println("6 - Remove item by Product ID");
  25.         System.out.print("Type number of action: ");
  26.         opt = sc.nextLine();
  27.        
  28.         switch (opt) {
  29.             case "1":
  30.                 callQuery.viewProdList("");
  31.                 break;
  32.             case "2":
  33.                 System.out.print("Product Name: ");
  34.                 prodName = sc.nextLine();
  35.                 callQuery.viewProdList(prodName);
  36.                 System.out.println("---Search End---");
  37.                 break;
  38.             case "3":
  39.                 System.out.print("Category: ");
  40.                 prodCat = sc.nextLine();
  41.                 callQuery.searchByCategory(prodCat);
  42.                 System.out.println("---Search End---");
  43.                 break;
  44.             case "4":
  45.                 System.out.println("Enter info for new row:");
  46.                 try {
  47.                     System.out.print("Product Name: ");
  48.                     prodName = sc.nextLine();
  49.                     System.out.print("Product Price: ");
  50.                     prodPrice = sc.nextDouble();
  51.                     sc.nextLine();
  52.                     System.out.print("Category: ");
  53.                     prodCat = sc.nextLine();
  54.                     System.out.print("Quantity: ");
  55.                     proQty = sc.nextInt();
  56.                     sc.nextLine();
  57.                     rowsAffected = callQuery.addNewRow(prodName, prodPrice, prodCat, proQty);
  58.                 } catch (Exception e) {
  59.                     System.out.println("invalid input please try again");
  60.                 }
  61.                
  62.                 if (rowsAffected == 1) {
  63.                     System.out.println("Record Complete");
  64.                 } else {
  65.                     System.out.println("Record failed");
  66.                 }                
  67.                 break;
  68.             case "5":
  69.                 callQuery.viewProdList("");
  70.                 try {
  71.                     System.out.print("Enter Product ID of row to be updated: ");
  72.                     pID = sc.nextInt();
  73.                     sc.nextLine();
  74.                     prodArray = callQuery.getSelectedRow(pID);
  75.                     System.out.print("Updated Product Name: ");
  76.                     prodName = sc.nextLine();
  77.                     if (prodName.isEmpty()) {
  78.                         prodName = prodArray[0];
  79.                     }
  80.                     System.out.print("Updated Product Price: ");
  81.                     prodPrice = sc.nextDouble();
  82.                     sc.nextLine();
  83.                     if (prodPrice == 0) {
  84.                         prodPrice = Double.parseDouble(prodArray[1]);
  85.                     }
  86.                     System.out.print("Updated Category: ");
  87.                     prodCat = sc.nextLine();
  88.                     if (prodCat.isEmpty()) {
  89.                         prodCat = prodArray[2];
  90.                     }
  91.                     System.out.print("Updated Quantity: ");
  92.                     proQty = sc.nextInt();
  93.                     sc.nextLine();
  94.                     if (proQty == 0) {
  95.                         proQty = Integer.parseInt(prodArray[3]);
  96.                     }
  97.                     rowsAffected = callQuery.updateRow(prodName, prodPrice, prodCat, proQty, pID);
  98.                 } catch (Exception e) {
  99.                     System.out.println("invalid input please try again");
  100.                 }
  101.                
  102.                 if (rowsAffected == 1) {
  103.                     System.out.println("Update Successful");
  104.                 } else {
  105.                     System.out.println("Update failed");
  106.                 }  
  107.                 break;
  108.             case "6":
  109.                 callQuery.viewProdList("");
  110.                 System.out.print("Enter Product ID of row to be deleted: ");
  111.                 pID = sc.nextInt();
  112.                 sc.nextLine();
  113.                 System.out.println("Are you sure you want to delete the row?(Y/N)");
  114.                 confirm = sc.nextLine();
  115.                 switch (confirm) {
  116.                     case "Y":
  117.                         rowsAffected = callQuery.deleteRowByID(pID);
  118.                         if (rowsAffected == 1) {
  119.                             System.out.println("Row deleted");
  120.                         } else {
  121.                             System.out.println("Action failed");
  122.                         }
  123.                         break;
  124.                 }      
  125.                 break;
  126.         }
  127.     }
  128. }
  129. //------------------------------
  130. package week4training;
  131. import java.sql.*;
  132.  
  133. public class Day25ACodeBehind {
  134.     public int addNewRow(String prodName
  135.             , double prodPrice, String prodCat, int proQty) {
  136.         int rowAffected = 0;
  137.         String address = "jdbc:mysql://localhost:3306/db_mng_b2";
  138.         String userName = "root";
  139.         String passWord = "";
  140.         String sqlQuery = "INSERT INTO tbl_prod_list"
  141.                 + "(fld_prod_name, fld_prod_price, fld_category, fld_quantity) "
  142.                 + "VALUES (?, ?, ?, ?)";
  143.         try {
  144.             Connection conn = DriverManager.getConnection(
  145.                     address,userName,passWord);
  146.             PreparedStatement stmt = conn.prepareStatement(sqlQuery);
  147.            
  148.             stmt.setString(1, prodName);
  149.             stmt.setDouble(2, prodPrice);
  150.             stmt.setString(3, prodCat);
  151.             stmt.setInt(4, proQty);
  152.            
  153.             rowAffected = stmt.executeUpdate();
  154.            
  155.             conn.close();
  156.         } catch (Exception e) {
  157.             System.out.println(e.getMessage());
  158.         }
  159.         return rowAffected;
  160.     }
  161.    
  162.     public void viewProdList(String pName) {
  163.         String address = "jdbc:mysql://localhost:3306/db_mng_b2";
  164.         String userName = "root";
  165.         String passWord = "";
  166.         String sqlQuery = "SELECT fld_plist_id"
  167.                 + ", fld_prod_name"
  168.                 + ", fld_prod_price"
  169.                 + ", fld_category"
  170.                 + ", fld_quantity "
  171.                 + " FROM tbl_prod_list"
  172.                 + " WHERE fld_prod_name LIKE ?";
  173.         String searchParam = "%"+pName+"%";
  174.        
  175.         try {
  176.             Connection conn = DriverManager.getConnection(
  177.                     address,userName,passWord);
  178.             PreparedStatement stmt = conn.prepareStatement(sqlQuery);
  179.            
  180.             stmt.setString(1,searchParam);
  181.            
  182.             ResultSet rs = stmt.executeQuery();
  183.             while (rs.next()) {
  184.                 System.out.println(String.format(
  185.                     "%-4d | %-30s | %8.2f | %-30s | %4d"
  186.                     ,rs.getInt(1)
  187.                     ,rs.getString(2)
  188.                     ,rs.getFloat(3)
  189.                     ,rs.getString(4)
  190.                     ,rs.getInt(5)
  191.                 ));
  192.             }
  193.             conn.close();
  194.         } catch (Exception e) {
  195.             System.out.println(e.getMessage());
  196.         }
  197.     }
  198.    
  199.     public void searchByCategory(String pCategory) {
  200.         String address = "jdbc:mysql://localhost:3306/db_mng_b2";
  201.         String userName = "root";
  202.         String passWord = "";
  203.         String sqlQuery = "SELECT fld_plist_id"
  204.                 + ", fld_prod_name"
  205.                 + ", fld_prod_price"
  206.                 + ", fld_category"
  207.                 + ", fld_quantity "
  208.                 + " FROM tbl_prod_list"
  209.                 + " WHERE fld_category LIKE ?";
  210.         String searchParam = "%"+pCategory+"%";
  211.        
  212.         try {
  213.             Connection conn = DriverManager.getConnection(
  214.                     address,userName,passWord);
  215.             PreparedStatement stmt = conn.prepareStatement(sqlQuery);
  216.            
  217.             stmt.setString(1,searchParam);
  218.            
  219.             ResultSet rs = stmt.executeQuery();
  220.             while (rs.next()) {
  221.                 System.out.println(String.format(
  222.                     "%-4d | %-30s | %8.2f | %-30s | %4d"
  223.                     ,rs.getInt(1)
  224.                     ,rs.getString(2)
  225.                     ,rs.getFloat(3)
  226.                     ,rs.getString(4)
  227.                     ,rs.getInt(5)
  228.                 ));
  229.             }
  230.             conn.close();
  231.         } catch (Exception e) {
  232.             System.out.println(e.getMessage());
  233.         }
  234.     }
  235.    
  236.     public int deleteRowByID(int pID) {
  237.         int rowAffected = 0;
  238.         String address = "jdbc:mysql://localhost:3306/db_mng_b2";
  239.         String userName = "root";
  240.         String passWord = "";
  241.         String sqlQuery = "DELETE FROM tbl_prod_list WHERE fld_plist_id = ?";
  242.        
  243.         try {
  244.             Connection conn = DriverManager.getConnection(
  245.                     address,userName,passWord);
  246.             PreparedStatement stmt = conn.prepareStatement(sqlQuery);
  247.            
  248.             stmt.setInt(1, pID);
  249.             rowAffected = stmt.executeUpdate();
  250.            
  251.             conn.close();
  252.         } catch (Exception e) {
  253.             System.out.println(e.getMessage());
  254.         }
  255.         return rowAffected;
  256.     }
  257.     public String[] getSelectedRow(int pID) {
  258.         // ang silbi lang nito sa update ay para di mawala yung laman kung hindi babaguhin
  259.         String[] rowValue = new String[5];
  260.         String address = "jdbc:mysql://localhost:3306/db_mng_b2";
  261.         String userName = "root";
  262.         String passWord = "";
  263.         String sqlQuery = "SELECT * FROM tbl_prod_list WHERE fld_plist_id = ?";
  264.        
  265.         try {
  266.             Connection conn = DriverManager.getConnection(
  267.                     address,userName,passWord);
  268.             PreparedStatement stmt = conn.prepareStatement(sqlQuery);
  269.            
  270.             stmt.setInt(1, pID);
  271.             ResultSet rs = stmt.executeQuery();
  272.             rs.next();
  273.             rowValue[4] = rs.getString(1);
  274.             rowValue[0] = rs.getString(2);
  275.             rowValue[1] = rs.getString(3);
  276.             rowValue[2] = rs.getString(4);
  277.             rowValue[3] = rs.getString(5);
  278.            
  279.             conn.close();
  280.         } catch (Exception e) {
  281.             System.out.println(e.getMessage());
  282.         }
  283.        
  284.         return rowValue;
  285.     }
  286.     public int updateRow(String prodName
  287.             , double prodPrice, String prodCat, int proQty, int pID) {
  288.         int rowAffected = 0;
  289.         String address = "jdbc:mysql://localhost:3306/db_mng_b2";
  290.         String userName = "root";
  291.         String passWord = "";
  292.         String sqlQuery = "UPDATE tbl_prod_list"
  293.                 + " SET fld_prod_name=?"
  294.                 + ", fld_prod_price=?"
  295.                 + ", fld_category=?"
  296.                 + ", fld_quantity=?"
  297.                 + " WHERE fld_plist_id=?";
  298.         try {
  299.             Connection conn = DriverManager.getConnection(
  300.                     address,userName,passWord);
  301.             PreparedStatement stmt = conn.prepareStatement(sqlQuery);
  302.            
  303.             stmt.setString(1, prodName);
  304.             stmt.setDouble(2, prodPrice);
  305.             stmt.setString(3, prodCat);
  306.             stmt.setInt(4, proQty);
  307.             stmt.setInt(5, pID);
  308.            
  309.             rowAffected = stmt.executeUpdate();
  310.            
  311.             conn.close();
  312.         } catch (Exception e) {
  313.             System.out.println(e.getMessage());
  314.         }
  315.         return rowAffected;
  316.     }
  317.  
  318. }
  319.  
  320.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement