HarrJ

B8 Day 27 UPDATE with skip

Oct 11th, 2022 (edited)
754
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.68 KB | None | 0 0
  1. package mattroseb8wk5;
  2. import java.sql.*;
  3. import java.util.Scanner;
  4.  
  5. public class Day27UpdateV2 {
  6.     public static void main(String[] args) {
  7.         Scanner sc = new Scanner(System.in);
  8.         Day27UpdateV2 callMethods = new Day27UpdateV2();
  9.        
  10.         int pid;
  11.         String pName;
  12.         double pPrice;
  13.         String pCat;
  14.         String pManufacturer;
  15.        
  16.         String[] originalValue;
  17.         String userInput;
  18.        
  19.         callMethods.getRows();
  20.        
  21.         System.out.print("Enter id of row to be updated >> ");
  22.         pid = sc.nextInt();
  23.         sc.nextLine();
  24.         // gagamitin natin yung String[] getRows(int searchId) para
  25.         // may pang puno tayo ng field kapag nag skip si user
  26.         originalValue = callMethods.getRows(pid);
  27.         System.out.print("Enter Updated Name: ");
  28.         userInput = sc.nextLine();
  29.         pName = callMethods.checkString(userInput, originalValue[1]);
  30.         userInput = "";
  31.        
  32.         System.out.print("Enter Updated Price: ");
  33.         userInput = sc.nextLine();
  34.         pPrice = callMethods.checkDouble(userInput, originalValue[2]);
  35.         userInput = "";
  36.        
  37.         System.out.print("Enter Updated Category: ");
  38.         userInput = sc.nextLine();
  39.         pCat = callMethods.checkString(userInput, originalValue[3]);
  40.         userInput = "";
  41.        
  42.         System.out.print("Enter Updated Manufacturer: ");
  43.         userInput = sc.nextLine();
  44.         pManufacturer = callMethods.checkString(userInput, originalValue[4]);
  45.         userInput = "";
  46.        
  47.         System.out.println(pName + ":" +  originalValue[1]);
  48.         System.out.println(pPrice + ":" +  originalValue[2]);
  49.         System.out.println(pCat + ":" +  originalValue[3]);
  50.         System.out.println(pManufacturer + ":" +  originalValue[4]);
  51.        
  52.         // uncomment nyo yung nasa baba pag talagang napipilian na nya yung skip ninyo
  53.         //int result = callMethods.updateRow(pName, pPrice, pCat, pManufacturer, pid);
  54.         //System.out.println(result + " row(s) affected");
  55.     }
  56.    
  57.     public int updateRow(String pName, double pPrice
  58.             , String pCat, String pManufacturer, int pid) {
  59.         int rowsAffected = 0;
  60.         String sqlQuery = "UPDATE tbl_price_list SET fld_pname = ? "
  61.             + ",fld_price = ? ,fld_pcategory = ? ,fld_manufacturer = ? "
  62.             + "WHERE fld_pid = ?;";
  63.        
  64.         try {
  65.             Connection conn = DriverManager.getConnection(
  66.                     address, userName, passWord);
  67.            
  68.             PreparedStatement stmt = conn.prepareStatement(sqlQuery);
  69.            
  70.             stmt.setString(1, pName);
  71.             stmt.setDouble(2, pPrice);
  72.             stmt.setString(3, pCat);
  73.             stmt.setString(4, pManufacturer);
  74.             stmt.setInt(5, pid);
  75.            
  76.             rowsAffected = stmt.executeUpdate();            
  77.             conn.close();
  78.         } catch (Exception e) {
  79.             System.out.println(e);
  80.         }
  81.        
  82.         return rowsAffected;
  83.     }
  84.    
  85.     // mga support ni UPDATE
  86.     public void getRows() {
  87.         String sqlQuery = "SELECT * FROM tbl_price_list ";
  88.        
  89.         try {
  90.             Connection conn = DriverManager.getConnection(
  91.                     address, userName, passWord);
  92.            
  93.             PreparedStatement stmt = conn.prepareStatement(sqlQuery);
  94.            
  95.             ResultSet rs = stmt.executeQuery();
  96.            
  97.             while (rs.next()) {      
  98.                 System.out.print(String.format("%3d | ", rs.getInt(1)));
  99.                 System.out.print(String.format("%-30s | ", rs.getString(2)));
  100.                 System.out.print(String.format("%7.2f | ", rs.getDouble(3)));
  101.                 System.out.print(String.format("%-18s | ", rs.getString(4)));
  102.                 System.out.println(String.format("%-35s | ", rs.getString(5)));
  103.             }
  104.            
  105.             conn.close();
  106.         } catch (Exception e) {
  107.             System.out.println(e);
  108.         }
  109.     }
  110.    
  111.     public String[] getRows(int searchId) {
  112.         String[] resultArr = {"0","name","0.0","cat","manufacturer"};
  113.        
  114.         String sqlQuery = "SELECT * FROM tbl_price_list WHERE fld_pid = ? LIMIT 1";
  115.        
  116.         try {
  117.             Connection conn = DriverManager.getConnection(
  118.                     address, userName, passWord);
  119.            
  120.             PreparedStatement stmt = conn.prepareStatement(sqlQuery);
  121.            
  122.             stmt.setInt(1, searchId);
  123.             ResultSet rs = stmt.executeQuery();
  124.            
  125.             rs.next();
  126.             resultArr[0] = rs.getString(1);
  127.             resultArr[1] = rs.getString(2);
  128.             resultArr[2] = rs.getString(3);
  129.             resultArr[3] = rs.getString(4);
  130.             resultArr[4] = rs.getString(5);
  131.            
  132.             conn.close();
  133.         } catch (Exception e) {
  134.            
  135.         }
  136.        
  137.         return resultArr;
  138.     }
  139.  
  140.     public String checkString(String newValue, String oldValue) {
  141.         String resStr = newValue;
  142.         if (newValue.trim().isEmpty()) {
  143.             resStr = oldValue;
  144.         }
  145.         return resStr;
  146.     }
  147.    
  148.     public double checkDouble(String newValue, String oldValue) {
  149.         double resDbl = 0.0;
  150.         try {
  151.             resDbl = Double.parseDouble(newValue);
  152.         } catch (Exception e) {
  153.             resDbl = Double.parseDouble(oldValue);
  154.         }
  155.         return resDbl;
  156.     }
  157.    
  158.     public int checkInt(String newValue, String oldValue) {
  159.         int resInt = 0;
  160.         try {
  161.             resInt = Integer.parseInt(newValue);
  162.         } catch (Exception e) {
  163.             resInt = Integer.parseInt(oldValue);
  164.         }
  165.         return resInt;
  166.     }
  167. }
  168.  
Advertisement
Add Comment
Please, Sign In to add comment