HarrJ

Day 27 Update

Jul 23rd, 2023
1,365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.63 KB | None | 0 0
  1. package projectSamples;
  2. import java.util.Scanner;
  3.  
  4. public class Day27EmpUpdate {
  5.     public static void main(String[] args) {
  6.         Scanner sc = new Scanner(System.in);
  7.         Day26EmpMethods callQuery = new Day26EmpMethods();
  8.         int eid;
  9.         String fName, lName, dept, phone, sex, salary, bday;
  10.         int rowsAffected = 0;
  11.        
  12.         callQuery.getRows();
  13.        
  14.         System.out.println("***Update Employee Info***");
  15.         //nilagay sa try catch para pag may maling input, di tutuloy
  16.         try {
  17.             System.out.print("Enter Employee ID: ");
  18.             eid = sc.nextInt();
  19.             sc.nextLine();
  20.             System.out.print("Updated First Name: ");
  21.             fName = sc.nextLine();
  22.             System.out.print("Updated Last Name: ");
  23.             lName = sc.nextLine();
  24.             System.out.print("Updated Dept(ADG/XVN/RTG): ");
  25.             dept = sc.nextLine();
  26.             System.out.print("Updated Phone: ");
  27.             phone = sc.nextLine();
  28.             System.out.print("Updated Sex: ");
  29.             sex = sc.nextLine();
  30.             System.out.print("Updated Salary: ");
  31.             salary = sc.nextLine();
  32.             System.out.print("Updated Birthday(YYYY-MM-DD): ");
  33.             bday = sc.nextLine();
  34.  
  35.             rowsAffected = callQuery.updateEmpInfo(fName, lName, dept
  36.                     , phone, sex, salary, bday, eid);
  37.         } catch (Exception e) {
  38.            
  39.         }
  40.         if (rowsAffected == 1) {
  41.             System.out.println("Employee Info Updated");
  42.         } else {
  43.             System.out.println("Record Failed");
  44.         }
  45.     }
  46. }
  47.  
  48. //---------------------------------------------
  49. package projectSamples;
  50. import java.sql.*;
  51.  
  52. class Day26EmpMethods {
  53.     // the 2 methods below are for update
  54.     //method 1 to get a copy of the row
  55.     public String[] getOneRow(int eid) {
  56.         String[] rowResult = new String[8];
  57.        
  58.         String address = "jdbc:mysql://localhost:3306/db_mng_b2";
  59.         String userName = "root";
  60.         String passWord = "";
  61.         String sqlQuery = "SELECT fld_enum"
  62.                 + ", fld_fname, fld_lname"
  63.                 + ", fld_work_dept, fld_phone"
  64.                 + ", fld_sex, fld_salary, fld_birthday"
  65.                 + " FROM tbl_employee"
  66.                 + " WHERE fld_enum = ?";
  67.         try {
  68.             Connection conn = DriverManager.getConnection(
  69.                     address,userName,passWord);
  70.             PreparedStatement stmt = conn.prepareStatement(sqlQuery);
  71.            
  72.             stmt.setInt(1, eid);
  73.  
  74.             ResultSet rs = stmt.executeQuery();
  75.             while (rs.next()) {
  76.                 rowResult[0] = rs.getString(1);
  77.                 rowResult[1] = rs.getString(2);
  78.                 rowResult[2] = rs.getString(3);
  79.                 rowResult[3] = rs.getString(4);
  80.                 rowResult[4] = rs.getString(5);
  81.                 rowResult[5] = rs.getString(6);
  82.                 rowResult[6] = rs.getString(7);
  83.                 rowResult[7] = rs.getString(8);
  84.             }
  85.            
  86.             conn.close();
  87.         } catch (Exception e) {
  88.             System.out.println(e.getMessage());
  89.         }
  90.         return rowResult;
  91.     }
  92.    
  93.     // method 2 - for updating
  94.     public int updateEmpInfo(String fName, String lName, String dept
  95.         ,String phone, String sex, String salary, String bday, int eid){
  96.         String[] origValue = getOneRow(eid);
  97.         double salaryForQuery;
  98.        
  99.         if (fName.isEmpty() || fName.trim().equals("")) {
  100.             fName = origValue[1]; //galing sa select
  101.         }
  102.         if (lName.isEmpty() || lName.trim().equals("")) {
  103.             lName = origValue[2];
  104.         }
  105.         if (dept.isEmpty() || dept.trim().equals("")) {
  106.             dept = origValue[3];
  107.         }
  108.         if (phone.isEmpty() || phone.trim().equals("")) {
  109.             phone = origValue[4];
  110.         }
  111.         if (sex.isEmpty() || sex.trim().equals("")) {
  112.             sex = origValue[5];
  113.         }
  114.         if (salary.isEmpty() || salary.trim().equals("")) {
  115.             salaryForQuery = Double.parseDouble(origValue[6]); //galing sa select
  116.         }else {
  117.             salaryForQuery = Double.parseDouble(salary); //gagamitin yung tinype ni user
  118.         }
  119.         if (bday.isEmpty() || bday.trim().equals("")) {
  120.             bday = origValue[7]; //galing sa select
  121.         }
  122.        
  123.         int rowsAffected = 0;
  124.         String address = "jdbc:mysql://localhost:3306/db_mng_b2";
  125.         String userName = "root";
  126.         String passWord = "";
  127.         String sqlQuery = "UPDATE tbl_employee"
  128.                 +" SET fld_fname = ?"
  129.                 +", fld_lname = ?"
  130.                 +", fld_work_dept = ?"
  131.                 +", fld_phone = ?"
  132.                 +", fld_sex = ?"
  133.                 +", fld_salary = ?"
  134.                 +", fld_birthday = ?"
  135.                 +" WHERE fld_enum = ?";
  136.    
  137.         try {
  138.             Connection conn = DriverManager.getConnection(
  139.                     address,userName,passWord);
  140.             PreparedStatement stmt = conn.prepareStatement(sqlQuery);
  141.            
  142.             stmt.setString(1, fName);
  143.             stmt.setString(2, lName);
  144.             stmt.setString(3, dept);
  145.             stmt.setString(4, phone);
  146.             stmt.setString(5, sex);
  147.             stmt.setDouble(6, salaryForQuery);
  148.             stmt.setString(7, bday);
  149.             stmt.setInt(8, eid);
  150.            
  151.             rowsAffected = stmt.executeUpdate();
  152.            
  153.             conn.close();
  154.         } catch (Exception e) {
  155.             System.out.println(e.getMessage());
  156.         }
  157.    
  158.         return rowsAffected;
  159.     }
  160. }
  161.  
Advertisement
Add Comment
Please, Sign In to add comment