All the following code examples are taken from the Book source code of Li Xinghua's "Java Web development practices". I think the design is good, so I extract the code and take notes.
First, we define the structure type to be stored in a java file:
setEmpno(.empno = .ename = .job = .hiredate = setSal(.sal =
Next we define a database connection class to initiate a connection to the database. Java to connect to the database needs driver package, we can download, testing I use mysql-connector-java-5.0.5-bin.jar. When running the program, Eclipse will prompt the database driver package to be loaded, which is similar to "org. gjt. mm. mysql. driver "and other standard packages. Generally, we can select the Driver package in the working directory.
String DBDRIVER = "org.gjt.mm.mysql.Driver" String DBURL = "jdbc:mysql://localhost:3306/mldn" String DBUSER = "root" String DBPASSWORD = "root" DatabaseConnection() .conn = close() (.conn !=
Next, we define an interface that helps us easily implement proxy methods. There are only three methods in the interface: insert, search all, and search by ID.
java.util.* doCreate(Emp emp) List<Emp> findAll(String keyWord) Emp findById( empno)
Then, we inherit this interface to implement the operation classes of specific databases, which are basic database operations. It is important to note that the constructor uses the Connection object for the parameter. When using this class later, you must pass in the Connection object returned by the getConnection () function in the database Connection class DatabaseConnection defined earlier.
java.sql.* EmpDAOImpl Connection conn = PreparedStatement pstmt = .conn = doCreate(Emp emp) flag = = "INSERT INTO emp(empno,ename,job,hiredate,sal) VALUES(?,?,?,?,?)".pstmt = .pstmt.setInt(1.pstmt.setString(2.pstmt.setString(3.pstmt.setDate(4,.pstmt.setFloat(5(.pstmt.executeUpdate() > 0= List<Emp> findAll(String keyWord) <Emp> all = ArrayList<Emp>= "SELECT empno,ename,job,hiredate,sal FROM emp WHERE ename LIKE ? OR job LIKE ?".pstmt = .pstmt.setString(1,"%"+keyWord+"%"); .pstmt.setString(2,"%"+keyWord+"%"= = = 12345 Emp findById( empno) = = "SELECT empno,ename,job,hiredate,sal FROM emp WHERE empno=?".pstmt = .pstmt.setInt(1= = 12345
Let's take a look at the implementation of the proxy class. I personally think it is interesting here. In this class, it declares a database connection DatabaseConnection object, a database application class EmpDAOImpl object, and uses the DatabaseConnection object to initialize the EmpDAOImpl object, then, each function in the proxy class uses the EmpDAOImpl object to call methods inherited from the same interface, which hides the specific implementation methods to a certain extent.
java.sql.* EmpDAOProxy DatabaseConnection dbc = EmpDAOImpl dao = EmpDAOProxy() .dbc = .dao = EmpDAOImpl( doCreate(Emp emp) flag = (.dao.findById(emp.getEmpno()) == = List<Emp> findAll(String keyWord) <Emp> all = = Emp findById( empno) = =
This is not all. We can add a factory class to implement the factory model:
IEmpDAO getIEmpDAOInstance()
What is the use of this factory class? Finally, we can call it in the main class file to see what role the factory class has:
Main (String args []) = (x = 0; x <5; x ++ = 1000 + "Chinese display test-" + "programmer-" + 500 *
It can be seen that the specific implementation method is better hidden. The factory class calls the get method to obtain the proxy class, the proxy class calls a specific method, and then the specific operation object in the proxy class calls the specific operation method.
In fact, these things seem very simple, but they are often forgotten when they are designed. The main reason is that it is not used much. At first, we always had to force ourselves to think about it and then gradually become a habit.