Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package projectSamples;
- import java.util.Scanner;
- public class Day27EmpUpdate {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- Day26EmpMethods callQuery = new Day26EmpMethods();
- int eid;
- String fName, lName, dept, phone, sex, salary, bday;
- int rowsAffected = 0;
- callQuery.getRows();
- System.out.println("***Update Employee Info***");
- //nilagay sa try catch para pag may maling input, di tutuloy
- try {
- System.out.print("Enter Employee ID: ");
- eid = sc.nextInt();
- sc.nextLine();
- System.out.print("Updated First Name: ");
- fName = sc.nextLine();
- System.out.print("Updated Last Name: ");
- lName = sc.nextLine();
- System.out.print("Updated Dept(ADG/XVN/RTG): ");
- dept = sc.nextLine();
- System.out.print("Updated Phone: ");
- phone = sc.nextLine();
- System.out.print("Updated Sex: ");
- sex = sc.nextLine();
- System.out.print("Updated Salary: ");
- salary = sc.nextLine();
- System.out.print("Updated Birthday(YYYY-MM-DD): ");
- bday = sc.nextLine();
- rowsAffected = callQuery.updateEmpInfo(fName, lName, dept
- , phone, sex, salary, bday, eid);
- } catch (Exception e) {
- }
- if (rowsAffected == 1) {
- System.out.println("Employee Info Updated");
- } else {
- System.out.println("Record Failed");
- }
- }
- }
- //---------------------------------------------
- package projectSamples;
- import java.sql.*;
- class Day26EmpMethods {
- // the 2 methods below are for update
- //method 1 to get a copy of the row
- public String[] getOneRow(int eid) {
- String[] rowResult = new String[8];
- String address = "jdbc:mysql://localhost:3306/db_mng_b2";
- String userName = "root";
- String passWord = "";
- String sqlQuery = "SELECT fld_enum"
- + ", fld_fname, fld_lname"
- + ", fld_work_dept, fld_phone"
- + ", fld_sex, fld_salary, fld_birthday"
- + " FROM tbl_employee"
- + " WHERE fld_enum = ?";
- try {
- Connection conn = DriverManager.getConnection(
- address,userName,passWord);
- PreparedStatement stmt = conn.prepareStatement(sqlQuery);
- stmt.setInt(1, eid);
- ResultSet rs = stmt.executeQuery();
- while (rs.next()) {
- rowResult[0] = rs.getString(1);
- rowResult[1] = rs.getString(2);
- rowResult[2] = rs.getString(3);
- rowResult[3] = rs.getString(4);
- rowResult[4] = rs.getString(5);
- rowResult[5] = rs.getString(6);
- rowResult[6] = rs.getString(7);
- rowResult[7] = rs.getString(8);
- }
- conn.close();
- } catch (Exception e) {
- System.out.println(e.getMessage());
- }
- return rowResult;
- }
- // method 2 - for updating
- public int updateEmpInfo(String fName, String lName, String dept
- ,String phone, String sex, String salary, String bday, int eid){
- String[] origValue = getOneRow(eid);
- double salaryForQuery;
- if (fName.isEmpty() || fName.trim().equals("")) {
- fName = origValue[1]; //galing sa select
- }
- if (lName.isEmpty() || lName.trim().equals("")) {
- lName = origValue[2];
- }
- if (dept.isEmpty() || dept.trim().equals("")) {
- dept = origValue[3];
- }
- if (phone.isEmpty() || phone.trim().equals("")) {
- phone = origValue[4];
- }
- if (sex.isEmpty() || sex.trim().equals("")) {
- sex = origValue[5];
- }
- if (salary.isEmpty() || salary.trim().equals("")) {
- salaryForQuery = Double.parseDouble(origValue[6]); //galing sa select
- }else {
- salaryForQuery = Double.parseDouble(salary); //gagamitin yung tinype ni user
- }
- if (bday.isEmpty() || bday.trim().equals("")) {
- bday = origValue[7]; //galing sa select
- }
- int rowsAffected = 0;
- String address = "jdbc:mysql://localhost:3306/db_mng_b2";
- String userName = "root";
- String passWord = "";
- String sqlQuery = "UPDATE tbl_employee"
- +" SET fld_fname = ?"
- +", fld_lname = ?"
- +", fld_work_dept = ?"
- +", fld_phone = ?"
- +", fld_sex = ?"
- +", fld_salary = ?"
- +", fld_birthday = ?"
- +" WHERE fld_enum = ?";
- try {
- Connection conn = DriverManager.getConnection(
- address,userName,passWord);
- PreparedStatement stmt = conn.prepareStatement(sqlQuery);
- stmt.setString(1, fName);
- stmt.setString(2, lName);
- stmt.setString(3, dept);
- stmt.setString(4, phone);
- stmt.setString(5, sex);
- stmt.setDouble(6, salaryForQuery);
- stmt.setString(7, bday);
- stmt.setInt(8, eid);
- rowsAffected = stmt.executeUpdate();
- conn.close();
- } catch (Exception e) {
- System.out.println(e.getMessage());
- }
- return rowsAffected;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment