Database connection based on JSP technology

Source: Internet
Author: User
Tags define object http request connect sql stmt valid access
js| Data | database | Database connection compared with the traditional Client/server model database system, the Web database system uses a three-layer browser/server structure (that is, Web browser/web server/database server structure), which has great advantages. The Web database system gives full play to the DBMS efficient data storage and management capabilities, with B/S mode as the platform, the client unified as a Web browser, to provide users with simple, content-rich database services, has become the Internet and the core services provided by the intranet, Provides technical support for e-business on the Internet. The key technology of Web database system is the connection and access optimization between Web and database.

Web Database Connection Technology

Common Web Database Connectivity technologies include CGI technology, WEBAPI Technology, RAD Technology, and JDBC technology. The earliest CGI technology was supported by almost all Web servers, but there were serious flaws such as slow running, poor development and portability. The advent of WEBAPI overcomes the problem of speed, but development is more difficult. The various APIs are incompatible and extremely limited in scope. RAD Technology (rapid development technology) fundamentally changes the status of development difficulties, but it is dependent on a particular Web server and lacks versatility.

The biggest advantage of JDBC (Java database Connectivity) technology is that it provides a standard interface for all database management systems and provides uniform access to a variety of relational databases, which can be grouped into the following three sections:
One of the main features of the JDBC API is simple and easy to master. It consists primarily of interfaces rather than integration classes, and is included in the java.sql and javax.sql two packages. These interfaces are performed by software vendors that provide JDBC drivers.
The purpose of the JDBC Driver Manager is to provide the most basic guideline function on the JDBC operating structure, that is, when a JDBC API program makes a database call, it chooses the correct JDBC driver to connect.
The JDBC-driven role is to actually connect the database and handle it appropriately when the JDBC API makes a data call to the program. JDBC Driver provides the interface class for the JDBC API.

JSP Technology

The characteristics of JSP technology

Java Server page (JSP) is a Web page touch board that dynamically generates HTML documents using Java code. The JSP runs on the server-side component called the JSP container, which transforms the JSP into an equivalent Java Servlet. Because of this, the servlet and JSP pages are ultimately related. JSP pages have all the benefits of a servlet, such as good performance and scalability, embedded support for HTTP sessions, and so on. JSP pages also have their own advantages, such as automatic recompilation and greater compatibility with web development tools, if needed.

The JSP container automatically manages the JSP page based on the timestamp of each file. When a request for a JSP page is issued, the container is first judged with. The name of the class that corresponds to the JSP file.

If the class does not exist or is more than. The old JSP file, and then the container creates Java source code for an equivalent servlet and compiles 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. So, a JSP page has three kinds of existence form, namely JSP source code, Java source code and compiled Java class.

JSP elements can be divided into three types: pseudo directives, script elements (including expressions, scripts, and declarations), and actions. Where the pseudo directives are commands that instruct the JSP container to generate what code, 9 suppressed objects can be used in expressions and scripts, and the behavior is to create, modify, or use the top-level JSP elements of an object, using strict XML syntax encoding.

Using JSP to realize the connection between Web and database

Java uses JDBC technology to process a database is a comprehensive, common way to connect with the database, perform queries and extract data and other operations. Many relational database management systems have JDBC drivers. The specific steps are as follows:

1. Complete the Environment setting, import java.sql package, order as follows:


#import java.sql.*

2. Load Drive

The JDBC specification divides drives into JDBC-ODBC bridges, pure Java to database middleware, and pure Java directly to the database based on the drive structure. Here, using the local API and some Java type drives, explicitly create a drive instance as follows and register with drive Manager:

Drivermanager.registerdriver (New Oracle.jdbc.driver.OracleDriver ());

3. Connecting to a database

The drive manager retains the registered drive list and calls its getconnection () method to get the connection object. The getconnection () parameters are the database server's IP address, port number, library name, and the account and password required to log on to the database, examples are as follows:

Connection conn= Dirvermanager.getconnection (
"Jdbc:oracle:thin: @localhost: 1521:demo", "username", "password");

4. Statement interface

The SQL language consists of statements that create, represent, and extract data from a relational database. The object-oriented representations of these SQL statements provided by JDBC encapsulate their text, execution status, and results. This representation is called the Java.sql.Statement interface. The two sub-interfaces that use precompiled SQL PreparedStatement and the callablestatement that invoke the stored procedure extend the functionality of statement, as shown in the following example:
Statement stmt=conn.createstatement ();

5. Get the result set

A result set is a sorted list of table rows, expressed using the Java.sql.ResultSet interface in JDBC. The result set is generated by the ExecuteQuery () method of the statement interface or by some metadata method calls, as follows:
ResultSet rs=stmt.executequery (SQL);

Optimize access efficiency with connection pooling

In this example, the application tier uses WebLogic 6.1, the database layer is Oracle8.1.6, and the client uses the Navigate browser.

Instance

In this example, establish a connection pool named ConnectionPool. The basic properties of the ConnectionPool are as follows:

Lower limit of connection quantity in M_connectionpoolsize connection pool;
The upper limit of connection quantity in M_connectionpoolmax connection pool;
M_connectionusecount the maximum number of times a connection is used;
M_connectiontimeout The maximum idle time of a connection;
M_maxconnections =-1 Maximum number of connections at the same time;
M_timer timer.

These properties define the valid status values of the connection pool and each of these connections.

The self-management of the connection pool is actually done by periodically determining the status of each connection and the number of connections.

Here you can define the basic interfaces that connectionpool need to complete management, specifically:

public class ConnectionPool implements timerlistener{
public Boolean Initialize ()//Connection pool initialization
public void Destroy ()//Connection pool destruction
Public synchronized java.sql.Connection getconnection ()//Fetch a connection
Public synchronized void Close ()//Closing a connection
Private synchronized void Removefrompool ()//Remove a connection from the connection pool
Private synchronized void Fillpool ()/Maintain connection pool size
Public synchronized void TimerEvent ()//Timer event handler
}

With these interfaces, the basic management of connection pooling can already be completed. The stateful inspection of the connection pool is completed in the Timeevent () function, and the connection pool remains at least the minimum number of connections when Fillpool (). Because the user wants to save the status of each connection, a database connection object is also required to see:

Class connectionobject{
Public java.sql.Connection con; public Boolean inUse; Whether the flag is used
public long lastaccess; Last time to start using
public int usecount; Number of times to be used
}

After adding the Connectionobject object, the ConnectionPool should only be connectionobject, and the other process needs only the Connectionobject con attribute. So here's a second class that gets the interface to the return connection as another process:

CLASS conn{
Getconnection (); Remove a valid connection from the connection pool
CloseConnection (); Returns the connection, the connection is not closed at this time, but the connection pool is put back
Destroypool (); Destroying connection pools
}

At present, using JSP technology to build the B/s structure of the Web database system is a popular way, while the use of database connection pool system in terms of efficiency and stability than the use of other traditional systems of the system is much better. Database connection pool is a feasible solution to the complex problems in the whole system. However, in practical applications, JDBC connection is only a small part of large Web application systems, the database connection pool management program and the Web server, JSP engine and RDBMS engine management strategy are likely to conflict. Users should fully consider the various parts of the system, so as to give full play to their efficiency.



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.