Template Method and callback function

Source: Internet
Author: User

Template Method and callback function
Remember that at the beginning of JDBC learning, every SQL Execution must go through the process of obtaining, executing, and obtaining results from connections. When I was in my sophomore year, I was learning JSP and Servlet (My Java was self-taught). A high school student asked me to help her design the course. I still remember that it was a campus card project with few functions. The entire course design involved more than 10 SQL statements. When I was writing code, I wrote all the SQL statements in a Java file. The execution of each SQL statement must go through: copying code // getting Connection conn = getConnection (); // custom // obtaining statement PreparedStatement psmt = conn. prepareStatement (SQL); // set the parameter //.... // run the SQL command psmt.executeupdate‑psmt.exe cuteQuery // obtain the result // retrieve the number from the ResultSet // close the connection close () and copy the code. When the code writes more than 400 rows, I felt that writing this was too troublesome. Is there a template to deal with this process? It was too troublesome to write this? This was my real idea at the time. I was wondering if I could put this annoying process into a template? The preceding process is the same except for how SQL Execution and results are processed. So I will take out the SQL Execution and result processing, and the rest of the process will remain unchanged. It should be okay. You can use abstract classes to complete this function. At that time, I thought of: public abstract class JDBCTemplate {private String url; private String driver; private String user; private String password; protected Connection conn; public JDBCTemplate () {} public JDBCTemplate (String driver, String url, String user, String password) {this. driver = driver; this. url = url; this. user = user; this. password = password;} // getter, and setter will not be stuck. Public Connection getConnection () {try {Class. forName (driver); conn = DriverManager. getConnection (url, user, password);} catch (Exception e) {e. printStackTrace ();} return conn;} public void close (PreparedStatement statement) throws Exception {if (statement! = Null) {statement. close ();} if (conn! = Null) {conn. close () ;}} protected final Object template (String SQL, Object [] params) {conn = getConnection (); PreparedStatement psmt = null; try {psmt = conn. prepareStatement (SQL); for (int I = 0; I <params. length; I ++) {psmt. setObject (I + 1, params [I]);} return executeAndGetResult (psmt);} catch (Exception e) {e. printStackTrace (); return e;} finally {try {close (psmt);} catch (Exception e) {e. printStackTr Ace () ;}} public abstract Object executeAndGetResult (PreparedStatement psmt);} copy the code so that executeAndGetRresult is completed by sub-classes, and there is no need to write any annoying processes. I didn't know at the time. In this way, the template method mode was applied. ========================================================== ================================ Later, when learning AJax, you can learn about a function called callback and callback on the Internet, which is also a hook function. At that time, I didn't know about this. I learned online query, but more or less I had a vague concept about callback functions. At that time, I thought of the JDBCTemplate I wrote earlier. Can I use Callback to adjust the code? So I modified the code: copy the code // Add a callback interface public interface ResultSetHandler {public Object handle (PreparedStatement statement);} // adjust the template method interface as follows: protected final Object template (String SQL, Object [] params, ResultSetHandler handler) {Connection conn = getConnection (); PreparedStatement psmt = null; try {psmt = conn. prepareStatement (SQL); for (int I = 0; I <params. length; I ++) {psmt. setObject (I + 1, params [I]);} return ha Ndler. handle (psmt);} catch (Exception e) {e. printStackTrace (); return e;} finally {try {close (psmt);} catch (Exception e) {e. printStackTrace () ;}} copies the code. That is to say, the whole adjustment is changed to the one in the template method: The result is changed from executeAndGetResult (psmt) to handler. handle (psmt); remove the abstract of tempalte. After the adjustment, I felt that this method was better than the first one, but I still didn't know what the template method was. I only know that it is quite easy to write. So I kept this class, even though I didn't use it later.

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.