HarrJ

Day 21

Aug 24th, 2023 (edited)
1,064
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.22 KB | None | 0 0
  1. package connecting_to_db;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7.  
  8. public class CrudProductListD21 {
  9.     String address = "jdbc:mysql://127.0.0.1:3306/db_mng_batch5?zeroDateTimeBehavior=convertToNull";
  10.     String userName = "root";
  11.     String passWord = "";
  12.    
  13.     public void getAllCategory(){
  14.         String sqlQuery = "SELECT * FROM tbl_category_list ORDER BY fld_aisle_num";
  15.        
  16.         try {
  17.             Connection conn = DriverManager.getConnection(
  18.                     address,userName,passWord);
  19.             PreparedStatement stmt = conn.prepareStatement(sqlQuery);
  20.            
  21.             ResultSet rs = stmt.executeQuery();
  22.            
  23.             while (rs.next()) {                
  24.                 System.out.printf("%3s - ",rs.getString("fld_catid"));
  25.                 System.out.printf("%-20s%n",rs.getString("fld_catname"));
  26. //                System.out.printf("%-3s%n",rs.getString("fld_aisle_num"));
  27.             }
  28.            
  29.             conn.close();
  30.         } catch (Exception e) {
  31.             System.out.println(e.getMessage());
  32.         }
  33.     }
  34.    
  35.     public void getAllRows(){
  36.         String sqlQuery = "SELECT * FROM tbl_product_list ORDER BY fld_pid DESC LIMIT 0,15";
  37.         // REMOVE  ORDER BY fld_pid DESC LIMIT 0,15  TO SHOW ALL ROWS
  38.         try {
  39.             Connection conn = DriverManager.getConnection(
  40.                     address,userName,passWord);
  41.             PreparedStatement stmt = conn.prepareStatement(sqlQuery);
  42.            
  43.             ResultSet rs = stmt.executeQuery();
  44.            
  45.             while (rs.next()) {                
  46.                 System.out.printf("%3s | ",rs.getString(1));
  47.                 System.out.printf("%-35s | ",rs.getString(2));
  48.                 System.out.printf("%8.2f | ",rs.getDouble(3));
  49.                 System.out.printf("%-35s | ",rs.getString(4));
  50.                 System.out.printf("%-3s%n",rs.getString(5));
  51.             }
  52.            
  53.             conn.close();
  54.         } catch (Exception e) {
  55.             System.out.println(e.getMessage());
  56.         }
  57.     }
  58.    
  59.     public void getAllRows(String searchName){
  60.         String sqlQuery = "SELECT * FROM tbl_product_list WHERE LOWER(fld_pname) LIKE LOWER(?)";
  61.        
  62.         searchName = "%"+searchName+"%";
  63.         // REMOVE  ORDER BY fld_pid DESC LIMIT 0,15  TO SHOW ALL ROWS
  64.         try {
  65.             Connection conn = DriverManager.getConnection(
  66.                     address,userName,passWord);
  67.             PreparedStatement stmt = conn.prepareStatement(sqlQuery);
  68.            
  69.             stmt.setString(1, searchName);
  70.             ResultSet rs = stmt.executeQuery();
  71.            
  72.             while (rs.next()) {                
  73.                 System.out.printf("%3s | ",rs.getString(1));
  74.                 System.out.printf("%-35s | ",rs.getString(2));
  75.                 System.out.printf("%8.2f | ",rs.getDouble(3));
  76.                 System.out.printf("%-35s | ",rs.getString(4));
  77.                 System.out.printf("%-3s%n",rs.getString(5));
  78.             }
  79.            
  80.             conn.close();
  81.         } catch (Exception e) {
  82.             System.out.println(e.getMessage());
  83.         }
  84.     }
  85.    
  86.     public int addNewRow(String pName, double pPrice, String pManufacturer, int catId) {
  87.         int rowsAffected = 0;
  88.         String sqlQuery = "INSERT INTO tbl_product_list ( fld_pname, fld_price, fld_manufacturer, fld_catid) VALUES (?, ?, ?, ?);";
  89.         try {
  90.             Connection conn = DriverManager.getConnection(
  91.                     address,userName,passWord);
  92.             PreparedStatement stmt = conn.prepareStatement(sqlQuery);
  93.            
  94.             stmt.setString(1, pName);
  95.             stmt.setDouble(2, pPrice);
  96.             stmt.setString(3, pManufacturer);
  97.             stmt.setInt(4, catId);
  98.            
  99.             rowsAffected = stmt.executeUpdate();
  100.            
  101.             conn.close();
  102.         } catch (Exception e) {
  103.             System.out.println(e.getMessage());
  104.         }
  105.         return rowsAffected;
  106.     }
  107.    
  108.     public int deleteProductRow(int pid) {
  109.         int rowsAffected = 0;String sqlQuery = "DELETE FROM tbl_product_list WHERE fld_pid = ?";
  110.        
  111.         try {
  112.             Connection conn = DriverManager.getConnection(
  113.                     address,userName,passWord);
  114.             PreparedStatement stmt = conn.prepareStatement(sqlQuery);
  115.            
  116.             stmt.setInt(1, pid);
  117.            
  118.             rowsAffected = stmt.executeUpdate();
  119.            
  120.             conn.close();
  121.         } catch (Exception e) {
  122.             System.out.println(e.getMessage());
  123.         }
  124.        
  125.         return rowsAffected;
  126.     }
  127. }
  128.  
  129.  
  130. //-------------------------------
  131. package connecting_to_db;
  132. import java.util.Scanner;
  133.  
  134. public class Day21B {
  135.     public static void main(String[] args) {
  136.         CrudProductListD21 callCRUD = new CrudProductListD21();
  137.         Scanner sc = new Scanner(System.in);
  138.         String op, txtIn;
  139.         int rowsAffected=0;
  140.         boolean isValid = true;
  141.         String pName, pManufacturer;
  142.         double pPrice = 0;
  143.         int catId = 0, prodId = 0;
  144.        
  145.         System.out.println("Using tbl_product_list");
  146.         System.out.println("Options:");
  147.         System.out.println("1 - Show all rows");
  148.         System.out.println("2 - Add new row");
  149.         System.out.println("3 - Search by product name");
  150.         System.out.println("4 - Delete by id");
  151.         System.out.print("Enter number of action:");
  152.         op = sc.nextLine();
  153.        
  154.         switch (op) {
  155.             case "1" :
  156.                 callCRUD.getAllRows();
  157.                 break;
  158.             case "2" :
  159.                 System.out.print("Enter Product Name: ");
  160.                 pName = sc.nextLine();
  161.                 System.out.print("Enter Price: ");
  162.                 txtIn = sc.nextLine();
  163.                 if (isNumeric(txtIn)) {
  164.                     pPrice = Double.parseDouble(txtIn);
  165.                 } else {
  166.                     isValid = false;
  167.                 }
  168.                 System.out.print("Enter Manufacturer: ");
  169.                 pManufacturer = sc.nextLine();
  170.                 callCRUD.getAllCategory();
  171.                 System.out.print("Enter Category ID: ");
  172.                 txtIn = sc.nextLine();
  173.                 if (isNumeric(txtIn)) {
  174.                     catId = Integer.parseInt(txtIn);
  175.                 } else {
  176.                     isValid = false;
  177.                 }
  178.                 if (isValid == true) {
  179.                     rowsAffected = callCRUD.addNewRow(pName, pPrice, pManufacturer, catId);
  180.                     if (rowsAffected == 1) {
  181.                         System.out.println("New row added");
  182.                     } else {
  183.                         System.out.println("Problem with SQL");
  184.                     }
  185.                 } else {
  186.                     System.out.println("One of your input is invalid");
  187.                 }
  188.                 break;
  189.             case "3" :
  190.                 System.out.print("Enter Product Name to search: ");
  191.                 pName = sc.nextLine();
  192.                 callCRUD.getAllRows(pName);
  193.                 break;
  194.             case "4" :System.out.print("Enter Category ID: ");
  195.                 txtIn = sc.nextLine();
  196.                 if (isNumeric(txtIn)) {
  197.                     prodId = Integer.parseInt(txtIn);
  198.                 } else {
  199.                     isValid = false;
  200.                 }
  201.                 if (isValid == true) {
  202.                     rowsAffected = callCRUD.deleteProductRow(prodId);
  203.                     if (rowsAffected == 1) {
  204.                         System.out.println("Row deleted");
  205.                     } else {
  206.                         System.out.println("Problem with SQL");
  207.                     }
  208.                 } else {
  209.                     System.out.println("One of your input is invalid");
  210.                 }
  211.                 break;
  212.         }
  213.     }
  214.    
  215.     public static boolean isNumeric(String strNum) {
  216.         if (strNum == null) {
  217.             return false;
  218.         }
  219.         try {
  220.             double d = Double.parseDouble(strNum);
  221.         } catch (NumberFormatException nfe) {
  222.             return false;
  223.         }
  224.         return true;
  225.     }
  226.    
  227. }
  228.  
Advertisement
Add Comment
Please, Sign In to add comment