In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
What is the design pattern of database class in J2EE application? I believe many inexperienced people don't know what to do about it. This paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
When developing J2EE applications, we usually need to find out all kinds of information involved in the application, such as a company's product catalog or the user information of a website, which we will put in the database.
In the usual design, we have to analyze the attributes and relationships of these data, and then carry out the logical design of the database to store all kinds of information in different tables. For example, to develop a book information query system. You can create the following two tables to represent the book and the publisher, respectively.
Table Book (ID, Name, ISBN, Author, PublisherID, Price, Volume)
Table Publisher (ID, Name, Telephone, Address, Postcode)
Table Book contains ID, book title, book number, author, publisher ID, price, number of pages. Table Publisher contains ID, club name, phone number, address, and zip code. The two tables are related through the publisher ID.
Let's introduce a pattern for the design of database-related classes.
Database related classes can be divided into entity classes (Entity Class) and session classes (Session Class).
An entity class corresponds to the encapsulation of a table's record, that is, an instance of the class corresponds to a record in the table. Moreover, the properties in this class correspond to the fields in the record one by one.
Session classes correspond to operations on all records in a table. For example, add a record, delete a record, find a record, and update a record.
Through the use of this design pattern, the program is more modular and easy to develop and maintain. Of course, other design patterns can also be used.
Program realization
In the specific implementation of the above model, we often choose different technologies according to the specific application. After seeing the pattern described above, it is easy to find that it can be implemented in EJB (there are two types of EJB, entity EJB and session EJB).
We know that the purpose of EJB is to provide the development of a distributed component system. If our application is a distributed application system, then there is no doubt that using EJB to implement can greatly reduce the programming workload. At the same time, by using some advanced features of the EJB container, the application can be made more reliable and scalable. In this way, developers do not have to care about some of the underlying technologies, such as transaction processing, security and other aspects, but focus on how to implement business logic. However, we should note that if the application developed is not distributed, then the use of EJB may greatly reduce the performance of the system. Because EJB calls are expensive.
This article will explore how to implement the pattern described above without using EJB technology.
The following is to develop a book information query system as an example.
1. Entity class
As mentioned earlier, each instance of the entity class corresponds to a record in the table. In this way, the attributes of the entity class should correspond to each field of the table. It must be noted that the instance of the entity class corresponds to each record in memory, so the operation on the instance in the program is not immediately reflected in the database record.
In this class, it is just a wrapper for the data, so the class only needs some basic methods, namely the setXX () and getXX () methods.
The following is an entity class that encapsulates the Book table.
Class Book {protected intID; protected String Name; protected String ISBN; protected String Author; protected intPublisherID; protected double Price; protected intVolume; public void setID (int iID); public intgetID (); public void setName (String sName); public String getName (); public void setISBN (String sISBN); public String getISBN (); public void setAuthor (String sAuthor); public String getAuthor (); public void setPublisherID (int iID); public intgetPublisherID () Public void setPrice (double dPrice); public double getPrice (); public void setVolume (int iVolume); public intgetVolume (); public Book (int iID, String sName, String sISBN, int iPublisherID, double dPrice, int iVolume);}
Similarly, the table Publisher can be encapsulated.
two。 Conversation class
The session class mainly deals with a table. These actions can be to create a record in a table, delete a record, update a record, and find a record. The result of these operations is to correspond the record in the table to the instance of the entity class in memory, or the instance to the record in the table.
We can look at the encapsulation of the Book table.
Class BookTable {void Add (Book book); void Delete (Book book); void Update (Book book); Collection findbyID (int iID); Collection findbyXXXX (XX,XX); Collection findbyPulisherName (String sPublisherName);}
In the declaration of the class above, Add () is used to map a Book instance in memory to the database. Delete () is used to delete a record in the database. Update () is used to update a record in the table. FindbyXXXX () corresponds to the SELECT statement.
When multiple table operations are involved, there are two ways. One is that, like BookTable, it encapsulates a class. Another way is to write a findbyPublisherName () directly in BoolTable. This method is designed to return a collection of Book.
The above is just a brief introduction to how to implement entity classes and session classes. In specific applications, the consistency of database operations should also be taken into account. Let's introduce the relevant content of transaction processing.
Transaction processing
In order to ensure the integrity and consistency of data operations, the problem of transaction processing should be fully taken into account in programming.
How to combine multiple SQL statements into a single transaction in 1.JDBC.
In JDBC, when you open a connection object Connection, the default is auto-commit mode, and each SQL statement is treated as a transaction, that is, each time a statement is executed, the transaction is automatically confirmed. In order to combine multiple SQL statements into a single transaction, block the auto-commit schema.
After the auto-commit mode is blocked, the SQL statement will not get the transaction confirmation if the commit () method is not called. All SQL after the most recent call to the commit () method is confirmed when the method commit () is called.
The following code is an example:
Con.setAutoCommit (false); PreparedStatement updateSales=con.prepareStatement ("UPDATE COFFES SET SALES=?" WHERE COF_NAME LIKE?); updateSales.setInt (1pm 50); updateSales.setString (2, "Colombian"); updateSales.executeUpdate (); PreparedStatement updateTotal=con.prepareStatement ("UPDATE COFFEES SET TOTAL = TOTAL+? WHERE COD_NAME LIKE?); updateTotal.setInt (1pm 50); updateTotal.setString (2, "Colombian"); updateTotal.executeUpdate (); con.commit (0; con.setAutoCommit (true))
Distributed transaction processing in 2.J2EE
In J2EE, JTA can be used in a program to invoke the underlying JTS (Java Transaction Service provider Service) to handle distributed transactions. In addition, if you use EJB, you can do this by specifying the properties of transaction in the description file.
After reading the above, have you mastered the design pattern of database classes in J2EE applications? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.