Promote from the serverProgramThe rapidly developed Java Server Pages (JSP) 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 JSPCodeAnd 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 using the first parameter, you can see the result in the program; when using the second parameter, you can update the value through the query (this is an untrustworthy function, in the futureArticle).
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.