Create JSP operations to improve database access efficiency

Source: Internet
Author: User

Java Server Pages (JSP), which promotes rapid program development on the Server side, makes it a popular tool. Although the dull side still exists, JSP can provide a lot of things for Web programmers, especially with database input and output operations and data processing, no matter how many disadvantages it contains.

Using JSP for database access does not require much guidance. The question is how to make developers more attractive to access the database of Web programs than to access the database of traditional local OLAP systems, and make them realize that, clean and brief data access methods are critical. At this point, JSP can be well solved.

What can you do?
If you are writing a Web program with high data volume and data density, what requirements should your database access meet? If you are using JSP, you must process a large amount of data on the server. The program may need a large amount of data, many users, or the combination of the two. You may also consider performance optimization, testability, the impact of multiple queries for each user, and query complexity, and high-level type conversion when a single session processes a large amount of data.

This includes large-scale processing. However, JSP can help you handle it well because it is compatible with Java Database Connectivity API (JDBC. You can include JDBC in JSP code, and JDBC can pass statements to execute commands similar to databases, as long as you can correctly use the JDBC driver.

Start
Here is a general and abstract JSP program execution system. The preferred JSP design mode is Model-View-Controller (MVCModel-View-Controller), which is a variant of the traditional three-tier system to better adapt to server programs. In the MVC design mode of JSP, Model refers to the logic and data of the program. View is View, and Controller is request processing.

When you design a JSP program, it is the best process to create a page in step 1 between the client and the server. For example, in a typical program, each specific step in data exchange has a page: a data entry page, a verification request page, and a database response page, and the subpages of these pages (a page for changing records, a page for deleting records, and so on ).

You can embed JDBC into each page to complete the requested database operations. However, this operation can also take a big risk, because the JSP and JDBC are mixed with the entire program-JDBC is based on SQL. That is, SQL is encapsulated in JDBC, and JDBC is also encapsulated in JSP -- this is enough to make you dizzy. If you choose this method, you will get the functions you want, but be careful when you make sure that the relationship between your program logic and the database access code is very clear.

Embedded JDBC
JDBC APIs do not communicate with databases directly. The driver completes the actual connection, you can download these drivers on the seller's Web site. In addition, there are four JDBC driver types. If you decide to use JDBC, You need to select the type that best suits your needs. You will use a DriverManager class to process driver-based connections.

You can use the DriverManager method named getConnection to establish your database connection. You can also use its URL parameters to identify databases:

Public static Connection getConnection (jdbc: odbc: nameOfDatabase)

Now, tell DriverManager about the driver (which should be in your classpath ):

Class. forName ("sun. jdbc. odbc. nameOfJDBCDriver ");


You have connected the database to the JSP program, but you still cannot execute a database command. To solve this problem, you can generate a declaration in the JSP code to create a database command, as shown below:

Public Statement createStatement (intresultSetType, intresultSetConcurrency)

 

The parameters allow you to control the results obtained from the database query. When the first parameter is used, you can see the result in the program; when the second parameter is used, you can update the value through the query (this is an uncertain function, it is worth further discussion in future articles ).

Table
Http://builder.com.com/5100-6387-5172666.html? Tag = SC # Listing
AListing A lists the complexity of the next two methods.

Statement is an SQL command. PreparedStatement is the SQL statement. You can control the program process through the parameters. CallableStatement is used to access SQL stored programs. Are you starting to realize that if you don't see these instructions, do you think these statements are complicated? Note that you can remove the transaction process by calling the rollback method.

If you want to fully use these database access methods, the only thing you miss is:

ResultSetexecuteQuery (string sqlQuery)


(You can use executeQuery to complete the above process. You can also use executeUpdate to complete updates, inserts, and deletes ). Your above declaration interface allows you to execute SQL statements in some ways. What ResultSet does is access the data obtained from the query, so you can use the data in the JSP program.

By breaking JSP programs into a single, clearly functional page and executing a single database operation on any given page, you can greatly simplify your database operations, and create pages that can be used for future program development, even if you embed SQL into the JDBC of these pages.

But you can do more to make the access to your JSP database more clean and easy to maintain. The process of embedding JDBC in JSP code and communicating with the database by sending SQL commands is good. However, it requires that you create an SQL command program through the interface without increasing the complexity of the Code. When your SQL processing needs to improve flexibility, You can further separate your database interface code to clean your JSP program. In the following article, I will show you how to implement these processes.

 

Related Article

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.