HarrJ

B8 Day 25 UPDATE demo

Oct 9th, 2022
1,309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.25 KB | None | 0 0
  1. package mattroseb8wk5;
  2. import java.util.Scanner;
  3. import java.sql.*;
  4.  
  5.  
  6. public class Day25A {
  7.     private final String address = "jdbc:mysql://localhost:3306/mr_batch8_db";
  8.     private final String userName = "root";
  9.     private final String passWord = "";
  10.    
  11. public static void main(String[] args) {
  12.         Scanner sc = new Scanner(System.in);
  13.         Day25Methods callMe = new Day25Methods();
  14.        
  15.         int pid;
  16.         String pName;
  17.         double pPrice;
  18.         String pCat;
  19.         String pManufacturer;
  20.        
  21.         callMe.getRows();
  22.        
  23.         System.out.print("Enter id of row to be updated");
  24.         pid = sc.nextInt();
  25.         sc.nextLine();
  26.         System.out.print("Enter Updated Name: ");
  27.         pName = sc.nextLine();
  28.         System.out.print("Enter Updated Price: ");
  29.         pPrice = sc.nextDouble();
  30.         sc.nextLine();
  31.         System.out.print("Enter Updated Category: ");
  32.         pCat = sc.nextLine();
  33.         System.out.print("Enter Updated Manufacturer: ");
  34.         pManufacturer = sc.nextLine();
  35.        
  36.         int result = callMe.updateRow(pName, pPrice, pCat, pManufacturer, pid);
  37.         System.out.println(result + " row(s) affected");
  38.     }
  39.  
  40.     public void getRows(String searchVal) {
  41.         searchVal = "%".concat(searchVal).concat("%");
  42.         String sqlQuery = "SELECT * FROM tbl_price_list WHERE fld_pname LIKE ?";
  43.        
  44.         try {
  45.             Connection conn = DriverManager.getConnection(
  46.                     address, userName, passWord);
  47.            
  48.             PreparedStatement stmt = conn.prepareStatement(sqlQuery);
  49.            
  50.             stmt.setString(1, searchVal);
  51.             ResultSet rs = stmt.executeQuery();
  52.            
  53.             while (rs.next()) {      
  54.                 System.out.print(String.format("%3d | ", rs.getInt(1)));
  55.                 System.out.print(String.format("%-30s | ", rs.getString(2)));
  56.                 System.out.print(String.format("%7.2f | ", rs.getDouble(3)));
  57.                 System.out.print(String.format("%-18s | ", rs.getString(4)));
  58.                 System.out.println(String.format("%-35s | ", rs.getString(5)));
  59.             }
  60.            
  61.             conn.close();
  62.         } catch (Exception e) {
  63.             System.out.println(e);
  64.         }
  65.     }
  66.  
  67.     public int updateRow(String pName, double pPrice
  68.             , String pCat, String pManufacturer, int pid) {
  69.         int rowsAffected = 0;
  70.         String sqlQuery = "UPDATE tbl_price_list SET fld_pname = ? "
  71.             + ",fld_price = ? ,fld_pcategory = ? ,fld_manufacturer = ? "
  72.             + "WHERE fld_pid = ?;";
  73.        
  74.         try {
  75.             Connection conn = DriverManager.getConnection(
  76.                     address, userName, passWord);
  77.            
  78.             PreparedStatement stmt = conn.prepareStatement(sqlQuery);
  79.            
  80.             stmt.setString(1, pName);
  81.             stmt.setDouble(2, pPrice);
  82.             stmt.setString(3, pCat);
  83.             stmt.setString(4, pManufacturer);
  84.             stmt.setInt(5, pid);
  85.            
  86.             rowsAffected = stmt.executeUpdate();            
  87.             conn.close();
  88.         } catch (Exception e) {
  89.             System.out.println(e);
  90.         }
  91.        
  92.         return rowsAffected;
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment