/ * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------ Write in front: I see many netizens's questions are related to server configuration, build a reliable and safe application development The server is the foundation of web programming, especially for friends who have developed JSP on Linux.
This document is the crystallization of my multi-year server configuration. I haven't been modified by my N before publishing (the last big revision is just released in RH7.2, and then because I have not found a mistake because I didn't find any mistakes, I didn't have it. Upgrade), documentation
Be reprinted by Tianji.com by the end of 2001 (http://www.yesky.com/200206/217592.shtml), then reproduced multiple Linux and Java communities such as LinuxBYE.NET, CNJsp.COM, have not been discovered since the time test Error, you are as good as
safe to use.
Documentation is not ideal in the web page, if you want to get PDF of this document, or have any comments, you can mail to Roczhao@msn.com
The document is longer, divided into more articles, this is the seventpage, the following is the text: ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------------------------------------- * /
Example 3: Use the processing layer connection database
## This example tells you how to use components
## In fact, you can write a bean to handle the database without using an example. Only four statements can remove all the data in the database and display it, so that the complete separation of the processing layer and the logic layer is achieved, and the logical layer issues an instruction, and the processing layer is derived, and the record set will not return to The JSP page of the logic layer, and the bean processing of the processing layer makes the code very streamlined, the efficiency is also higher. As for how to write components is not the focus of this document, only one component is given and explain the calling method of the logical layer, you will find that the logical layer is a bureaucrat, the processing layer is subordinate, huh, huh.
1 Create a processing layer component
The following components are the simplified version of the database processing component written by the company, take into account the problems such as space and readability. Here I simplifically has its function (like data paging, bulk update, get the number of fields, and field name, with SET And GET improves its scales, and does not appear in the code of other library methods, so that all common functions and make it clearer and easy to understand; second, I will add library action plus In the constructor, this is an advantage that it is also a disadvantage. It has simplified the library action. The disadvantage is to reduce versatility. I don't add a way to set up a data source, so that whenever a database is changed, it is necessary to rewrite the connection pool in the component. Name, this is ok when it is a fixed project, if you need a set data source, you can add it yourself;
## Use components:
I am talking about using the general method of using bean. You build your own bean directory can be used as this example. In the root of your site, build the directory specified in resin.conf, I have the example of Classes, then create a classes directory, then Classes is your site's class root directory. Then COM / Gledecity / YESGO / SQL then sequentially, pay attention to the father and child relationship, not the same level folder, then put the downcomer DBRIDGE.JAVA "Site Root Catalog / Classes / COM / GleDecity / Yesgo / SQL directory, then upload the file to the server. The above steps can be done using the development tools such as Ultradev on the Microsoft platform, of course, can be manually. The source file is as follows:
/ / -------------------------------------------------------------------------------------------- ------------------------------
// file: dbbridge.java
................................
// author: Zhao Fengjun @ 06/18/01
// Last Revision: Zhao Fengjun @ 01/18/2002
// DESCRIPTION: Database interface class. A unified database interface implements a complete separation of the logical layer and the data layer, encapsulating common database operations. The main function is: Connect the database, perform SQL statements, database values operation, data paging, clear data, turn off databases.
// Version: Simplified version 1.0
/ / -------------------------------------------------------------------------------------------- ------------------------------
// Specify the package directory
Package com.gledecity.yesgo.sql;
// Import package
Import java.sql. *;
IMPORT COM.CAUCHO.SQL. *;
Public Class Dbbridge {
// Declare attribute
PRIVATE DBPOOL POOL;
PRIVATE CONNECTION CONN;
Private resultset rs;
Private statement stmt;
// -------------------------------------- Constructor-------- -------------------------
Public dbridge () {
Pool = NULL;
CONN = NULL;
RS = NULL;
STMT = NULL;
Try {open ();
Catch (java.sql.sqlexception ex) {ex.toT7tring ();}
}
/ / -------------------------------------------------------------------------------------------- ------------------------------------
/ / Connect the method of the database
Public void open ()
Throws sqlexception {
IF (conn! = null &&! conn.isclosed ())
Throw New Sqlexception ("The Connection Has Been Established Already.");
Clear ();
Pool = New dbpool ();
Conn = pool.getpool ("Your_Data_Source_Name"). getConnection ();
}
// Method for executing the SQL statement, two methods // in the jdbc in jdbc are one, pay attention to the return value is shaped, public int execsql (String SQLSTMT)
Throws sqlexception {
IF (conn == null || conn.isclosed ())
Throw New Sqlexception ("This Connection Has NOT BEEN Established Yet.");
IF (SQLSTMT == Null)
"" SQL-Statement is Null. ");
Clear ();
Conn.setautocommit (TRUE);
STMT = conn.createstatement ();
IF (SqlStmt.touppercase (). StartSwith ("SELECT")) {
RS = stmt.executequery (SQLSTMT);
Return -1;
}
Else {
INT Numrow = stmt.executeUpdate (SQLSTMT);
Clear ();
Return NumRow;
}
}
// Get the field value, the parameters are plastic - field order
Public String getString (int fieldno)
Throws sqlexception {
Return Rs.getstring (FieldNo);
}
// Get field value, parameter is string - field name
Public string getString (String FieldName)
Throws sqlexception {
Return Rs.getstring (FieldName);
}
// Upload pointer
Public Boolean Previous ()
Throws sqlexception {
IF (rs == null)
Throw new Sqlexception ("ResultSet is Null.");
Return rs.previous ();
}
// downward pointer
Public boolean next ()
Throws sqlexception {
IF (rs == null)
Throw new Sqlexception ("ResultSet is Null.");
Return rs.next ();
}
/ / The pointer is the most
Public boolean first ()
Throws sqlexception {
IF (rs == null)
Throw new Sqlexception ("ResultSet is Null.");
Return rs.first ();
}
/ / The pointer is at the bottom
Public Boolean Last ()
Throws sqlexception {
IF (rs == null)
Throw new Sqlexception ("ResultSet is Null.");
Return Rs.last ();
}
// Clear the variable, when you only need to clear the variable without the library, you can call this method.
Private void clear () throws sqlexception {
IF (RS! = null) rs.close ();
RS = NULL;
IF (stmt! = null) stmt.close ();
STMT = NULL;
}
/ / Clear the variable and close public void close () throws sqlexception {
Clear ();
IF (conn == null)
Throw New Sqlexception ("This Connection Has Been Closed Already.");
IF (conn.isclosed ())
Throw New Sqlexception ("This Connection Has Been Closed.");
CONN.CLOSE ();
CONN = NULL;
}
}
2 Writing logical layer code
## Create dbbridge.jsp at the client, then uploaded to the server:
## 上 级 要 来 察 工作 工作 该 该 该 该 该 该 该 定 定 定 该 定 定 定 定 定 定 定:
## 层 处理 处理 处理 层: 小子, go with my background. // Database and initialization
## The logical layer said to the processing layer: prepare our work outcome this year. / / Execute SQL statement
<% bridge.execsql ("Select * from proV");
## 层 层 处理 处理 层 层 层 层 层 层 层 层 层 层? 层 层 层 层 层here you go! On the superior, I saw a piece of data. // loop display
While (bridge.next ())
{OUT.PRINT (Bridge.getstring ("proV_id") bridge.getstring ("proV_name") "
");}
## 逻 层 处理 处理 层 层 层 好 好 好 吧 吧 吧 吧 吧 吧 吧 吧 吧 吧 吧 吧 吧 吧 吧 吧 吧 吧// Turn off all variables
Bridge.close ();%>
## 上 上 看 满 满 满 拍 官 僚 僚 的 僚 一 一 一 一 一 一 一 一 一 一 一 一 一 一 一 一 一 一 一 一 一 一 一 一 一 一 一 一 一 一 一 一 一 一 一 一
3 Test the program
Lynx http://www.yesgo.loc/dbbridge.jsp
## Of course, you can also test on the client browser, just you need to add your server's IP to the DNS list in your network settings.
## Processing The result is all the data in the database:
1 Anhui 2 Beijing 3 Chongqing 4 Fujian 5 Gansu 6 Guangdong 7 Guangxi 8 Guizhou 9 Hainan 10 Hebei 11 Heilongjiang 12 Henan 13 Hubei 14 Hunan 15 Inner Mongolia 16 Jiangsu 17 Jiangxi 18 Jilin 19 Liaoning 20 Ningxia 21 Qinghai 22 Shanxi 23 Shaanxi 24 Shandong 25 Shanghai 26 Sichuan 27 Tianjin 28 Tibet 29 Xinjiang 30 Yunnan 31 Zhejiang 32 Hong Kong 33 Macau 34 Taiwan