2008年(3500)
分类:
2008-05-04 19:43:05
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_n
UPDATE table_name SET col_1=value_1,col_2=value_2,col_3=value_3 WHERE col_x=value_x
2. 对于表单中对应数据库字符串类型的元素,在SQL构造中需要做单引号的处理。这类元素我们暂且称他们为字符串型元素。字符串型元素我们规定其名为s_ 数据库表字段名 (s=String)。
基于上面的规格我们可以非常轻松写一个javabean。代码如下:
/**
* @version: 1.1
* @Time: 2005.03.02
*/
package com.river.page ;
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" ;
}
if(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_")){
colstr = colstr "," end_name;
if(get(arg_name).equals("")){
valstr = valstr ",NULL";
}else{
valstr = valstr "," String.valueOf(get(arg_name));
}
}else if(pre_name.equalsIgnoreCase("s_")){
colstr = colstr "," end_name;
if(get(arg_name).equals("")){
valstr = valstr ",NULL";
}else{
valstr = valstr ",'" get(arg_name).replaceAll("'","''") "'";
}
}
}
if(!colstr.equals("")){
colstr = 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.hasMoreElements()){
arg_name = String.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{
colstr = "," end_name "=" get(arg_name);
}
}else if(pre_name.equalsIgnoreCase("s_")){
if(get(arg_name).equals("")){
colstr = "," end_name "=" get(arg_name);
}else{
colstr = "," end_name "='" get(arg_name).replaceAll("'","''") "'";
}
}
}
if(!colstr.equals("")){
colstr = colstr.substring(1);
}
sqlstr = "UPDATE " c_table " SET " colstr;
if(!c_where.equals("")){
sqlstr = " WHERE " c_where;
}
return sqlstr;
}else if(c_genre.equalsIgnoreCase("DELETE")){
sqlstr = "DELETE FROM " c_table;
if(c_where != null && !c_where.equals("")){
sqlstr = " WHERE " c_where;
}
}else{
com.river.debug.Debug.show("unknow action type : " c_genre);
return null;
}
return sqlstr;
}
public String toString(){
return "version 1.0, date 2005.03.02, author river";
}
}
这样我们就可以根据页面元素的命名来指导SQL语句的生成。这样做有很多的明显的好处:
2.通用、稳定、易于维护,javabean固有的优点,就不用太多的说明了。