|
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:
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 |
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.
![]() |
|---|
| Figure 4.4 - To generate the code |
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" .
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.
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();).
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.
In case there are any exception, you can call the rollback function to cancel all the operation (t.rollback());
The following is an example of using the transaction API.
Sample:
PersistentTransaction t = SchoolSystemPersistentManager.instance().getSession().beginTransaction();}
try {Course lCourse = CourseFactory.createCourse();} catch (Exception e) {
lCourse.setTitle(getTitleTextField().getText());
lCourse.setDescription(getDescriptionTextField().getText());
lCourse.setTeacher(_teacher);
_teacher.save();
t.commit();
return lCourse;e.printStackTrace();}
t.rollback();
return null;
Source File : School Project\system\dialog\AddCourseDialog.java
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.
![]() |
|---|
| Figure 4.6 - school system |
![]() |
|---|
| Figure 4.7 - Add Student dialog |
...
PersistentTransaction t = SchoolSystemPersistentManager.instance().getSession().beginTransaction();
_user = StudentFactory.createStudent();
Source File : School Project\system\dialog\AddCourseDialog.java
Source File : School Project\system\dialog\AddCourseDialog.java
_user.save();}catch (Exception e) {
t.commit();
e.printStackTrace();}
t.rollback();
Source File : School Project\system\dialog\AddCourseDialog.java
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 |
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
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.
![]() |
|---|
| Figure 4.10 - To Update Information |
![]() |
|---|
| Figure 4.11 - Update User Information |
...}
PersistentTransaction t = SchoolSystemPersistentManager.instance().getSession().beginTransaction();
try {if (_user != null) {} catch (Exception e) {_user.setName(_userNameField.getText());}
_user.setPassword(_passwordField.getText());
if (_user instanceof Teacher) {((Teacher) _user).setEmail(_emailField.getText());}
_user.save();
...
t.commit();e.printStackTrace();} finally {
t.rollback();this.setVisible(false);}
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.
![]() |
|---|
| Figure 4.12 - Add Course dialog |
![]() |
|---|
| Figure 4.13 - Register a course |
![]() |
|---|
| private void fireDeleteCourse() throws PersistentException { PersistentTransaction t = SchoolSystemPersistentManager.instance() .getSession().beginTransaction(); Source File : School System\src\system\courseInformationPane.java |
| try { _currentCourse.deleteAndDissociate(); _courseImpl.fireDeleteCourse(); t.commit(); } catch (Exception e) { e.printStackTrace(); t.rollback(); } Source File : School System\src\system\courseInformationPane.java |
|
|
|||||||