Author: Zhao Yang pseudonym: bootcool
[bootcool @ 263.net, bootcool @ 163.net]
Beautiful and warm Kunming is actually very suitable for calm thinking
Further improve the performance of JDBC applications (2)
Second: Do balance between object-oriented programming and improved program performance
When we design the structure of the database application, you often need to map the object model into a table in a database, or reverse the table back to the object model. One basic principle for object-oriented is to make every object with a common feature in the real world to meet our thinking habits.
For example, there is such a student table in the database:
Id
Name
Department
Position
Address
1
Zhang San
Physics
member
Kunming
Now, we need to map the table from the database to the user. Based on the above principles, we consider encapsulating a class (here we temporarily call it for the record set, of course, the map of the student watch, refers to the Student class) And draw a mapping scheme following object-oriented design principles:
(Figure 3 - Scenario 1)
We know that a main feature of the database application is to reappear to the user in some form to query the results from the database. In order to meet the user's habits, our program uses JTABLE in the Java Swing package as an interface interacting between users and programs. What we have to do now is to make sure that the program map data is accurate and fast.
We use AbstractTableModel (Abstract to view JDK1.3 DOC) as an interface of mapping data, of course, the core of the user and program interaction. We know that a table model can be abstract into a two-dimensional object array, or a data structure such as a two-dimensional vector. So, we have another data structure-based mapping scheme:
(Figure 4 - Program 2)
Which way is to choose? We are not difficult to conclusions by comparison of the following table.
Mapping scheme
Face object
Encapsulation
Easy-to-understand
flexibility
Interface easy to achieve
Small work, good performance
plan 1
ü
ü
ü
Scenario 2
ü
ü
ü
(Table 4)
Note 1: The surface of the program 1 seems to follow the object-oriented principle, but:
(1): The workload is obviously larger than the program 2.
(2): Result set lost flexibility because of class packages.
(3): The intermediate conversion will reduce the performance of the program.
Description 2: But the plan 1 is the best choice in many cases. Because, when the user interacts with the program through the web, the renewal of the result set is necessary.
Summary: In the author's program, in order to improve the performance of the program, the object-oriented programming principle is not abandoned. The mapping scheme is obtained in reverse in the characteristics of JTABLE and ABSTRACTTABLEMODEL API. Sometimes the method of the API is often available, often can improve the performance of the program. Of course, this choice is mainly depends on the actual requirements and the structural design of the entire system, we should consider from different angles, and finally make a balance.
Zhao Yang: 2002.3.31
Reference article:
Http://java.sun.com/j2se/1.3/docs/guide/jdbc/getstart/gettingstartedtoc.fm.html