Check if employee already exists

1

I am inserting data into a table in an Oracle sqldeveloper database and I want to check some things like (that the employee number already exists or that it is not entered correctly, that the boss does not exist, that the salary> 0, and that there is no department code) among others but I do not know how to check what the user has entered with what is in the table before entering this data. This is my code to enter the data:

int emp_no, dir, dept_no;
    float salario, comision;
    String apellido, oficio, fecha;

    String insertEmployee = "INSERT INTO EMPLEADOS (?,?,?,?,?,?,?,?)";
    PreparedStatement pstmt1 = con.prepareStatement(insertEmployee);

    System.out.println("Indica el numero de treballador: ");
    emp_no = keyboard.nextInt();
    System.out.println("Indica el cognom del treballador: ");
    keyboard.nextLine();
    apellido = keyboard.nextLine();
    System.out.println("Indica l'ofici del treballador: ");
    oficio = keyboard.nextLine();
    System.out.println("Indica el numero del director del treballador: ");
    dir = keyboard.nextInt();
    System.out.println("Indica el dia quan es va contractar el treballador: ");
    keyboard.nextLine();
    fecha = keyboard.nextLine();
    sdf.parse(fecha);
    System.out.println("Indica el salari del treballador: ");
    salario = keyboard.nextFloat();
    System.out.println("Indica la comissio del treballador: ");
    comision = keyboard.nextFloat();
    System.out.println("Indica el numero de departament al que pertany el treballador: ");
    dept_no = keyboard.nextInt();

    pstmt1.setInt(1, emp_no);
    pstmt1.setString(2, apellido);
    pstmt1.setString(3, oficio);
    pstmt1.setInt(4, dir);
    pstmt1.setString(5, fecha);
    pstmt1.setFloat(6, salario);
    pstmt1.setFloat(7, comision);
    pstmt1.setInt(8, dept_no);

    pstmt1.executeUpdate();
    con.commit();}
    
asked by Pau Bacardit Agset 22.06.2018 в 09:47
source

1 answer

1

In the end with the advice of all I have managed to solve it with a sentence and continued using resultset.next () since if I found the employee number I would skip the resultset.next () I used a boolean to indicate whether or not I could add the user.

int emp_no, dir, dept_no;
    float salario, comision;
    String apellido, oficio, fecha;

    String insertEmployee = "INSERT INTO EMPLEADOS values (?,?,?,?,?,?,?,?)";
    PreparedStatement pstmt1 = con.prepareStatement(insertEmployee);

    System.out.println("Indica el numero de treballador: ");
    emp_no = keyboard.nextInt();
    System.out.println("Indica el cognom del treballador: ");
    keyboard.nextLine();
    apellido = keyboard.nextLine();
    System.out.println("Indica l'ofici del treballador: ");
    oficio = keyboard.nextLine();
    System.out.println("Indica el numero del director del treballador: ");
    dir = keyboard.nextInt();
    System.out.println("Indica el dia quan es va contractar el treballador: ");
    keyboard.nextLine();
    fecha = keyboard.nextLine();
    sdf.parse(fecha);
    System.out.println("Indica el salari del treballador: ");
    salario = keyboard.nextFloat();
    System.out.println("Indica la comissio del treballador: ");
    comision = keyboard.nextFloat();
    System.out.println("Indica el numero de departament al que pertany el treballador: ");
    dept_no = keyboard.nextInt();
    Statement statement = con.createStatement();
    ResultSet checkEmp = statement.executeQuery("SELECT EMP_NO FROM EMPLEADOS WHERE EMP_NO = " + emp_no);
    boolean b = false; 
    while(checkEmp.next()){
        b= true;
    }
    if(b == false){
    pstmt1.setInt(1, emp_no);         
    pstmt1.setString(2, apellido);
    pstmt1.setString(3, oficio);
    pstmt1.setInt(4, dir);
    pstmt1.setString(5, fecha);
    pstmt1.setFloat(6, salario);
    pstmt1.setFloat(7, comision);
    pstmt1.setInt(8, dept_no);
    pstmt1.executeUpdate();
    con.commit();
    }else System.out.println("no s'ha pogut afegir el treballador");
    
answered by 22.06.2018 в 13:16