HarrJ

Day 22 Methods

Feb 25th, 2024 (edited)
1,046
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.97 KB | None | 0 0
  1. package week4;
  2.  
  3. import java.util.Scanner;
  4. public class Day22A {
  5. //    Pwede natin paghiwalayin
  6. //    pwede magsiksikan sila sa psvm
  7.    
  8.     public static void main(String[] args) {
  9.         Scanner sc = new Scanner(System.in);
  10.         Day22MethodsLine callMethods = new Day22MethodsLine();
  11.         String pName;
  12.         double pPrice;
  13.         String pMfg;
  14.         String pCategory;
  15.         String opt;
  16.         String searchColumn;
  17.         String searchValue;
  18.         int rowsAffected;
  19.        
  20.         System.out.println("Options:");
  21.         System.out.println("1 - Add New Row");
  22.         System.out.println("2 - View All Row");
  23.         System.out.println("3 - Search by Product Name");
  24.         System.out.println("any other character - View Table");
  25.         System.out.print(">> ");
  26.         opt = sc.nextLine();
  27.        
  28.         switch (opt) {
  29.             case "1" :
  30.                 try {
  31.                     System.out.println("Add New Row!!");
  32.                     System.out.print("Enter Product Name: ");
  33.                     pName = sc.nextLine();
  34.                     System.out.print("Enter Price: ");
  35.                     pPrice = sc.nextDouble();
  36.                     sc.nextLine();
  37.                     System.out.print("Enter Product Manufacturer: ");
  38.                     pMfg = sc.nextLine();
  39.                     System.out.print("Enter Product Category: ");
  40.                     pCategory = sc.nextLine();
  41.                    
  42.                     rowsAffected = callMethods.addNewRow(pName, pPrice, pMfg, pCategory);
  43.                    
  44. //                    System.out.println(">" + pName);
  45. //                    System.out.println(">" + pPrice);
  46. //                    System.out.println(">" + pMfg);
  47. //                    System.out.println(">" + pCategory);
  48. //                    System.out.println(">" + rowsAffected);
  49.                     if (rowsAffected == 1) {
  50.                         System.out.println("New row added");
  51.                     } else {
  52.                         System.out.println("There is problem in adding new row");
  53.                     }
  54.                 } catch (Exception e) {
  55.                     System.out.println(e.toString());
  56.                 }
  57.                 break;
  58.             case "2" :
  59.                 callMethods.printAllRows();
  60.                 break;
  61.             case "3" :
  62.                 System.out.println("type the option of which column to:(default by name)");
  63.                 System.out.println("A - search by product name");
  64.                 System.out.println("B - search by product manufacturer");
  65.                 System.out.println("C - search by product category");
  66.                 System.out.print(">>> ");
  67.                 searchColumn = sc.nextLine();
  68.                 System.out.print("Search Name: ");
  69.                 searchValue = sc.nextLine();
  70.                 callMethods.printAllRows(searchColumn, searchValue);
  71.                 break;
  72.             default:
  73.                 System.out.println("your selection is not an action");
  74.         }
  75.     }
  76. }
  77.  
  78. //--------------------------------------------------
  79.  
  80. package week4;
  81.  
  82. import java.sql.*;
  83. public class Day22MethodsLine {
  84.     private String address = "jdbc:mysql://localhost:3306/db_jtvi_b8_23";
  85.     private String userName = "jtvi23b8"; //default "root"
  86.     private String passWord = "demo";
  87.    
  88.     int addNewRow(String name, double price, String mfg, String catName) {
  89.         int rowsAffected = 0;
  90.         try {
  91.             Connection conn = DriverManager.getConnection(address, userName, passWord);
  92.             String sqlQuery = "INSERT INTO tbl_price_list (fld_pname ,fld_price"
  93.                     + " ,fld_manufacturer ,fld_cat_name)"
  94.                     + " VALUES (? ,? ,? ,? );";
  95.            
  96.             PreparedStatement stmt = conn.prepareStatement(sqlQuery);
  97.             stmt.setString(1, name);
  98.             stmt.setDouble(2, price);
  99.             stmt.setString(3, mfg);
  100.             stmt.setString(4, catName);
  101.             rowsAffected = stmt.executeUpdate();
  102.            
  103.             conn.close();
  104.         } catch (Exception e) {
  105.             rowsAffected = -1;
  106.             System.out.println(e.toString());
  107.         }
  108.         return rowsAffected;
  109.     }
  110.    
  111.     void printAllRows() { // pang command line na result
  112.         try {
  113.             Connection conn = DriverManager.getConnection(address, userName, passWord);
  114.             String sqlQuery = "SELECT fld_pid ,fld_pname ,fld_price "
  115.                     + ",fld_manufacturer ,fld_cat_name FROM tbl_price_list;";
  116.            
  117.             PreparedStatement stmt = conn.prepareStatement(sqlQuery);
  118.            
  119.             ResultSet rs = stmt.executeQuery();
  120.            
  121.             while (rs.next()) {
  122.                 System.out.printf("%-2d | %-35s | %-8.2f | %-35s | %-20s%n"
  123.                     , rs.getInt(1)
  124.                     , rs.getString(2)
  125.                     , rs.getDouble(3)
  126.                     , rs.getString(4)
  127.                     , rs.getString(5)
  128.                 );
  129.             }
  130.            
  131.         } catch (Exception e) {
  132.             System.out.println(e.toString());
  133.         }
  134.     }
  135.    
  136.     // pwede na sana ito
  137.     void printAllRows(String pName) { // pang command line na result
  138.         String searchVal = "%" + pName + "%";
  139.         try {
  140.             Connection conn = DriverManager.getConnection(address, userName, passWord);
  141.             String sqlQuery = "SELECT fld_pid ,fld_pname ,fld_price "
  142.                     + ",fld_manufacturer ,fld_cat_name FROM tbl_price_list"
  143.                     + " WHERE fld_pname LIKE ?;";
  144.            
  145.             PreparedStatement stmt = conn.prepareStatement(sqlQuery);
  146.             stmt.setString(1, searchVal);
  147.            
  148.             ResultSet rs = stmt.executeQuery();
  149.            
  150.             while (rs.next()) {
  151.                 System.out.printf("%-2d | %-35s | %-8.2f | %-35s | %-20s%n"
  152.                     , rs.getInt(1)
  153.                     , rs.getString(2)
  154.                     , rs.getDouble(3)
  155.                     , rs.getString(4)
  156.                     , rs.getString(5)
  157.                 );
  158.             }
  159.            
  160.         } catch (Exception e) {
  161.             System.out.println(e.toString());
  162.         }
  163.     }
  164.    
  165.     // what if di lang pang isang field na where
  166.     void printAllRows(String pField, String pSearch) { // pang command line na result
  167.         String searchVal = "%" + pSearch + "%";
  168.         String searchField;
  169.         switch (pField) {
  170.             case "A":
  171.                 searchField = "fld_pname";
  172.                 break;
  173.             case "B":
  174.                 searchField = "fld_manufacturer";
  175.                 break;
  176.             case "C":
  177.                 searchField = "fld_cat_name";
  178.                 break;
  179.             default:
  180.                 searchField = "fld_pname";
  181.         }
  182.         try {
  183.             Connection conn = DriverManager.getConnection(address, userName, passWord);
  184.             String sqlQuery = "SELECT fld_pid ,fld_pname ,fld_price "
  185.                     + ",fld_manufacturer ,fld_cat_name FROM tbl_price_list"
  186.                     + " WHERE " + searchField + " LIKE ?;";
  187.            
  188.             PreparedStatement stmt = conn.prepareStatement(sqlQuery);
  189.             stmt.setString(1, searchVal);
  190.            
  191.             ResultSet rs = stmt.executeQuery();
  192.            
  193.             while (rs.next()) {
  194.                 System.out.printf("%-2d | %-35s | %-8.2f | %-35s | %-20s%n"
  195.                     , rs.getInt(1)
  196.                     , rs.getString(2)
  197.                     , rs.getDouble(3)
  198.                     , rs.getString(4)
  199.                     , rs.getString(5)
  200.                 );
  201.             }
  202.            
  203.         } catch (Exception e) {
  204.             System.out.println(e.toString());
  205.         }
  206.     }
  207. }
  208.  
  209. //CREATE TABLE tbl_price_list (
  210. //    fld_pid INT NOT NULL AUTO_INCREMENT PRIMARY KEY
  211. //    ,fld_pname VARCHAR (35) NOT NULL
  212. //    ,fld_price DECIMAL (20, 4) NOT NULL
  213. //    ,fld_manufacturer VARCHAR (35) NOT NULL
  214. //    ,fld_cat_name VARCHAR (20) NOT NULL
  215. //);
Advertisement
Add Comment
Please, Sign In to add comment