<jsp:useBean id="table" class="table1_bean" /> <jsp:setProperty name="table" property="*" /> (注:table1_bean为上述自动生成的对应某一个表的JavaBean) 然后调用table1_bean中insert、update、delete函数完成数据表存储,并返回执行结果。如: <%boolean success =table.insert(); %> |
<Form Name=Frm1 Method=Post Action="DBdataStore.jsp? tablename=table1&OperType=…"> |
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为数据表的属主。 |
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'; |
String tablename=request.getParameter("tablename");//提取表名 String OperType=request.getParameter("OperType");//提取操作类型 String sFieldValue="";//存放表单提交的字段数据值 String fieldname="",Datatype="" //存放字段名,字段数据类型 int iFieldvalue=0; String updateSql="",whereSql=" where ",insSql1="",insSql2="",opSql="",strSql =""; ResultSet rs1=null,rs2=null; insSql1="insert into "+tablename+" ("; insSql2="values("; |
rs1=Stmt.executeQuery("SELECT column_name FROM v_pkey_column WHERE table_name='"+tablename+"'");//取关键字段字段名 while(rs1.next()){ fieldname=rs1.getString("column_name"); rs2=Stmt.executeQuery("SELECT data_type FROM v_dbstru WHERE table_name='"+tablename+"' AND column_name='"+fieldname+"'");//取关键字段数据类型 if(rs2.next()){ Datatype=rs2.getString("data_type"); sFieldValue=request.getParameter(fieldname.toLowerCase()); //生成insert语句关键字段部分 if(OperType.equals("insert")){ insSql1+=fieldname+","; if((sFieldValue==null) ){ //表单未提交关键字段数据值时,本文只按数字型处理,数据值按下一流水号计算。 rs2= Stmt. executeQuery("SELECT max("+fieldname+")+1 FROM "+tablename);rs2. next();iFieldvalue=rs2.getInt(1);insSql2+=Integer.toString(iFieldvalue)+","; }else if(Datatype.equals("DATE")){ insSql2+= "To_Date('" + sFieldValue + "','YYYY-MM-DD'),"; }else if(Datatype.equals("VARCHAR2") || Datatype.equals("CHAR")){ insSql2+="'" + sFieldValue+"',";} else /*NUMBER,FLOAT */ insSql2+=sFieldValue+",";} //生成update,delete语句where部分:where fieldname=... AND if(OperType.equals("update") || OperType.equals("delete")){ if(Datatype.equals("DATE")){ whereSql+=fieldname+"=To_Date('" + sFieldValue + "','YYYY-MM-DD') AND "; }else if(Datatype.equals("VARCHAR2") || Datatype.equals("CHAR")){ whereSql+=fieldname+"='" + sFieldValue+"' AND ";} else /*NUMBER,FLOAT */ whereSql+=fieldname+"="+ sFieldValue+" AND ";} } } whereSql=whereSql.substring(0,whereSql.length()-4); |
updateSql="update "+tablename+" set "; strSql="SELECT column_name,data_type,data_length,data_precision,data_scale FROM v_dbstru a "+"where table_name='"+tablename+"' AND a.column_name not in (SELECT b.column_name FROM v_pkey_column b where b.table_name=a.table_name)"; rs1=Stmt.executeQuery(strSql);//取非关键字段字段名和数据类型 while(rs1.next()){ fieldname=rs1.getString("column_name");Datatype=rs1.getString("data_type"); sFieldValue=request.getParameter(fieldname.toLowerCase());//若表单未提交该字段的值,则忽略该字段的处理 if((sFieldValue!=null)){ //生成insert语句=insSql1+insSql2 即insert into tablename(… 和 values(… if(OperType.equals("insert")){ insSql1+=fieldname+","; if(Datatype.equals("DATE")){ insSql2+= "To_Date('" + sFieldValue + "','YYYY-MM-DD'),"; } else if(Datatype.equals("VARCHAR2") || Datatype.equals("CHAR")){ insSql2+="'" + sFieldValue+"',";}else /*NUMBER,FLOAT*/ insSql2+= sFieldValue+",";} //生成update语句=updateSql+whereSql 即update tablename set ... where fieldname=... if(OperType.equals("update")){ if(Datatype.equals("DATE")){ updateSql+=fieldname+"=To_Date('" + sFieldValue + "','YYYY-MM-DD'),"; }else if(Datatype.equals("VARCHAR2") || Datatype.equals("CHAR")){ updateSql+=fieldname+"='" + sFieldValue,1}+"',";}else /*NUMBER,FLOAT*/ updateSql+=fieldname+"="+sFieldValue+",";} )) rs1.close(); |
if(OperType.equals("insert")) opSql=insSql1.substring(0,insSql1.length()-1)+")"+insSql2.substring(0,insSql2.length()-1)+")"; if(OperType.equals("update")) opSql=updateSql.substring(0,updateSql.length()-1)+" "+whereSql;if(OperType.equals("delete")) opSql="delete FROM "+tablename+" "+whereSql; //已生成完整的sql语句opSql try{sqlnrows=Stmt.executeUpdate(opSql);} catch(SQLException e){out.println("SQLException:"+opSql);} |