Advertisement
HarrJ

B8 Day 26 DELETE using Java

Oct 10th, 2022 (edited)
1,180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.53 KB | None | 0 0
  1. package mattroseb8wk5;
  2. import java.util.Scanner;
  3. import java.sql.*;
  4.  
  5. public class Day26A {
  6.     private final String address = "jdbc:mysql://localhost:3306/mr_batch8_db";
  7.     private final String userName = "root";
  8.     private final String passWord = "";
  9.  
  10.     public static void main(String[] args) {
  11.         Scanner sc = new Scanner(System.in);
  12.         Day26A callMe = new Day26A();
  13.        
  14.         int pid;
  15.         String confirm = "NO";
  16.         callMe.getRows();
  17.  
  18.         System.out.print("\nEnter id of row to be DELETED >> ");
  19.         pid = sc.nextInt();
  20.         sc.nextLine();
  21.                
  22.         System.out.println();
  23.         System.out.println("Are you sure you want to delete row with id "
  24.             + pid + "?(YES/NO)");
  25.         confirm = sc.nextLine();
  26.        
  27.         if (confirm.equals("YES")) {
  28.             int rowAffected = callMe.deleteRow(pid);
  29.             System.out.println(rowAffected + " row deleted");
  30.         }
  31.        
  32.     }
  33.  
  34.     public void getRows() {
  35.         String sqlQuery = "SELECT * FROM tbl_price_list";
  36.        
  37.         try {
  38.             Connection conn = DriverManager.getConnection(
  39.                     address, userName, passWord);
  40.            
  41.             PreparedStatement stmt = conn.prepareStatement(sqlQuery);
  42.            
  43.             ResultSet rs = stmt.executeQuery();
  44.            
  45.             while (rs.next()) {      
  46.                 System.out.print(String.format("%3d | ", rs.getInt(1)));
  47.                 System.out.print(String.format("%-30s | ", rs.getString(2)));
  48.                 System.out.print(String.format("%7.2f | ", rs.getDouble(3)));
  49.                 System.out.print(String.format("%-18s | ", rs.getString(4)));
  50.                 System.out.println(String.format("%-35s | ", rs.getString(5)));
  51.             }
  52.            
  53.             conn.close();
  54.         } catch (Exception e) {
  55.             System.out.println(e);
  56.         }
  57.     }
  58.  
  59.     public int deleteRow(int pid) {
  60.         int rowsAffected = 0;
  61.         String sqlQuery = "DELETE FROM tbl_price_list WHERE fld_pid = ?;";
  62.        
  63.         try {
  64.             Connection conn = DriverManager.getConnection(
  65.                     address, userName, passWord);
  66.            
  67.             PreparedStatement stmt = conn.prepareStatement(sqlQuery);
  68.            
  69.             stmt.setInt(1, pid);
  70.            
  71.             rowsAffected = stmt.executeUpdate();            
  72.             conn.close();
  73.         } catch (Exception e) {
  74.             System.out.println(e);
  75.         }
  76.        
  77.         return rowsAffected;
  78.     }
  79. }
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement