INSERT, DELETE, UPDATE three SQL statements are three basic statements of database technology. It can be said to be everywhere in the usual web development. If it is simple to construct these SQL statements, on the one hand Give us a lot of workload, and the system flexibility is limited. So can you let the system to remove elements from the page form to construct the SQL statement from the page form? First let us see see generally INSERT, DELETE, UPDATE statement three kinds of basic forms: INSERT INTO table_name (col_1, col_2, col_3,) VALUES (value_1, value_2, value_3 ...) DELETE FROM table_name WHERE col_n = value_nUPDATE table_name SET col_1 = value_1, col_2 = value_2 , col_3 = value_3 where col_x = value_x We know that borrowing the request.getParameterNames () method in J2EE, you can read the value of the element name Borrowing the Request.GetParameter (ElementName) method to get the value of the element . Assume that we will match the name of the page element and the field name of the underlying database table. Then, in these three statements are not unknown for us, unknown data remains Table_Name, COL_X, and Value_x. Now if we write a method, incoming the Request object, take Table_name, Col_x, Value_x as a parameter into the method, then we can easily construct the SQL statement. But doing so is still flexible, because every hand uses this method we have to operate Table_name, col_x and value_x; on the other hand, don't forget that the field of the string needs to add single quotes and replace strings in the SQL statement. Middle single quotes, integrity, floating point, system functions (such as now (), to_date () and other database functions such as single quotes, if there is no good solution, our method will be subject to Very big restriction. To reach further separation is the best way to make an article on the form element, we can define a set of elements naming rules, different processing for different rules named - set up our definition element naming specifications as follows: 1. Table_name, col_x, value_x This type of element is common elements. We stipulate that this type of element name begins with C_K, and we limit the element name of Table_name. INSERT, DELETE, UPDATE SQL statement. We named C_Genre to this element, which is limited in INSERT, DELETE, and UPDATE. 2. For elements of the corresponding database string type in the form, the processing of single quotes is required in the SQL configuration. This type of element we temporarily called the string type element. String elements We specify its named S_ Database Table Field Name (S = String). 3. For elements that do not need to do but quotation, such as Integer, FLOAT, database system functions, such as now (), to_date (), etc.). We temporarily and simple collective such elements for integer elements. For integer elements, we restrict its naming rules for i_ database table field name (i = integer). Based on the above specifications we can easily write a JavaBean.
Code is as follows: / ** * @version: 1.1 * @Time:. 2005.03.02 * / package com.river.page; import java.util *; import javax.servlet.http.HttpServletRequest; public class PageUtil {private HttpServletRequest request = null; public PageUtil () {} public void init (HttpServletRequest _request) {this.request = _request;} public void clear () {if (this.request = null!) {this.request = null;}} public String get (String elementName) {if (request == null || request.getParameter (elementName) == null) {return "";} else {return request.getParameter (elementName);}} public String get (HttpServletRequest _request, String elementName) {init (_request); return get (elementName);} public String getSQL (HttpServletRequest _request) {init (_request); return getSQL ();} public String getSQL () {String sqlstr = ""; String c_table = get ("c_table"); string c_genre = GET ("c_genre"); string c_where = GET ("c_where"); if (c_genre == null || c_genre.equals (")) { Return "The action is null / empty";} if (c_table == null || c_table.equals (")) {RETURN" unknow table / empty ";} f (c_genre.equalsignorecase (" insert ") {Java .util.Enumeration arg_names = request.getParameterNames (); String colstr = "", valstr = ""; String arg_name, pre_name, end_name; while (arg_names.hasMoreElements ()) {arg_name = String.valueOf (arg_names.nextElement () ); If (arg_name.length () <2) {Continue;} pre_name = arg_name.substring (0, 2); End_name =
Arg_name.substring (2); if (pre_name.equalsignorecase ("i _")) {color = colstr "," END_NAME; IF (GET (Arg_Name) .Equals (")) {Valstr = Valstr ", NULL "; } else {valStr = Valstr "," String.Valueof (GET (arg_name));}} else f (pre_name.equalsignore ") {colstr = colstr ", " end_name; if (Get (get (arg_name) ) .Equals (")) {Valstr = Valstr ", null ";} else {valstr = valstr ", '" get (arg_name) .ReplaceAll ("' "," '") "' ";} }}} F (! ") {Color = colstr.substring (1); valstr = valstr.substring (1);} SQLSTR =" INSERT INTO " C_TABLE " (" Colstr ") VALUES " valstr ") "; return sqlstr;} else if (c_genre.equalsIgnoreCase (" UPDATE ")) {java.util.Enumeration arg_names = request.getParameterNames (); String colstr =" "; String arg_name, pre_name, end_name; While (arg_names.hasmorelements ()) {arg_name = s Tring.valueof (arg_names.nextelement ()). Trim (); if (arg_name.length () <2) {Continue;} pre_name = arg_name.substring (0, 2); End_name = arg_name.substring (2); if (pre_name.equalsignorecase ("i _")) {IF (GET (arg_name) .Equals (")) {colstr =", end_name "= null";} else {color = ","