Application of design patterns in Java Database Programming

Source: Internet
Author: User

Http://www.35java.com/zhibo/forum.php? MoD = viewthread & tid = 260 & extra = Page % 3d2

JDBC technology is the standard for database programming in Java. With the widespread rise of B/S in recent years, Java technology is increasingly reused on servers. As a key part of the information system, database programming is a must for programmers. The JDBC technology provides some available programming interfaces to process some basic transactions, but we also find that, if you simply use the programming interface provided in the JDBC specification to complete the transaction logic, Code High redundancy and low programming efficiency. Based on this, if, on the basis of the underlying interface, the appropriate design pattern is used for abstract encapsulation, and some specific designs are followed, A Scalable framework is formed on top of the JDBC standard interface. Development at this abstract level will greatly improve both reusability, reliability, and development efficiency. 1. Key points of the design model
The so-called mode is a solution to a specific problem in a specific situation. These solutions are often summed up by experience and are recognized as excellent. The design mode is a well-recognized design solution for specific problems in specific scenarios during the object-oriented software design. The object-oriented thinking has been around for so many years, and now there are a lot of specific models that deserve our reference when we encounter most problems in software design.
2. Key Points of JDBC Programming
The general programming process is as follows:
(1) obtain the database connection.
(2) perform specific SQL statement operations on the obtained connection.
If the above operations are performed on each database access, without some organizational design, the Code redundancy will be very high, Program The workload is also very large, and the code written is not flexible, so it is necessary to introduce a certain design pattern to improve code reusability and scalability.
3. Use the factory method mode to control the generation of database connections
In actual systems that have already been determined, the database is often rarely changed. Therefore, the first step to getting the database is the same for almost all accesses. In order to improve the reusability, it is necessary to extract it into a separate module. Here, only the factory method can be used to encapsulate the database connection operation. Considering the frequent connection, there is no need to request a connection to the database every time you access the database. We can use the connection pool technology to manage a certain number of database connections. Here we define a connectionpool class, the database connection operation is defined as newconnection ().
The following is the basic code snippet of the factory method:

Public
class connectionpool ......
private connection newconnection () {
......
connection con = drivermanager. getconnection (URL, user, password);
......
return con;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.