<%@ page contentType="text/xml; charset=GB2312" %>
<%@ page import="xmlinterface.*" %>
<%@ page import="java.sql.*" %>
<%
Connection conn = null;
String sql = new String();
String where = new String();
java.sql.Statement stmt = null;
ResultSet rs = null;
ResultSetMetaData rsmd =null;
String acttype = null; //操作类型
XmlAction action = null; //一个修改动作
XmlPost xmlpost = new XmlPost();
try {
String drivers = "sun.jdbc.odbc.JdbcOdbcDriver";
System.setProperty("jdbc.drivers",drivers);
String username = "";
String password = "";
String url = "jdbc:odbc:jcldemo";
conn=DriverManager.getConnection(url,username,password);
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
conn.setAutoCommit(false); //自己处理事务
xmlpost.Open(request);
for(int i = 0;i<xmlpost.Actions.size();i++) {
action = xmlpost.GetAction(i);
acttype = action.ActionType;
if(acttype.equals("old")) { //有操作类型是old,用于定位数据,后面紧接的操作是update
where = " where ";
where = where+"SALE_ID="+action.GetField("SALE_ID").Value;
}
if(acttype.equals("update")){
String set = " set ";
for(int j=0;j<action.Fields.size();j++) {
if(action.GetField(j).Name.equals("SALE_PRICE")
||action.GetField(j).Name.equals("SALE_AMOUNT")) {
set = set+action.GetField(j).Name+"="+action.GetField(j).Value;
}
else {
set = set+action.GetField(j).Name+"='"+action.GetField(j).Value+"'";
}
if(j<action.Fields.size()-1) {
set = set+ "," ;
}
}
sql="update SALE"+set+where;
stmt.executeUpdate(sql);
}
if(acttype.equals("delete")){
where = " where ";
where = where+"SALE_ID="+action.GetField("SALE_ID").Value;
sql = "delete from SALE"+where;
stmt.executeUpdate(sql);
}
if(acttype.equals("insert")){
String field = new String();
String value = new String();
for(int j=0;j<action.Fields.size();j++) {
field = field+action.GetField(j).Name;
if(action.GetField(j).Name.equals("SALE_PRICE")
||action.GetField(j).Name.equals("SALE_AMOUNT")) {
value = value+action.GetField(j).Value;
}
else {
value=value+"'"+action.GetField(j).Value+"'";
}
if(j<action.Fields.size()-1) {
field = field+ "," ;
value = value+",";
}
}
sql="insert into SALE ("+field+") values ("+value+")";
stmt.executeUpdate(sql);
}
}
conn.commit(); //最后提交数据
out.println(xmlpost.GetXml()); //返回处理信息
} catch(Exception e) {
xmlpost.Error=e.getMessage();
out.println(xmlpost.GetXml()); //出错后,返回出错信息
e.printStackTrace() ;
return;
} finally {
if(rs != null)
rs.close();
if(stmt != null)
stmt.close();
if(conn != null)
conn.close();
}
%>