Java Server Pages (JSP), which facilitates rapid program development from the server side, has become a widely popular tool. Although the dull side still exists, no matter how many shortcomings it contains, JSP has a lot to offer Web programmers, especially with database input and output operations and data processing.
Database access via JSP does not require much guidance. The problem is how to make developers accept that database access from Web programs is more attractive than database access from traditional local OLAP systems, and to make them realize that clean and concise data access methods are key. At this point, JSP can solve it very well.
What can you do?
If you are writing a Web program with high data volume and high data density, what requirements should your database access have? If you are using JSP, then you have to deal with a lot of server-side data. The program may require large amounts of data, or many users, or a combination of the two. Possible areas you may also consider include: performance optimization, detectability, the impact of multiple queries per user, query complexity, and high-level type conversion when a single session handles large amounts of data.
This involves large-scale processing. However, JSP can help you deal with it well because it is well compatible with the Java Database Connectivity API (JDBC). You can include JDBC in JSP code, and JDBC can pass statements to execute database-like commands, as long as you use the JDBC driver correctly.
At the beginning, here is a general, abstract JSP program execution system. The preferred JSP design pattern is Model-View-Controller (MVCModel-View-Controller), which is a variation of the traditional three-tier system to better suit server programs. In the MVC design pattern of JSP, Model refers to the logic and data of the program, View refers to viewing, and Controller refers to request processing.
When you design a JSP program, it is best to create a page that is the first step between client and server interaction. For example, in a typical program, there would be pages for each specific step in the data exchange: a data entry page, a validation request page, a database response page, and subpages of these pages (a change record page, a Delete recorded pages, etc.).
You can embed JDBC into every page to complete requested database operations. However, this operation also carries a great risk, because the entire program is mixed by mixing JSP and JDBC-JDBC is based on SQL. This means that SQL is encapsulated in JDBC, and JDBC is also encapsulated in JSP─this is enough to make you dizzy. If you choose this approach, you will get the functionality you want, but be extra careful to ensure that the relationship between your program logic and database access code is very clear.
EmbeddedJDBC
The JDBC API does not communicate directly with the database. The actual connection is done by the drivers, which you can download from the seller's website. In addition, there are four JDBC driver types. 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 driver-based connections.
You can use a DriverManager method called getConnection to establish your database connection. You can also identify the database using its URL parameters:
public static Connection getConnection(jdbc:odbc:nameOfDatabase)
Now, tell the DriverManager 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 still cannot execute a database command. To solve this, you can generate the statement in JSP code to establish the database command like this:
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 results in the program; when using the second parameter, you can update the value through the query (this is an incredible feature that deserves further discussion in a future article).
Table A
http://builder.com.com/5100-6387-5172666.html?tag=sc#Listing
AListing A shows the complexity of listing the next two methods.
Statements are SQL commands. PreparedStatement is a statement of SQL, and you can control the process of the program through its parameters. CallableStatement is used to access SQL stored procedures. Are you starting to realize that if you don't see these instructions, do you find these statements complicated? Please note that by calling the rollback method, you can undo the transaction process.
If you want to use all these database access methods, the only thing you are missing is:
ResultSetexecuteQuery(string sqlQuery)
(You can use executeQuery to complete the above process. You can also use an executeUpdate to complete updates, inserts and deletes). Your declaration interface above allows you to use a number of methods to perform SQL declarations. All the ResultSet does is access the data obtained from the query, so you can use this data in your JSP program.
By breaking JSP programs into single, functionally obvious pages and performing a single database operation in 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 JDBC on these pages.
But there's more you can do to make your JSP database access cleaner and easier to maintain. Embedding JDBC in JSP code and communicating with the database by sending SQL commands is a good process. However, it requires building a program for SQL commands through the interface without increasing the complexity of the code. When your SQL processing needs increased flexibility, you can further separate your database interface code to clean your JSP programs. In the following article, I will tell you how to implement these processes.