JDBC design in JSP development accelerates Database Access

Source: Internet
Author: User
Tags tld

JSP programs are all modules and have powerful expression-request functions. Creating a perfect JDBC design database access is a challenging process, and the JDBC design interface can well complete this process. However, the JDBC code embedded in JSP code, like SQL commands embedded in JDBC, can make full use of the JSP function to create a clean and easy API for the client. To achieve this goal, we can consider using JSP operations to create database interface components.

The perfect JSP database access design mode is Model-View-Controller (MVC ). The traditional three-tier system is: Model for Program Logic and data; View for viewing; and Controller for request processing. Following this model, a JSP program contains each "line" page of the client-server "dialog box. In a typical program, you can see a query page, a verification page, a database insertion page, a database update page, and so on.

How to embed JDBC in each page to ensure a more reasonable program structure. However, creating executable SQL commands, just as variables are passed through JDBC commands, may increase the complexity of the program.

JSP operations designed by JDBC

Another way to operate a JSP database is to create a set of operations for the database without using JDBC. Using this method, you can get two benefits: First, you can eliminate the need to use JDBC design, which simplifies a lot of work; second, your design and code organization are more reasonable (such as readability, flexibility, and maintainability ).

You still need some drivers, but you need to simplify the above operations first. Operations in JSP programs are logical blocks which are usually written and used by other JSP program developers. However, you can use them as subprograms. Using JSP operations is to standardize some functions and minimize the number of Java code embedded in JSP.

JSP provides a set of standard extended classes. Through these classes, you can define an operation through a tag manager (tag handler. There are two Java interfaces defined in JSP: Tag interface and BodyTag interface, which are executed by TagSupport class and BodyTagSupport class respectively.

You can create a tag library for common JSP purposes, and you can also execute tag handler to extend the class support. Follow these steps.

First, execute a label manager class:

 
 
  1. packagecom.myactions;  
  2.  
  3. (import statements go here)  
  4.  
  5. public class MyActionTag extends TagSupport {  
  6. ...  
  7. }  
  8.  

Next, compile the code and place the class file in the class library of the program. Then, you will need a Tag Library Descriptor (TLD) file, which is an XML file to match your operation name and the corresponding Tag manager class.

 
 
  1. ﹤tag﹥  
  2. ﹤name﹥MyAction﹤/name﹥  
  3. ﹤tagclass﹥com.myactions.MyActionTag﹤/tagclass﹥  
  4. ﹤bodycontent﹥ (whatever) ﹤/bodycontent﹥  
  5. ﹤attribute﹥myData﹤/attribute﹥  
  6. ﹤/tag﹥  
  7.  
  8. ﹤/tag﹥  
  9.  

Assume that you have created an operation named MyAction, which is a TLD that matches the com. myactions. MyActionTag class. The TLD file must be located in the program's TLDs path.

When you access the database page from a JSP to call an operation, the TLD tells the JSP correct class to use the operation. This brings great convenience and requires only a small amount of code.

But where to introduce SQL? First, you need to establish database access with the connection function. You can use the javax interface, while javax is visible in the JDBC 2.0 Optional toolbox. The javax. SQL. DataSource class of JDBC 2.0 provides the connection you need.

Where is the SQL statement located? It is in bean. You can use JDBCcreateStatement and PreparedStatement to create a method in bean. Turn this method into a public Vector and pass your SQL statement to this method correctly.

Summary

Your Database bean executes an SQL statement embedded in the operator body. You can pass a statement to the SQL statement or use it to execute a pre-operation. You can use the tag manager to perform your operations. Since JDBC is embedded in the library code, you cannot use it explicitly in JSP programs.

The first time you use this JDBC design, you will think it is more complicated than the embedded SQL in JDBC and the embedded JDBC in JSP access database. However, you create SQL operations and store them in a TLD, you only need to perform these operations once and can access them in all JSP programs. This is the advantage of this method.


Edit recommendations]

  1. Six common issues for JSP Programming
  2. Specific algorithms for implementing the tree structure of JSP forums
  3. Introduction to JSP: several solutions to avoid repeated submission of Form forms
  4. Li> JSP tutorial-traffic count JSP source code
  5. JSP entry-level website environment setup steps

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.