Establishing JSP operation to improve database access efficiency

Source: Internet
Author: User
Tags include interface odbc sql query access
js| Access | data | database

The rapid development of Java Server Pages (JSP) from the server-side facilitation program has made it a popular tool. Although the inflexible side still exists, however many drawbacks it contains, JSP can provide a lot of things for web programmers, especially with database input and output operations and data processing.

Using a JSP for database access does not require too much booting. The problem is how developers can accept database access for Web programs more attractive than traditional local OLAP systems, and make them aware that neat and concise data access methods are critical. At this point, JSP can be a good solution.

What can you do?

If you're writing a Web program with high data size and data density, what is your database access requirement? If you are using JSP, then you have to handle a lot of server-side data. Programs may need a lot of data, or a lot of users, or a combination of both. The possible scope of your consideration will include: performance optimization, scalability, the impact of multiple queries per user, complexity of queries, and high class conversions when a single session handles large amounts of data.

This includes a large scale of processing. However, JSP can help you handle it well because it is compatible with the Java Database Connectivity API (JDBC). You can include JDBC in your JSP code, and JDBC can deliver commands that execute a database like this, as long as you can use the JDBC driver correctly.

Begin

Here is a common, abstract JSP program execution system. The preferred JSP design pattern is a model-view-Controller (Mvcmodel-view-controller), a variant of the traditional three-tier system to better fit the server program. In the JSP MVC design pattern, model refers to the logic and data of the program, view for viewing, and controller for request processing.

When you design a JSP program, it is the best process to create a first-step page between client and server interaction. For example, in a typical program, there will be pages for each specific step in the data interchange: A Data entry page, a validation request page, a database response page, and a subpage of those pages (a page that changes records, a page that deletes records, and so on).

You can embed JDBC into each page to complete the requested database operation. However, this operation can also take a lot of risk, because the mixture of JSP and JDBC mixed with the entire program ──JDBC is based on SQL. This is where SQL is encapsulated in JDBC, and JDBC is encapsulated in jsp── that's enough to make you dizzy. If you choose this method, you will get the functionality you want, but be sure to make sure that your program logic is very clear with the database access code, which is especially careful.

Embedded JDBC

The JDBC API does not communicate directly with the database. Where the driver completes the actual connection, you can download these drivers on the seller's web site. In addition, there are four types of JDBC drivers, and if you decide to use JDBC, you need to correctly choose the type that best suits your needs. You will use a DriverManager class to handle the connection based on the driver.

You can use a DriverManager method called Getconnection to build your database connection. You can also use its URL parameters to identify the database:

public static Connection getconnection (Jdbc:odbc:nameOfDatabase)

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

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

You have connected the database to the JSP program, but you are still unable to execute a database command. To solve this, you can generate a declaration in your JSP code to create a database command, as follows:

Public Statement createstatement (Intresultsettype, intresultsetconcurrency)

The parameters allow you to control the results from the database query. When you use the first parameter, you can see the result in the program, and when you use the second parameter, you can update the value with the query (this is an unbelievable feature that is worth further discussion in future articles).

Table A

Http://builder.com.com/5100-6387-5172666.html?tag=sc#Listing

Alisting A shows the complexity of the next two approaches.

The declaration (Statement) is the SQL command. PreparedStatement is the statement of SQL, which you can use to control the procedure. CallableStatement is used to access SQL stored programs. Are you beginning to realize that if you don't see these explanations, do you think these statements are complicated? Note that by invoking the Rollback method, you can remove the transaction process.

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

Resultsetexecutequery (String sqlquery)

(You can use ExecuteQuery to complete the above process.) You can also use a executeupdate to complete the update, insert, and delete. The above declaration interface allows you to use a number of methods to execute the SQL declaration. What resultset do is access the data from the query, so you can use that data in your JSP program.

By breaking up a JSP into a single, feature-capable page, and performing 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 those pages.

But there are more things you can do to make your JSP database access cleaner and easier to maintain. Embedding JDBC in the JSP code and communicating with the database by sending SQL commands are all good. However, it requires a program that creates an SQL command through an interface without increasing the complexity of the code. When your SQL processing needs to be more flexible, you can further detach your database interface code to clean your JSP program. In the following article, I will show you how to implement these procedures.



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.