Welcome to the Java Community Forum and interact with 2 million technical staff. Ask questions. Currently, almost all application systems cannot avoid using database systems. It is very easy to access the database in the JAVA World. JDBC provides a unified interface for JAVA applications to access the database. By using the JDBC interface, developers do not need to worry about the system.
Welcome to the Java Community Forum, interact with 2 million technical staff> ask questions. Currently, almost all application systems cannot avoid using database systems. It is very easy to access the database in the JAVA World. JDBC provides a unified interface for JAVA applications to access the database. By using the JDBC interface, developers do not need to worry about the system.
Welcome to the Java Community Forum and interact with 2 million technical staff> enter
Problem proposal
Currently, almost all application systems cannot avoid using database systems. Accessing a database in the JAVA World is very easy. JDBC provides a unified interface for JAVA applications to access the database, by using the JDBC interface, developers do not need to worry about which database the system uses. Because JDBC only defines several JAVA interface classes, the specific implementation is provided by the database vendor, this method is similar to other database connection methods such as ODBC. However, in the actual application process, the developer finds that there is still a certain distance from the original intention of the JDBC design. For example, the encoding problem during string storage. I think many developers will encounter this problem, it is not because it is difficult to solve technical problems, but because it is indeed very complicated. We must perform encoding and anti-encoding each time when writing or reading strings; or we can write a method that can be encoded, but it must be called every time the database is operated, although the call is simple, do I have to do this? If you forget the encoding, DEBUG again. Of course, you may think that is nothing, or you may be very diligent and like to write a lot of repeated code, but do you not think that such tedious work is wasting your precious youth? Stop your keyboard input. let's solve this problem!
Solution
In traditional applications, we can think of two layers of database operations: one is the database's "connection pool" and the other is the business data operation layer. Here, the database connection pool is generalized. You can also regard DriverManager in JDBC as a connection pool, specifically, we can use this layer to obtain the connection to the specified database without worrying about how it is obtained. If the database system (such as Informix and SQL Server) requires that strings be transcoded before they can be stored (for example, the most common GBK-> ISO8859_1 transcoding ), then we have to do it at the business data operation layer. In this way, we have to perform Code transcoding for how many business data operations we have to perform. This is too troublesome and the code is filled with a lot of repeated content. The solution proposed in this article is to encapsulate the obtained database connection instance, that is, to add a connection encapsulation layer between the database connection pool and the business data operation layer. Of course, we can also directly encapsulate the connection into the database connection pool. For more information about how to implement the connection pool, see my other article "using JAVA Dynamic proxy for database connection pool".
We know that the encoding and transcoding work is concentrated on the two JDBC interfaces PreparedStatement and ResultSet, mainly involving the setString method of PreparedStatement and the getString method of ResultSet. We mentioned earlier that we need to add a connection encapsulation layer to encapsulate the database connection instance twice. But how can we change the PreparedStatement and ResultSet interfaces through this encapsulation? This problem is actually quite simple, because the PreparedStatement interface must obtain the instance through the Connection interface, and the ResultSet interface must obtain the instance from the Statement or PreparedStatement interface. With this cascade relationship, the problem is solved. I also used the dynamic interface proxy technology in my article "using JAVA Dynamic proxy to implement database connection pool. First, we design the Connection interface's proxy class _ Connection, which takes over all the methods in the Connection interface that may obtain the Statement or PreparedStatement interface instance, such as prepareStatement and createStatement. Change these two methods to make them return the Statement or PreparedStatement instance after taking over. There is also a corresponding proxy class _ Statement for the Statement interface. This proxy class takes over all the methods used to obtain the ResultSet interface instance, including taking over the setString method to determine whether to encode the string. It is relatively simple to take over the class _ ResultSet of the ResultSet interface. It only needs to process the getString method.
[1] [2] [3]