前提xml中各组件已连上了,个常量也对应好了下面的代码可是不能删除啊???
1.
package addressbook.forms;import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;public final class DelForm extends ActionForm { private String name = null;
private String phone = null;
private String address = null;
private String hunk = null;
private String delId = null;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
} public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getHunk() {
return address;
}
public void setHunk(String hunk) {
this.hunk = hunk;
}
public String getDelId() {
return delId;
}public void reset(ActionMapping mapping, HttpServletRequest request) { //name = null;
//phone = null;
//address = null;
}
public void setDelId(String delId) {
this.delId = delId;
}public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) { ActionErrors errors = new ActionErrors();
if (((delId == null)))
errors.add("name", new ActionMessage("error.name.required"));
// if(((phone == null)|| (phone.length() < 1)))
// errors.add("phone", new ActionMessage("error.phone.required"));
//if(((address == null)|| (address.length() < 1)))
// errors.add("address", new ActionMessage("error.address.required")); return errors; }2.
2.import addressbook.Constants;
包都蹈进了....
public final class DelAction extends AbstActionBase { private Log log =
LogFactory.getLog(this.getClass().getName()); public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception { Locale locale = getLocale(request);
MessageResources messages = getResources(request); ActionMessages errors = new ActionMessages();
String name =((DelForm) form).getName();
String delId = ((DelForm) form).getDelId();//
//String deleteId ="8";
String phone = ((DelForm) form).getPhone();
String address=((DelForm)form).getAddress();
String hunk = ((DelForm) form).getHunk(); if (!errors.isEmpty()) {
//saveErrors(request, errors);
return (new ActionForward(mapping.getInput()));
} String strSql = new String("delete FROM " + Constants.TABLENAME + " WHERE ");
//error:IOException:java.sql.SQLException: Can not issue data manipulation statements with executeQuery() //if (!name.equals(""))
strSql = strSql + "name = "+ name +" ";//id + delId +
//if (!phone.equals(""))
//strSql = strSql + " phone LIKE '"+ phone +"%' AND";
//if (!address.equals(""))
//strSql = strSql + " address LIKE '"+ address +"%'";
//else
//strSql = strSql.substring(0,strSql.length()-3); //strSql = strSql + "ORDER by ID"; //java.lang.NullPointerException
strSql = strSql.substring(0,strSql.length());//删除 //try
//{
AddressBookBean bean=new AddressBookBean(delId,phone,address,hunk,delId);
//bean.delete(strSql);
AddressBookBean.delete(strSql);//访问静态??
//}
//catch(Exception ex)
//{
// ex.printStackTrace(System.out);
// errors.add(ActionMessages.GLOBAL_MESSAGE,
// new ActionMessage('error.ioException'));
// }
HttpSession session = request.getSession(); //??????????
if (log.isDebugEnabled()) {
log.debug("SearchAction session = " + session);
log.debug("SearchAction strSql = " + strSql); }
session.setAttribute(Constants.SQLSTMT_KEY, strSql);//SQLSTMT_KEY = "sqlstatement"???????? return (mapping.findForward(Constants.FORWARD_DELOK));//FORWARD_SUCCESS }
}
}
3.
包已导入
public class AddressBookBean { String name;
String phone;
String address;
String hunk;
String id; // public AddressBookBean() {
}
public AddressBookBean(String name,String phone,String address,String hunk,String id) {
this.name=name;
this.phone=phone;
this.address=address;
this.hunk=hunk;
this.id=id; //
}
public String getName(){
return name;
}
public String getPhone(){
return phone;
}
public String getAddress(){
return address;
}
public String getHunk(){
return hunk;
}
public String getId(){
return id; ///
}
public void setName(String name){
this.name=name;
}
public void setPhone(String phone){
this.phone=phone;
}
public void setAddress(String address){
this.address=address;
}
public void setHunk(String hunk){
this.hunk=hunk;
}
public void setId(String id){
this.id=id;
} /////////
///////////////////////////////////////
public static Vector delete(String strSql)throws Exception{
Vector addressbookBeans=new Vector();
Connection con= DbUtil.connectToDb();
PreparedStatement pStmt=null;
ResultSet rs=null;
try{
pStmt=con.prepareStatement(strSql);
con.setAutoCommit(false);
pStmt.executeUpdate();
con.commit();
rs=pStmt.executeQuery();
while(rs.next()){//
String sname=rs.getString("NAME");
String sphone=rs.getString("PHONE");
String saddress=rs.getString("ADDRESS");
String shunk=rs.getString("HUNK");
addressbookBeans.add(new AddressBookBean( sname,sphone,saddress,shunk,rs.getString("ID")));
}//
return addressbookBeans;
}finally{
try{
rs.close();
pStmt.close();
con.close();
}catch(Exception e){e.printStackTrace();}
}
}
///////////////////////////////////////
public static Vector search(String strSql)throws Exception{
Vector addressbookBeans=new Vector();
Connection con= DbUtil.connectToDb();
PreparedStatement pStmt=null;
ResultSet rs=null;
try{
pStmt=con.prepareStatement(strSql);
rs=pStmt.executeQuery();
while(rs.next()){//
String sname=rs.getString("NAME");
String sname_zh=new String(sname.getBytes("iso-8859-1"),"gb2312");
String sphone=rs.getString("PHONE");
String sphone_zh=new String(sphone.getBytes("iso-8859-1"),"gb2312");
String saddress=rs.getString("ADDRESS");
String saddress_zh=new String(saddress.getBytes("iso-8859-1"),"gb2312");
String shunk=rs.getString("HUNK");
String shunk_zh=new String(shunk.getBytes("iso-8859-1"),"gb2312");
addressbookBeans.add(new AddressBookBean(
sname_zh,sphone_zh,saddress_zh,shunk_zh,rs.getString("ID")));//rs.getString("NAME"),rs.getString("PHONE"),rs.getString("ADDRESS")shunk_zh
}
return addressbookBeans;
}finally{
try{
rs.close();
pStmt.close();
con.close();
}catch(Exception e){e.printStackTrace();}
}
}
}
输入http://localhost:8080/addressbook_o/del.do?delId=8后,只是查出了该条记录,并未删除该条记录啊???????
1.
package addressbook.forms;import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;public final class DelForm extends ActionForm { private String name = null;
private String phone = null;
private String address = null;
private String hunk = null;
private String delId = null;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
} public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getHunk() {
return address;
}
public void setHunk(String hunk) {
this.hunk = hunk;
}
public String getDelId() {
return delId;
}public void reset(ActionMapping mapping, HttpServletRequest request) { //name = null;
//phone = null;
//address = null;
}
public void setDelId(String delId) {
this.delId = delId;
}public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) { ActionErrors errors = new ActionErrors();
if (((delId == null)))
errors.add("name", new ActionMessage("error.name.required"));
// if(((phone == null)|| (phone.length() < 1)))
// errors.add("phone", new ActionMessage("error.phone.required"));
//if(((address == null)|| (address.length() < 1)))
// errors.add("address", new ActionMessage("error.address.required")); return errors; }2.
2.import addressbook.Constants;
包都蹈进了....
public final class DelAction extends AbstActionBase { private Log log =
LogFactory.getLog(this.getClass().getName()); public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception { Locale locale = getLocale(request);
MessageResources messages = getResources(request); ActionMessages errors = new ActionMessages();
String name =((DelForm) form).getName();
String delId = ((DelForm) form).getDelId();//
//String deleteId ="8";
String phone = ((DelForm) form).getPhone();
String address=((DelForm)form).getAddress();
String hunk = ((DelForm) form).getHunk(); if (!errors.isEmpty()) {
//saveErrors(request, errors);
return (new ActionForward(mapping.getInput()));
} String strSql = new String("delete FROM " + Constants.TABLENAME + " WHERE ");
//error:IOException:java.sql.SQLException: Can not issue data manipulation statements with executeQuery() //if (!name.equals(""))
strSql = strSql + "name = "+ name +" ";//id + delId +
//if (!phone.equals(""))
//strSql = strSql + " phone LIKE '"+ phone +"%' AND";
//if (!address.equals(""))
//strSql = strSql + " address LIKE '"+ address +"%'";
//else
//strSql = strSql.substring(0,strSql.length()-3); //strSql = strSql + "ORDER by ID"; //java.lang.NullPointerException
strSql = strSql.substring(0,strSql.length());//删除 //try
//{
AddressBookBean bean=new AddressBookBean(delId,phone,address,hunk,delId);
//bean.delete(strSql);
AddressBookBean.delete(strSql);//访问静态??
//}
//catch(Exception ex)
//{
// ex.printStackTrace(System.out);
// errors.add(ActionMessages.GLOBAL_MESSAGE,
// new ActionMessage('error.ioException'));
// }
HttpSession session = request.getSession(); //??????????
if (log.isDebugEnabled()) {
log.debug("SearchAction session = " + session);
log.debug("SearchAction strSql = " + strSql); }
session.setAttribute(Constants.SQLSTMT_KEY, strSql);//SQLSTMT_KEY = "sqlstatement"???????? return (mapping.findForward(Constants.FORWARD_DELOK));//FORWARD_SUCCESS }
}
}
3.
包已导入
public class AddressBookBean { String name;
String phone;
String address;
String hunk;
String id; // public AddressBookBean() {
}
public AddressBookBean(String name,String phone,String address,String hunk,String id) {
this.name=name;
this.phone=phone;
this.address=address;
this.hunk=hunk;
this.id=id; //
}
public String getName(){
return name;
}
public String getPhone(){
return phone;
}
public String getAddress(){
return address;
}
public String getHunk(){
return hunk;
}
public String getId(){
return id; ///
}
public void setName(String name){
this.name=name;
}
public void setPhone(String phone){
this.phone=phone;
}
public void setAddress(String address){
this.address=address;
}
public void setHunk(String hunk){
this.hunk=hunk;
}
public void setId(String id){
this.id=id;
} /////////
///////////////////////////////////////
public static Vector delete(String strSql)throws Exception{
Vector addressbookBeans=new Vector();
Connection con= DbUtil.connectToDb();
PreparedStatement pStmt=null;
ResultSet rs=null;
try{
pStmt=con.prepareStatement(strSql);
con.setAutoCommit(false);
pStmt.executeUpdate();
con.commit();
rs=pStmt.executeQuery();
while(rs.next()){//
String sname=rs.getString("NAME");
String sphone=rs.getString("PHONE");
String saddress=rs.getString("ADDRESS");
String shunk=rs.getString("HUNK");
addressbookBeans.add(new AddressBookBean( sname,sphone,saddress,shunk,rs.getString("ID")));
}//
return addressbookBeans;
}finally{
try{
rs.close();
pStmt.close();
con.close();
}catch(Exception e){e.printStackTrace();}
}
}
///////////////////////////////////////
public static Vector search(String strSql)throws Exception{
Vector addressbookBeans=new Vector();
Connection con= DbUtil.connectToDb();
PreparedStatement pStmt=null;
ResultSet rs=null;
try{
pStmt=con.prepareStatement(strSql);
rs=pStmt.executeQuery();
while(rs.next()){//
String sname=rs.getString("NAME");
String sname_zh=new String(sname.getBytes("iso-8859-1"),"gb2312");
String sphone=rs.getString("PHONE");
String sphone_zh=new String(sphone.getBytes("iso-8859-1"),"gb2312");
String saddress=rs.getString("ADDRESS");
String saddress_zh=new String(saddress.getBytes("iso-8859-1"),"gb2312");
String shunk=rs.getString("HUNK");
String shunk_zh=new String(shunk.getBytes("iso-8859-1"),"gb2312");
addressbookBeans.add(new AddressBookBean(
sname_zh,sphone_zh,saddress_zh,shunk_zh,rs.getString("ID")));//rs.getString("NAME"),rs.getString("PHONE"),rs.getString("ADDRESS")shunk_zh
}
return addressbookBeans;
}finally{
try{
rs.close();
pStmt.close();
con.close();
}catch(Exception e){e.printStackTrace();}
}
}
}
输入http://localhost:8080/addressbook_o/del.do?delId=8后,只是查出了该条记录,并未删除该条记录啊???????
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货