本文以Oracle数据库为例,介绍了在采用JSP技术开发Web应用时一种简便通用的表单数据存储处理方法,以减轻开发工作量,同时提供了主要的程序代码。
引言
J2EE(Java 2 EntERPrise Edition)技术已广泛应用在Web应用开发中,其中的JavaBean、Servlet技术为开发者提供了更为清晰的开发环境,使用JSP技术表现页面,使用Servlet技术完成大量的业务处理,使用Bean来存储数据及一些业务处理。在WEB应用中,业务数据存储到数据库中的处理工作经常很繁重,其中一种主要的形式就是表单数据存储到数据库,整个应用处理过程牵涉到大量的这种数据存储操作,对每个表单都要单独编写相应的数据存储程序,花费了开发人员大量的时间和精力。采用什么方法来减轻表单数据存储的开发工作量是值得研究的问题。
两种常见的表单数据存储处理方法
1、对每一表单都编写相应的程序代码
在JSP页面或JavaBean或Servlet中,使用request. getparameter()函数逐一提取表单提交的数据,或编写相应的JavaBean,使用setProperty方法将数据自动取到JavaBean中,然后生成SQL语句(insert,update,delete),最后执行executeupdate()函数完成数据表存储。
2、对每一数据表自动生成一个JavaBean程序代码
数据库系统必须支持用户能够读取表结构,并识别关键字段。利用面向对象快速开发工具,如PowerBuilder、Delphi等,自行开发一个java代码自动生成程序。在该程序中读取数据库表的结构:字段名、数据类型、数据长度,自动生成一个JavaBean代码。在该代码中定义与表中字段对应的同名变量,建立所有变量的setValue和getValue方法,建立insert、update、delete函数分别处理insert、update、delete的SQL语句生成和执行。
在表单提交的数据处理页面中,编写如下代码,将表单数据存储到JavaBean中:
<jsp:useBean id="table" class="table1_bean" />
<jsp:setProperty name="table" property="*" />
(注:table1_bean为上述自动生成的对应某一个表的JavaBean)
然后调用table1_bean中insert、update、delete函数完成数据表存储,并返回执行结果。如:
<%boolean success =table.insert(); %>
第一种方法简单直观,但对每一表单都需要编写相应的数据处理程序。对稍微大一点的应用,表单数量可能很多,开发工作量很大,开发工作效率低。表结构变动如增加、减少字段时,需修改相应的数据处理程序。
第二种方法相对第一种简便得多,每一数据表的数据处理由对应的JavaBean实现,JavaBean自动生成,不需编写,表结构变动时只需重新生成新的JavaBean,经java编译后覆盖原java类即可。但该方法需要开发JavaBean自动生成程序,表结构变动时JavaBean需要重新生成和编译。
介绍一种简便通用的方法实现表单数据存储
在WEB应用开发中,很多表单在经过前台浏览器端简单的数据校验后,提交后台服务器,服务器对数据不用作任何处理直接将数据存储到一个数据表中。对这种情况,我们可以只编写一个程序,对这些表单统一处理,将数据存储到相应的一个数据表中。该方法同样要求数据库系统支持表结构读取和关键字段识别。我们采用JSP技术编写该程序,程序文件取名为DbdataStore.jsp。
1、调用格式
在网页中表单的Action调用方法如下:
<Form Name=Frm1 Method=Post Action="DBdataStore.jsp? tablename=table1&OperType=…">
table1为数据将要存储的数据库表的表名,OperType操作类型分为三种:insert,update,delete。
表单中的<input type=text name=…>,<textarea name=…><select name=…>等中的name值应与数据表的字段名相同,DBdataStore.jsp中逐一提取表单提交的对应字段名的数据值,若表单中未定义输入,得到的值为空值,则对该字段不作处理。
2、以oracle为例的视图定义
1) 建立表各列数据类型视图
CREATE OR REPLACE VIEW v_dbstru AS SELECT table_name,column_name,data_type,data_length,data_precision,data_scale,column_id
FROM all_tab_columns WHERE owner='user1';//user1为数据表的属主。
2) 建立表的关键列视图
CREATE OR REPLACE VIEW v_pkey_column AS
SELECT b.table_name,b.column_name,b.position
FROM all_constraints a,all_cons_columns b
WHERE a.owner=b.owner AND a.constraint_name=b.constraint_name AND a.owner='user1' AND a.constraint_type='P';
Introduction
J2EE (Java 2 EntERPrise Edition) technology has been widely used in Web application development, the JavaBean, Servlet technologies for developers to provide a more clear development environment, the use of the technical performance of the JSP page, use the Servlet technology to deal with a large amount of business, Bean used to store data and handle some business. WEB in the applications, data storage business to the database processing often very heavy, one of the main form is in the form of data storage to the database, the whole application process involves a large number of such data storage operations, each table One must separate the preparation of the data storage procedures, the developers spent a lot of time and effort. The methods adopted to alleviate the form of data storage development work is worth studying.
Two common forms of data storage method
1, each form is the preparation of the code
In the JavaBean or JSP or Servlet page, use the request. Getparameter () function to submit one by one form of data extraction, or preparation of the JavaBean, the use of setProperty method will automatically check data to the JavaBean, and then generates SQL statements (insert, update, delete), the implementation of the final executeupdate () function to complete the data storage table.
2, data for each table automatically generate a JavaBean code
Database system to support users to read the table structure, and identify keyword section. The use of object-oriented rapid development tools, such as PowerBuilder, Delphi and so on, developed a java program code automatically generated. In the process of reading the structure of the database table: field names, data types, data length, and automatically generate a JavaBean code. The code defined in the table with the field corresponding to the variables of the same name, set up all the variables and getValue method setValue, the establishment of insert, update, delete function to deal with each insert, update, delete the formation and implementation of the SQL statement.
In the form submitted by the data-processing page, following the preparation of the code will form the data storage JavaBean:
<jsp:useBean Id="table" class="table1_bean" />
<jsp:setProperty Name="table" property="*" />
(Note: table1_bean for the above-mentioned automatically generated a corresponding list of JavaBean)
Table1_bean and then call in the insert, update, delete function to complete the data storage table and return the results. Such as:
<% Boolean success = table.insert ();%>
The first method is simple and intuitive, but each form requires the preparation of the corresponding data-processing procedures. For a little bigger application form may be a lot of volume, the development of the heavy workload, the development of low efficiency. Such as changes in the structure of the table, reducing the field, the need to amend the corresponding data-processing procedures.
The second method is relatively simple and much more the first, each table of data-processing data from the corresponding JavaBean to achieve, JavaBean automatically without the need to prepare, changes in the structure of the table only when the re-formation of a new JavaBean, compiled by the java coverage Java to the former category. However, the need to develop methods of automatically generated JavaBean procedures, changes in the structure of the table when the JavaBean need to re-generate and compile.
A simple way to achieve common form of data storage
WEB in application development, a lot of form in front through a simple browser-side data validation, submitted to the background servers, data servers do not have any direct dealing with the data stored in a data table. This situation, we can only write a program, these form a unified processing, data storage corresponding to a data table. This method is the same database system to support the request form and structure of the keyword paragraph to read identification. JSP technology we use to prepare the program, the program file named DbdataStore.jsp.
1, call format
Page in the form of Action call as follows:
<Form Name=Frm1 Method=Post Action="DBdataStore.jsp? tablename=table1&OperType=…">
table1 data to be stored in a database table of the table, OperType operation is divided into three types: insert, update, delete.
In the form of <input type=text name=…>, <textarea name=…> <select name=…> such as the name and value of the data tables were the same field, DBdataStore.jsp extracted one by one in the form submitted to the The field names correspond to the value of the data, if not defined in the form input values to be null, the field does not deal with.
2, as an example of the oracle in order to view the definition of
1) the establishment of the table data type view
CREATE OR REPLACE VIEW v_dbstru AS SELECT table_name, column_name, data_type, data_length, data_precision, data_scale, column_id
FROM all_tab_columns WHERE owner = 'user1'; / / user1 to be the main data table.
2) the establishment of a key column table view
CREATE OR REPLACE VIEW v_pkey_column AS
SELECT b.table_name, b.column_name, b.position
FROM all_constraints a, all_cons_columns b
WHERE a.owner = b.owner AND a.constraint_name = b.constraint_name AND a.owner = 'user1' AND a.constraint_type = 'P';
回复Comments
作者:
{commentrecontent}