top of page

Crud Operation using JavaFX

In this example we are seeing how to develop CRUD (Create, Read, Update and Delete) operation application using technologies JavaFX.

In this example all operations are performed on Employee basic properties like employee id, employee name,department name,mobile number and employee salary . Application main aim is adding employee details to DB using user interface, and performing multiple operations like update, viewing and deleting.




Employee.fxml



<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>

<AnchorPane prefHeight="520.0" prefWidth="501.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.EmployeeController">
   <children>
      <Label layoutY="-1.0" prefHeight="60.0" prefWidth="501.0" style="-fx-background-color: white;" text="            Employee Management System" textFill="#172c90">
         <font>
            <Font size="25.0" />
         </font>
      </Label>
      <Label layoutX="46.0" layoutY="82.0" prefHeight="38.0" prefWidth="101.0" text=" Employee Name" />
      <Label layoutX="46.0" layoutY="130.0" prefHeight="38.0" prefWidth="101.0" text=" Department" />
      <Label layoutX="46.0" layoutY="173.0" prefHeight="38.0" prefWidth="101.0" text=" Mobile No." />
      <Label layoutX="46.0" layoutY="213.0" prefHeight="38.0" prefWidth="101.0" text=" Salary" />
      <TextField fx:id="ename" layoutX="194.0" layoutY="82.0" prefHeight="38.0" prefWidth="149.0" />
      <TextField fx:id="dept" layoutX="194.0" layoutY="130.0" prefHeight="38.0" prefWidth="149.0" />
      <TextField fx:id="mobNo" layoutX="194.0" layoutY="174.0" prefHeight="38.0" prefWidth="149.0" />
      <TextField fx:id="salary" layoutX="194.0" layoutY="219.0" prefHeight="38.0" prefWidth="149.0" />
      <Button layoutX="358.0" layoutY="278.0" mnemonicParsing="false" onAction="#register" prefHeight="38.0" prefWidth="101.0" text="Register" />
      <Label fx:id="lavel" layoutX="54.0" layoutY="278.0" prefHeight="38.0" prefWidth="235.0" />
      <Button layoutX="128.0" layoutY="334.0" mnemonicParsing="false" onAction="#deleteEmployee" prefHeight="38.0" prefWidth="120.0" text="Delete Empoyee" />
      <TextField fx:id="id" layoutX="6.0" layoutY="334.0" prefHeight="38.0" prefWidth="112.0" text="Enter Id for Delete" />
      <Button layoutX="269.0" layoutY="334.0" mnemonicParsing="false" onAction="#showAll" prefHeight="38.0" prefWidth="101.0" text="Show All" />
      <TableView fx:id="table" layoutX="-1.0" layoutY="380.0" prefHeight="155.0" prefWidth="501.0">
        <columns>
          <TableColumn fx:id="eid" prefWidth="75.0" text="Eid" />
          <TableColumn fx:id="name" prefWidth="105.0" text="Name" />
            <TableColumn fx:id="department" prefWidth="92.0" text="Department" />
            <TableColumn fx:id="salary1" prefWidth="92.0" text="salary1" />
            <TableColumn fx:id="mobile" prefWidth="95.0" text="mobile" />
        </columns>
      </TableView>
      <Button layoutX="391.0" layoutY="335.0" mnemonicParsing="false" onAction="#update" prefHeight="38.0" prefWidth="101.0" text="update" />
   </children>
</AnchorPane>

EmployeeController.java


package application;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;

public class EmployeeController {
	
		@FXML
		private TextField ename;
		@FXML
		private TextField dept;
		@FXML
		private TextField mobNo;
		@FXML
		private TextField salary;
		
		@FXML
		private Label lavel;
		
		@FXML
		private TableView<Employee> table;
		
		@FXML
		private TableColumn<Employee, Integer> eid;
		@FXML
		private TableColumn<Employee, String> name;
		@FXML
		private TableColumn<Employee, String> department;
		@FXML
		private TableColumn<Employee, Float> salary1;
		@FXML
		private TableColumn<Employee, String> mobile;
		
		@FXML 
		private TextField id;
		
		
		Employee emp;
		Connection con=null;
		ResultSet rs=null;
		PreparedStatement pstmt=null;
		// create table employee ( id number,name varchar2(15),department varchar2(15),mobileNo varchar2(10),salary number);
		public void register(ActionEvent ae){
			
			emp=new Employee();
			emp.setName(ename.getText());
			emp.setDepartment(dept.getText());
			emp.setMobileNo(mobNo.getText());
			emp.setSalary(Float.parseFloat(salary.getText()));
			
			if(emp.getMobileNo().length()<10 || emp.getMobileNo().length()>10){
				lavel.setText("please enter correct mobile No");
				return;
			}
			
			try {
				con=DBUtil.getConnection();
				String sql="insert into employee values((select nvl(max(id),0)+1 from employee),?,?,?,?)";
				pstmt=con.prepareStatement(sql);
				pstmt.setString(1,emp.getName());
				pstmt.setString(2,emp.getDepartment());
				pstmt.setString(3,emp.getMobileNo());
				pstmt.setFloat(4, emp.getSalary());
				rs=pstmt.executeQuery();
				if(rs.next()){
					lavel.setText("Register Sucessfully.");
					eid.setCellValueFactory(new PropertyValueFactory<Employee, Integer>("eid"));
					name.setCellValueFactory(new PropertyValueFactory<Employee, String>("name"));
					department.setCellValueFactory(new PropertyValueFactory<Employee, String>("department"));
					salary1.setCellValueFactory(new PropertyValueFactory<Employee, Float>("salary1"));
					mobile.setCellValueFactory(new PropertyValueFactory<Employee, String>("mobile"));
					ObservableList<Employee> data = FXCollections.observableArrayList(
					         new Employee(101,emp.getName(),emp.getDepartment(),123,"12312323")
					);
						
						table.getItems().addAll(data);

					
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
			System.out.println(emp.getSalary()+"...........");
		}
		public void showAll(ActionEvent ae){
			try {
				con =DBUtil.getConnection();
				String sql="select * from employee";
				pstmt=con.prepareStatement(sql);
				
				rs=pstmt.executeQuery();
				while(rs.next()){
					eid.setCellValueFactory(new PropertyValueFactory<Employee, Integer>("eid"));
					name.setCellValueFactory(new PropertyValueFactory<Employee, String>("name"));
					department.setCellValueFactory(new PropertyValueFactory<Employee, String>("department"));
					salary1.setCellValueFactory(new PropertyValueFactory<Employee, Float>("salary1"));
					mobile.setCellValueFactory(new PropertyValueFactory<Employee, String>("mobile"));
					ObservableList<Employee> data = FXCollections.observableArrayList(
					         new Employee(rs.getInt("id"),rs.getString("name"),rs.getString("department"),rs.getFloat("salary"),rs.getString("mobileNo"))
					);
					table.getItems().addAll(data);
				}
				
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		public void deleteEmployee(ActionEvent ae){
			try {
				con =DBUtil.getConnection();
				String sql="delete from employee where id=?";
				pstmt=con.prepareStatement(sql);
				pstmt.setInt(1, Integer.parseInt(id.getText()));
				rs=pstmt.executeQuery();
				if(rs!=null){
					lavel.setText("Record deleted ");
				}else{
					lavel.setText("please check employee id");
				}
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		public void update(ActionEvent ae) throws IOException{
			Stage primaryStage= new Stage();
			Parent root =FXMLLoader.load(getClass().getResource("/application/Update.fxml"));
//			Parent root = FXMLLoader.load(getClass().getResource(arg0))
			Scene scene = new Scene(root);
			scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
			primaryStage.setScene(scene);
			primaryStage.show();
		}
}


Employee.java
package application;

public class Employee {

	private int eid;
	private String name;
	private String department;
	private float salary;
	private String mobileNo;
	
	
	public Employee() {
		super();
	}

	public Employee(int eid, String name, String department, float salary,
			String mobileNo) {
		super();
		this.eid = eid;
		this.name = name;
		this.department = department;
		this.salary = salary;
		this.mobileNo = mobileNo;
	}


	public int getEid() {
		return eid;
	}


	public void setEid(int eid) {
		this.eid = eid;
	}


	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}


	public String getDepartment() {
		return department;
	}


	public void setDepartment(String department) {
		this.department = department;
	}


	public float getSalary() {
		return salary;
	}


	public void setSalary(float salary) {
		this.salary = salary;
	}


	public String getMobileNo() {
		return mobileNo;
	}


	public void setMobileNo(String mobileNo) {
		this.mobileNo = mobileNo;
	}

}

Update.fxml


<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>


<AnchorPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/11.0.1" fx:controller="application.UpdateController">
   <children>
      <AnchorPane layoutY="65.0" prefHeight="535.0" prefWidth="511.0">
         <children>
            <Label layoutY="-66.0" prefHeight="61.0" prefWidth="511.0" style="-fx-background-color: white;" text="            Employee Management System" textFill="#172c90">
               <font>
                  <Font size="25.0" />
               </font>
            </Label>
            <Label layoutX="46.0" layoutY="82.0" prefHeight="38.0" prefWidth="101.0" text=" Employee Name" />
            <Label layoutX="46.0" layoutY="130.0" prefHeight="38.0" prefWidth="101.0" text=" Department" />
            <Label layoutX="46.0" layoutY="173.0" prefHeight="38.0" prefWidth="101.0" text=" Mobile No." />
            <Label layoutX="46.0" layoutY="213.0" prefHeight="38.0" prefWidth="101.0" text=" Salary" />
            <TextField fx:id="ename" layoutX="194.0" layoutY="82.0" prefHeight="38.0" prefWidth="149.0" />
            <TextField fx:id="dept" layoutX="194.0" layoutY="130.0" prefHeight="38.0" prefWidth="149.0" />
            <TextField fx:id="mobNo" layoutX="194.0" layoutY="174.0" prefHeight="38.0" prefWidth="149.0" />
            <TextField fx:id="salary" layoutX="194.0" layoutY="219.0" prefHeight="38.0" prefWidth="149.0" />
            <Button layoutX="358.0" layoutY="278.0" mnemonicParsing="false" onAction="#updateEmployee" prefHeight="38.0" prefWidth="101.0" text="update" />
            <Label fx:id="lavel" layoutX="54.0" layoutY="278.0" prefHeight="38.0" prefWidth="235.0" />
            <TableView fx:id="table" layoutX="-1.0" layoutY="380.0" prefHeight="155.0" prefWidth="501.0">
               <columns>
                  <TableColumn fx:id="eid" prefWidth="75.0" text="Eid" />
                  <TableColumn fx:id="name" prefWidth="105.0" text="Name" />
                  <TableColumn fx:id="department" prefWidth="92.0" text="Department" />
                  <TableColumn fx:id="salary1" prefWidth="92.0" text="salary1" />
                  <TableColumn fx:id="mobile" prefWidth="95.0" text="mobile" />
               </columns>
            </TableView>
            <TextField fx:id="id" layoutX="194.0" layoutY="28.0" prefHeight="38.0" prefWidth="149.0" />
            <Label layoutX="46.0" layoutY="27.0" prefHeight="38.0" prefWidth="101.0" text=" Employee Id" />
         </children>
      </AnchorPane>
   </children>
</AnchorPane>

UpdateController.java

package application;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;

public class UpdateController {
	@FXML
	private TextField ename;
	@FXML
	private TextField dept;
	@FXML
	private TextField mobNo;
	@FXML
	private TextField salary;
	@FXML 
	private TextField id;
	
	@FXML
	private Label lavel;
	
	@FXML
	private TableView<Employee> table;
	
	@FXML
	private TableColumn<Employee, Integer> eid;
	@FXML
	private TableColumn<Employee, String> name;
	@FXML
	private TableColumn<Employee, String> department;
	@FXML
	private TableColumn<Employee, Float> salary1;
	@FXML
	private TableColumn<Employee, String> mobile;
	
	
	Employee emp;
	Connection con=null;
	ResultSet rs=null;
	PreparedStatement pstmt=null;
	
	public void updateEmployee(ActionEvent ae){
		emp=new Employee();
		emp.setEid(Integer.parseInt(id.getText()));
		emp.setName(ename.getText());
		emp.setDepartment(dept.getText());
		emp.setMobileNo(mobNo.getText());
		emp.setSalary(Float.parseFloat(salary.getText()));
		
		if(emp.getMobileNo().length()<10 || emp.getMobileNo().length()>10){
			lavel.setText("please enter correct mobile No");
			return;
		}
		
		try {
			con=DBUtil.getConnection();
			String sql="update employee set name=?,department=?,mobileno=?,salary=? where id=?";
			pstmt=con.prepareStatement(sql);
			pstmt.setString(1,emp.getName());
			pstmt.setString(2,emp.getDepartment());
			pstmt.setString(3,emp.getMobileNo());
			pstmt.setFloat(4, emp.getSalary());
			pstmt.setInt(5, emp.getEid());
			rs=pstmt.executeQuery();
			if(rs.next()){
				lavel.setText("Update Sucessfully.");
				eid.setCellValueFactory(new PropertyValueFactory<Employee, Integer>("eid"));
				name.setCellValueFactory(new PropertyValueFactory<Employee, String>("name"));
				department.setCellValueFactory(new PropertyValueFactory<Employee, String>("department"));
				salary1.setCellValueFactory(new PropertyValueFactory<Employee, Float>("salary1"));
				mobile.setCellValueFactory(new PropertyValueFactory<Employee, String>("mobile"));
				ObservableList<Employee> data = FXCollections.observableArrayList(
				         new Employee(emp.getEid(),emp.getName(),emp.getDepartment(),emp.getSalary(),emp.getMobileNo())
				);
					
					table.getItems().addAll(data);	
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}

DBUtil.java
package application;

import java.sql.Connection;

import java.sql.DriverManager;
import java.sql.SQLException;
public final class DBUtil {
		private static boolean isDriverLoaded = false;
		static{
			try{
				Class.forName("oracle.jdbc.driver.OracleDriver");
				System.out.println("Driver Loaded");
				isDriverLoaded = true;	
			}catch(ClassNotFoundException e){
				e.printStackTrace();
			}
		}
		
		private final static String url="jdbc:oracle:thin:@localhost:1521:XE";
		private final static String user="SYSTEM";
		private final static String password="system";
		
		public static Connection getConnection() throws SQLException{
			Connection con = null;
			if(isDriverLoaded){
				con  = DriverManager.getConnection(url,user,password);
				System.out.println("Connection established");
			}
			return con;
		}
		
		
		public static void closeConnection(Connection con) throws SQLException{
			if(con!=null){
				con.close();
				System.out.println("connection closed");
			}
		}	
}

Main.java

package application;
	
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;


public class Main extends Application {
	@Override
	public void start(Stage primaryStage) {
		try {
			Parent root = FXMLLoader.load(getClass().getResource("/application/Employee.fxml"));
			Scene scene = new Scene(root);
			scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
			primaryStage.setScene(scene);
			primaryStage.show();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		launch(args);
	}
}


800 views0 comments

Recent Posts

See All
bottom of page