Database connection based on JSP technology

xiaoxiao2021-03-06  42

The web database system uses a three-layer browser / server structure (ie the web browser / web server / database server structure) with respect to the traditional Client / Server mode based database system. The web database system has fully utilized DBMS efficient data storage and management capabilities. With the B / S mode as platform, unify clients as web browsers, providing users with simple, content-rich database services, have become Internet and intranet Core service provides technical support for e-commerce on the Internet. The key technology for the web database system is the connection and access optimization of the web and the database.

Web database connection technology

Common web database connection technologies are: CGI technology, WebAPI technology, RAD technology, and JDBC technology. The earliest CGI technology has been supported by almost all web servers, but there is a serious defect, such as slow operating speed, difficult development, and transplantation. The emergence of WebAPI overcomes speed problems, but developing more difficult. Various different APIs are not compatible with each other and is extremely limited. RAD technology (rapid development technology) fundamentally changes the status quo of developing difficulties, but it is very dependent on the specific web server, lacks versatility.

The biggest advantage of JDBC (Java Database Connectivity technology is that it provides a standard interface for all database management systems, which can provide unified access for a variety of relational databases, which can be divided into the following three parts:

◆ One of the main features of the JDBC API is simple and easy to master. It consists mainly of interface instead of integrated classes, which are included in java.sql and javax.sql two packages. These interfaces are done by providing JDBC-driven software vendors.

◆ The role of the JDBC Drive Manager is to provide the most basic guidelines on the JDBC running structure, that is, when a JDBC API program is called, it selects a correct JDBC driver to connect.

◆ The role of the JDBC driver is to actually connect the database and perform the corresponding processing when the program created by the JDBC API. JDBC drivers provide interface classes for JDBC APIs.

JSP technology

Characteristics of JSP technology

Java Server Page (JSP) is a web page touchpad that uses Java code to generate an HTML document. JSP is running on server-side components, called JSP containers, which convert JSP into equivalent Java servlet. Because of this, the servlet and JSP pages are ultimately related. The JSP page has all the advantages of servlet, such as good performance and scalability, providing embedded support for HTTP sessions. At the same time, the JSP page also has its own advantages. It automatically recompiles and greater compatibility with the web development tools as needed.

The JSP container automatically manages the JSP page based on the timestamp of each file. When the request is issued for a JSP page, the container first judges the name of the class corresponding to the .jsp file.

If the class does not exist or is older than the .jsp file, then the container creates a Java source code for an equivalent servlet and compile it. If the Servlet instance is not running, the container loads the servlet class and creates an instance. Finally, the container sends a thread to handle the current HTTP request in the loaded instance. Therefore, there are three forms of a JSP page, namely JSP source, Java source, and compiled Java classes.

JSP elements can be divided into three: pseudo-instructions, scripting elements (including expressions, script, and declaration) and actions. The directive is a command to indicate what code to generate the JSP container; 9 implied objects can be used in the expression and Script; behavioral is created, modified, or using the high-level JSP element of the object, using strict XML syntax coding.

Use JSP to implement the connection of Web and databases

Java uses the JDBC technology to process the database is a comprehensive, general way, implementation with the database, executes the query and extraction data. Many relational database management systems have JDBC drivers. The specific steps are as follows: 1. Complete environment settings, import java.sql package, commands as follows:

#import java.sql. *

2. Load the drive

The JDBC specification is divided into a JDBC-ODBC bridge based on the drive structure, and pure Java to the database middleware and pure Java are directly to several types of databases. Here, the local API is used to explicitly create a driver instance with a partial Java type drive and register with the drive manager:

DriverManager.RegisterDriver (new oracle.jdbc.driver.OracleDriver ());

3. Connect to the database

The Drive Manager retains the registered drive list and calls its getConnection () method to get the Connection object. The parameters of GetConnection () are the IP addresses, port numbers, library names, and login databases of the database server. Examples are as follows:

Connection conn = DirverManager.getConnection

"JDBC: Oracle: Thin: @localhost: 1521: Demo", "UserName", "Password");

4. Statement interface

The SQL language consists of a statement that is created from a relational database, represents and extracting data. Object-oriented representation of these SQL statements provided by JDBC is used to encapsulate its text, execute status, and results. This representation is a java.sql.statement interface. The two sub-interfaces using the pre-compiled SQL preparedStatement and calling the stored procedure extended the functionality of Statement, as follows:

Statement Stmt = conn.createstatement ();

5. Get the result set

A result set is a list of row of tables, represented using Java.sql.Resultset interface in JDBC. The result set creates the executeQuery () method of the Statement interface or some metadata method calls, as follows:

ResultSet RS = Stmt.executeQuery (SQL);

Optimize access efficiency by connecting pool

In this example, the application layer uses WebLogic 6.1, the database layer uses oracle8.1.6, and the client uses the NaviGate browser.

Example

A connecting pool named ConnectionPool is created in this example. The basic properties of ConnectionPool are as follows:

M_ConnectionPoolSize Connection Pool Lower Limit;

M_ConnectionPoolMax Connection Pool The upper limit of the connection;

M_ConnectionUseCount a maximum number of connections for connections;

M_ConnectionTIMEOUT a connection for the longest free time;

M_maxConnections = -1 The maximum number of connections at the same time;

m_timer timer.

These attributes define the valid status values ​​of the connection pool to each of them.

The self-management of the connection pool is actually performed by judging the status of each connection and the number of connections to each connection.

Here you can define the basic interface that ConnectionPool to complete the management needs, see below:

Public class connectpool imports timerlistener {

Public Boolean Initialize () // Connection Pool Initialization

Public void destroy () // Connect the pool destruction

Public synchronized java.sql.connection getConnection () // Take a connection to public synchronized void close () // Close a connection

Private synchronized void transovefromPool () // Remove a connection from the connection pool

Private synchronized void fillpool () // Maintenance connection pool size

Public synchronized void timeevent () // Timer event handler

}

Through these interfaces, the basic management of the connection pool can be completed. When you complete the status verification of the connection pool in the TimeEvent () function, the connection pool maintains at least the maximum number of connections when FillPool (). Because the user wants to save the status of each connection, there is also a database connection object to see:

Class connectionObject {

Public java.sql.connection con; // is used to use a sign

Public long lastaccess; // Last started using time

Public int usecount; // is used

}

After adding the ConnectionObject object, the operation in ConnectionPool should be just ConnectionObject, while other processes need only the CON attribute of the ConnectionObject. So here additionally, the following class is added, and the interface to the returned connection is obtained as other processes:

Class conn {

GetConnection (); // Remove an effective connection from the connection pool

CloseConnection (); // Returns the connection, there is no connection, just put it back the connection pool

DestroyPool (); // Destroy the connection pool

}

The Web database system that is currently constructing the B / S structure is a popular way, while using the system of the database connection pool is much better than the system of efficiency and stability than using traditional other ways. Database connection pools are a feasible solution for complicated issues over the entire system. However, in practical applications, JDBC connections are only a small part of a large web application system. The database connection pool management program is possible to conflict with the management policy of the web server, JSP engine, and RDBMS engine. Users should fully consider the various parts in the system, so they can fully utilize their efficiency

转载请注明原文地址:https://www.9cbs.com/read-61353.html

New Post(0)