HarrJ

Day 24 discussion

Jul 19th, 2023
1,176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.11 KB | None | 0 0
  1. package week4training;
  2. import java.util.Scanner;
  3.  
  4. public class Day24A {
  5.     public static void main(String[] args) {
  6.         Scanner sc = new Scanner(System.in);
  7.         RunQuery02 callQuery = new RunQuery02();
  8.         String opt;
  9.         String prodName,prodCat;
  10.         double prodPrice;
  11.         int proQty, rowsAffected = 0;
  12.        
  13.        
  14.         System.out.println("Options:");
  15.         System.out.println("1 - View all items");
  16.         System.out.println("2 - Add new item");
  17.         System.out.print("Type number of action: ");
  18.         opt = sc.nextLine();
  19.        
  20.         switch (opt) {
  21.             case "1":
  22.                 callQuery.viewProdList();
  23.                 break;
  24.             case "2":
  25.                 System.out.println("Enter info for new row:");
  26.                 try {
  27.                     System.out.print("Product Name: ");
  28.                     prodName = sc.nextLine();
  29.                     System.out.print("Product Price: ");
  30.                     prodPrice = sc.nextDouble();
  31.                     sc.nextLine();
  32.                     System.out.print("Category: ");
  33.                     prodCat = sc.nextLine();
  34.                     System.out.print("Quantity: ");
  35.                     proQty = sc.nextInt();
  36.                     sc.nextLine();
  37.                     rowsAffected = callQuery.addNewRow(prodName, prodPrice, prodCat, proQty);
  38.                 } catch (Exception e) {
  39.                     System.out.println("invalid input please try again");
  40.                 }
  41.                
  42.                 if (rowsAffected == 1) {
  43.                     System.out.println("Record Complete");
  44.                 } else {
  45.                     System.out.println("Record failed");
  46.                 }                
  47.                 break;
  48.         }
  49.     }
  50. }
  51.  
  52.  
  53. //------
  54.  
  55. package week4training;
  56. import java.sql.*;
  57.  
  58. public class RunQuery02 {
  59.     public int addNewRow(String prodName
  60.             , double prodPrice, String prodCat, int proQty) {
  61.         int rowAffected = 0;
  62.        
  63.         String address = "jdbc:mysql://localhost:3306/db_mng_b2";
  64.         String userName = "root";
  65.         String passWord = "";
  66.         String sqlQuery = "INSERT INTO tbl_prod_list"
  67.                 + "(fld_prod_name, fld_prod_price, fld_category, fld_quantity) "
  68.                 + "VALUES (?, ?, ?, ?)";
  69.         // ? IS A PLACEHOLDER
  70.         try {
  71.             Connection conn = DriverManager.getConnection(
  72.                     address,userName,passWord);
  73.             PreparedStatement stmt = conn.prepareStatement(sqlQuery);
  74.            
  75.             stmt.setString(1, prodName);
  76.             stmt.setDouble(2, prodPrice);
  77.             stmt.setString(3, prodCat);
  78.             stmt.setInt(4, proQty);
  79.            
  80.             rowAffected = stmt.executeUpdate();
  81.             // O RETURNED = INSERT INTO FAILED
  82.            
  83.             conn.close();
  84.         } catch (Exception e) {
  85.             System.out.println(e.getMessage());
  86.         }
  87.         return rowAffected;
  88.     }
  89.    
  90.     public void viewProdList() {
  91.         String address = "jdbc:mysql://localhost:3306/db_mng_b2";
  92.         String userName = "root";
  93.         String passWord = "";
  94.         String sqlQuery = "SELECT fld_plist_id"
  95.                 + ", fld_prod_name"
  96.                 + ", fld_prod_price"
  97.                 + ", fld_category"
  98.                 + ", fld_quantity "
  99.                 + "FROM tbl_prod_list";
  100.        
  101.         try {
  102.             Connection conn = DriverManager.getConnection(
  103.                     address,userName,passWord);
  104.             PreparedStatement stmt = conn.prepareStatement(sqlQuery);
  105.            
  106.             ResultSet rs = stmt.executeQuery();
  107.             while (rs.next()) {
  108.                 System.out.println(String.format(
  109.                     "%-4d | %-30s | %8.2f | %-30s | %4d"
  110.                     ,rs.getInt(1)
  111.                     ,rs.getString(2)
  112.                     ,rs.getFloat(3)
  113.                     ,rs.getString(4)
  114.                     ,rs.getInt(5)
  115.                 ));
  116.             }
  117.             conn.close();
  118.         } catch (Exception e) {
  119.             System.out.println(e.getMessage());
  120.         }
  121.     }
  122. }
  123.  
Advertisement
Add Comment
Please, Sign In to add comment