Previous Next
Programmers Guide for Java Chapter 4 - Developing Standalone Java Application

Chapter 4 - Developing Standalone Java Application

With DB Visual ARCHITECT (DB-VA) you can develop quality Standalone Java Application much faster, better and cheaper. DB-VA generates all Java code for accessing database. You do not need to write SQL to insert, query, update or delete the record. All code you need to program is plain Java code (e.g. OrderDAO.save(myOrder);). In this chapter we will use a simple "School System" application to show you how to generate Java code, create standalone Java application, and create/query/update/delete objects. Again you do not need to write a single SQL statement for all the above operations.

Figure 4.1 - Develop Standalone Java Application with DB Visual ARCHITECT

The architecture of Standalone Java Application with DB-VA Persistent Layer

In this chapter:

Introduction

You will develop a School System.

The School System provides the following functions:

Required Software:

Please open the SchoolSystem.vpp project file in the "Chapter 4 Standalone Java School System.zip" file. The project file contains the following diagrams. For the details about how to draw class diagram and entity relationship diagram, please see the Designer's Guide.

Class Diagram of School System:

Figure 4.2 - Class Diagram of School System

Entity Relationship Diagram of School System:

Figure 4.3 - ERD of School System

Generating Java Source and Database

In order to develop Java application, you need to generate code by DB-VA and configure the source code and library. The following example is a standalone Java application, so please pay attention to the following settings when configure to generate Java code.

  1. From menu bar, select Tools > Object-Relational Mapping (ORM) > Generate Code... to open the Database Code Generation dialog box.
  2. Figure 4.4 - To generate the code
  3. Fill in code generation information.
  4. For Language, select Java.
    For Association Handling, select Smart.
    For Persistent API, select Factory Class.

    Figure 4.5 - Database Code Generation dialog

For the other configuration details, please refer to Chapter 1 "Generate Java Code, Database Schema (DDL) and persistent Library" and Chapter 2 "Configure Source and Library in Eclipse" .

Using PersistentManager and Transaction

There is a PersistentManager (Class) in DB-VA generated code. The PersistentManager is used to manage the database connection, state of the persistent objects and transaction when the application is running.

A database transaction is a unit for the application. The database transaction is used to keep integrity of your data in database. DB-VA Persistent Library provides a transaction API for you to create, commit and rollback the transaction for your application.

Start Transaction

When you need to start a transaction, you just need to get the session from the PersistentManager and call the beginTransaction function (PersistentManager.instance().getSession().beginTransaction();).

PersistentTransaction t = PersistentManager.instance().getSession().beginTransaction();

Commit Transaction

After you inserted/updated or deleted the data, you can call the commit function of the transaction object (t.commit()) to commit your changes to Database.

t.commit();

Rollback Transaction

In case there are any exception, you can call the rollback function to cancel all the operation (t.rollback());

t.rollback();

The following is an example of using the transaction API.

Sample:

private Course fireOK() throws PersistentException {
PersistentTransaction t = SchoolSystemPersistentManager.instance().getSession().beginTransaction();
try {
Course lCourse = CourseFactory.createCourse();
lCourse.setTitle(getTitleTextField().getText());
lCourse.setDescription(getDescriptionTextField().getText());
lCourse.setTeacher(_teacher);
_teacher.save();
t.commit();
return lCourse;
} catch (Exception e) {
e.printStackTrace();
t.rollback();
}
return null;
}

Source File : School Project\system\dialog\AddCourseDialog.java

Creating Object and Saving to Database

In this chapter we will use a simple Swing application called "School System" to illustrate the application of the persistent API. If you are interested in this application, please browse the source code inside the "Chapter 4 Standalone Java School System.zip" zip file.

