This post covers example of JPA with JavaFX. Student Management System is implemented and shown here.Controller and designing is in JavaFX. And at Data base side, JPA is in  role.

In the example,There is Entity file named Student which has properties like Roll NO, Name, Marks.Crud operations are performed for this entity.User can perform Crud operations from FXML file. StartUP file and Controller file also created separately so that code can remain clean.

Prerequisites:Knowledge of JPA and JavaFX.
Technical Specification:

I have used Eclipse And MySql.

Database schema name is StuJpaDbFX. Before running the project, please make sure that you have created schema. And verify data base details(like port no, user name password ) in xml file.

So lets get started.

Here is project hierarchy:

Project Hierarchy JPA with JavaFX

Project Hierarchy JPA with JavaFX

 

Here is FXML File:

FXML File JPA with JavaFX

FXML File JPA with JavaFX

Coding of each file is as follows:

StartUpFile.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
 
public class StartUpFile extends Application {
 
       @Override
       public void start(Stage primaryStage) {
              try
              {
                     FXMLLoader loader = new FXMLLoader(getClass().getResource("CreateStudentEntity.fxml"));
                     AnchorPane login = (AnchorPane) loader.load();
                     Scene scene = new Scene(login);      
               primaryStage.setTitle("Student Managment System");
               primaryStage.setScene(scene);
               primaryStage.show();
 
           }
              catch(Exception e)
              {
                     e.printStackTrace();
              }
 
       }
 
       public static void main(String[] args) {
              launch(args);
       }
}

Here is Controller file
StudentController.java :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package learn_java_Controller;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import learn_java_Services.*;
 
public class StudentController
{
 
       @FXML
       protected TextArea addDetailstxtName,addDetailstxtMarks,addDetailstxtRollNo,editDetailstxtName,editDetailstxtMarks;
 
       @FXML protected void btnAddStuPerform(ActionEvent event)
         {
              CreateStudent obj=new CreateStudent();
              obj.createStudent(Integer.parseInt(addDetailstxtRollNo.textProperty().getValue()),addDetailstxtName.textProperty().getValue(),Double.parseDouble( addDetailstxtMarks.textProperty().getValue()));
     }
 
       @FXML protected void btnUpdateStuPerform(ActionEvent event)
         {
              UpdateStudent obj=new UpdateStudent();
              obj.updateStudent(Integer.parseInt(addDetailstxtRollNo.textProperty().getValue()),addDetailstxtName.textProperty().getValue(),Double.parseDouble( addDetailstxtMarks.textProperty().getValue()));
   }
 
       @FXML protected void btnDeleteStuPerform(ActionEvent event)
         {
              DeleteStudent obj=new DeleteStudent();
              obj.deleteStudent(Integer.parseInt(addDetailstxtRollNo.textProperty().getValue()));
      }
 
}
}

Here is Entity file

Student.java :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package learn_java_Entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Student
{
 
          @Id
          @GeneratedValue(strategy = GenerationType.AUTO)
 
          private int rollno;
          private String sName;
          private double marks;
 
 
          public Student(int rollno,String sName, double marks)
          {
             super( );
             this.rollno=rollno;
             this.sName = sName;
             this.marks = marks;
 
          }
 
          public Student( ) {
             super();
          }
 
          public String getsName( ) {
             return sName;
          }
 
          public void setsName(String  sName) {
             this.sName = sName;
          }
 
          public double getMarks( ) {
             return marks;
          }
 
          public void setMarks(double marks) {
             this.marks = marks;
          }
 
          public int getRollno( ) {
                    return rollno;
                 }
 
                 public void setRollno(int rollno) {
                    this.rollno = rollno;
                 }
 
 
          @Override
          public String toString() {
             return "Employee [Student name=" + sName + ", marks=" + marks + "]";
          }
}

There are three services file :
CreateStudent
DeleteStudent
UpdateStudent

CreateStudent.java :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package learn_java_Services;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import learn_java_Entity.Student;
 
public class CreateStudent
{
       public void createStudent(int rollno,String sName,double marks)
       {
                EntityManagerFactory emfactory = Persistence.createEntityManagerFactory( "StudentManagmentSystem" );
 
             EntityManager entitymanager = emfactory.createEntityManager( );
             entitymanager.getTransaction( ).begin( );
 
             Student stu = new Student( );
             stu.setRollno(rollno);
             stu.setsName( sName );
             stu.setMarks( marks );
 
             entitymanager.persist( stu );
             entitymanager.getTransaction( ).commit( );
 
             entitymanager.close( );
             emfactory.close( );
 
       }  
}

DeleteStudent.java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package learn_java_Services;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import learn_java_Entity.*;
public class DeleteStudent
{
 
       public void deleteStudent(int rollno ) {
 
             EntityManagerFactory emfactory = Persistence.createEntityManagerFactory( "StudentManagmentSystem" );
             EntityManager entitymanager = emfactory.createEntityManager( );
             entitymanager.getTransaction( ).begin( );
 
             Student stu = entitymanager.find( Student.class, rollno );
             entitymanager.remove( stu );
             entitymanager.getTransaction( ).commit( );
             entitymanager.close( );
             emfactory.close( );
          }
 
}

UpdateStudent.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package learn_java_Services;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
 
import learn_java_Entity.*;;
public class UpdateStudent
{
       public void updateStudent(int rollno,String sName,double marks)
       {
              EntityManagerFactory emfactory = Persistence.createEntityManagerFactory( "StudentManagmentSystem" );
 
             EntityManager entitymanager = emfactory.createEntityManager( );
             entitymanager.getTransaction( ).begin( );
             Student stu = entitymanager.find( Student.class, rollno );
 
             //before update
             System.out.println( stu );
             stu.setsName(sName);
             stu.setMarks(marks);
             entitymanager.getTransaction( ).commit( );
 
             //after update
             System.out.println( stu );
             entitymanager.close();
             emfactory.close();
       }
       public static void main( String[ ] args ) {
 
          }
}

So When you run the program, FXML file will be loaded.you can perform respective CRUD operation.At back end Side JPA will be doing its work.
Hope this example helped you.Please let us know your view in comments section.

See other JavaFX Examples.Like Image Editor with JavaFX,JavaFX Animation,Sine Wave etc.

Save lots of $$.Signup for free Udemy courses.!

About

Leave a Reply

Your email address will not be published. Required fields are marked *

Name *
Email *
Website

%d bloggers like this: