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.