使用场景:关闭页面弹窗时执行sql语句。
其实js里执行sql语句有多种方式。
方式一:直接在js代码里调用sql语句,原则上不能使用,因为这将sql直接暴露在客户端,安全性极差。
方式二:在js里运用ajax技术,调用后台方法执行sql语句。
方式三:运用dwr框架,其实是对ajax技术进行了封装。
一、首先编写一个dwr.xml文件
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://www.getahead.ltd.uk/dwr//dwr20.dtd"> <dwr> <allow> <!-- 对呼叫类型的操作 --> <create javascript="CallTypeOpDwr" creator="new"> <param name="class" value="cn.etcom.jfgl.tel.fee.callType.CallTypeOpDwr" /> <include method="addCallType" /> <include method="modifyCallType" /> <include method="deleteCallType" /> <include method="updateSp" /> </create> </allow> <!-- 用户信息管理操作中用到的map配置 --> <signatures> <![CDATA[ import java.util.List; import cn.etcom.jfgl.customer.tel.userInfo.UserInfoManageOpDwr; UserInfoManageOpDwr.addUsersInfo(String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,Map<String,String>); UserInfoManageOpDwr.addUserInfo(String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,Map<String,String>); ]]> </signatures> </dwr>
二、在web.xml文件里配置servlet
<!-- DWR相关配置 --> <servlet> <servlet-name>dwr-invoker</servlet-name> <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class> <init-param> <param-name>debug</param-name> <param-value>false</param-value> </init-param> <init-param> <param-name> allowGetForSafariButMakeForgeryEasier </param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>crossDomainSessionSecurity</param-name> <param-value>false</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dwr-invoker</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping>
三、写一个普通的JAVA类
package cn.etcom.jfgl.tel.fee.callType; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; import org.json.JSONException; import org.json.JSONObject; import cn.etcom.common.utility.DBUtil; import cn.etcom.common.utility.RsHandler; import cn.etcom.common.utility.SqlActuator; import cn.etcom.common.utility.StringUtil; /******************************************************************************* * * @author 刘彦宁 2011-08-12 * */ // 处理呼叫类型的dwr public class CallTypeOpDwr { /*************************************************************************** * DWR 方法,执行存储过程。 * */ public String updateSp() { String sql = "exec Pub_Sp_UpdateFieldMemo ?"; Connection conn = DBUtil.getConnection(); PreparedStatement psmt = null; try { psmt = conn.prepareStatement(sql); psmt.setString(1, "CALL"); boolean a = psmt.execute(); DBUtil.close(null, psmt, conn); conn = DBUtil.getConnection(); psmt = conn.prepareStatement(sql); psmt.setString(1, "USER"); boolean b = psmt.execute(); System.out.println(a); System.out.println(b); System.out.println("关闭弹窗执行存储过程 >> "+sql); } catch (SQLException e) { e.printStackTrace(); }finally { DBUtil.close(null, psmt, conn); } return null; } }