The school system provides the register function for teacher and student. They need to enter login id, password etc... information for system. The registration process is same for teacher and student, so we will only demonstrate how to create the student and save to database.

  1. From menu bar, select File > Register Student to open the Add Student dialog box.
  2. Figure 4.6 - school system
  3. Enter the Student information; click OK to create the new Student record in School System.
  4. Figure 4.7 - Add Student dialog
  5. After click OK, the system create the new Student Persistent object
  6. private void fireOK() throws PersistentException {
    ...
    PersistentTransaction t = SchoolSystemPersistentManager.instance().getSession().beginTransaction();
    _user = StudentFactory.createStudent();

    Source File : School Project\system\dialog\AddCourseDialog.java

  7. Set the student information from the text fields value to the Student Object
  8. ((Student) _user).setEnrolmentDate(new Date());
    _user.setLoginID(_loginIDField.getText());
    _user.setName(_userNameField.getText());
    _user.setPassword(_passwordField.getText());

    Source File : School Project\system\dialog\AddCourseDialog.java

  9. Call save() method of Student Persistent Object and commit() method of PersistentTransaction., then the new Student object will be recorded in database. If any error occurred during the transaction, you can call the rollback() method to cancel all proposed changes.
  10. _user.save();
    t.commit();
    }catch (Exception e) {
    e.printStackTrace();
    t.rollback();
    }

    Source File : School Project\system\dialog\AddCourseDialog.java

Querying Object from Database

After the user login to the School System, the system queries different Course objects from the database according to user role. If the user is a student, the system shows all the available courses. The student can select and register the course. If the user is teacher, the system shows courses that are created by that teacher. The teacher can update or delete the course information in system.

Student Login:

Figure 4.8 - Student login to the system

Teacher Login:

Figure 4.9 - Teacher login to the system
  1. Query the course objects when user login. When Student login, the system will call listCourseByQuery() method in CouseFactory to get all available courses. When Teacher login, the system will call courses collection variable in Teacher object.
  2. private void updateCourseTree() throws PersistentException{
    try {
    Course[] courses = null;
    if (_currentUser instanceof Student) {
    courses = CourseFactory.listCourseByQuery(null, null);
    } else if (_currentUser instanceof Teacher) {
    courses = ((Teacher) _currentUser).courses.toArray();
    }
    ...
    }

    Source File : School Project\system\SchoolSystemFrame.java

Updating Object and Save to Database

You can modify the user information and update the record in database. After the user login, the User object is stored in the application, so you can set new information in the user object and update the database record.

  1. From menu bar, select User > Update Information to open the Update User Information dialog box.
  2. Figure 4.10 - To Update Information
  3. Enter new user information and click OK to update the User record.
  4. Figure 4.11 - Update User Information
  5. Update the information for the User object includes set password, name and email address. The process is simple with the create User object process.
  6. private void fireOK() throws PersistentException {
    ...
    PersistentTransaction t = SchoolSystemPersistentManager.instance().getSession().beginTransaction();
    try {
    if (_user != null) {
    _user.setName(_userNameField.getText());
    _user.setPassword(_passwordField.getText());
    if (_user instanceof Teacher) {
    ((Teacher) _user).setEmail(_emailField.getText());
    }
    _user.save();
    }
    ...
    t.commit();
    } catch (Exception e) {
    e.printStackTrace();
    t.rollback();
    } finally {
    this.setVisible(false);
    }
    }

Deleting Object in Database

Teacher can create course for students to registration and they can cancel the course in the system. They only need to click Delete then the course information will be deleted in the database and all its relationships with register students will be removed.

  1. Teacher can create the course by clicking Add Course. Fill in Course name and Description to create Course.
  2. Figure 4.12 - Add Course dialog
  3. Student can register the course by clicking Register.
  4. Figure 4.13 - Register a course
  5. After the student is registered, the teacher can view how many students have registered his course and he can delete the course in system.
  6. Click Delete then it will pass the Course object to the fireDeleteCourse() method .
  7. private void fireDeleteCourse() throws PersistentException {

    PersistentTransaction t = SchoolSystemPersistentManager.instance()

    .getSession().beginTransaction();

    Source File : School System\src\system\courseInformationPane.java
    Call deleteAndDissociate() method to delete the course object and remove the relationship of student and teacher.
    try {

    _currentCourse.deleteAndDissociate();

    _courseImpl.fireDeleteCourse();

    t.commit();

    } catch (Exception e) {

    e.printStackTrace();

    t.rollback();

    }

    Source File : School System\src\system\courseInformationPane.java

Previous Next
Visual Paradigm International Limited
Website: www.visual-paradigm.com
E-mail: support@visual-paradigm.com