0

我无法将值插入数据库,但我的编码是正确的。在我的编码中,我无法保存会话。我无法修复错误。请帮我修复错误。Cadd.java

package controller;
import com.mycompany.strutsfinal.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ActionContext;
import java.util.Map;
import java.util.regex.Pattern;
import org.apache.struts2.ServletActionContext;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

/**
 *
 * @author joshiba
 */
public class Cadd extends ActionSupport
{
     public SessionFactory factory; 
     private String custname;
     private String custaddr;
     private static java.sql.Timestamp getCurrentTimeStamp() {
        java.util.Date today = new java.util.Date();
        return new java.sql.Timestamp(today.getTime());
     }

   @Override
   public String execute()
   {
     HttpServletRequest request=(HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);


      String Custname = request.getParameter("cusname");

      String Custaddr = request.getParameter("cusaddrs")    
      Session session=null;
      session = NewHibernateUtil.getSessionFactory().openSession();
      Transaction tx = null;
      Integer cus=null;

      try{
          tx = session.beginTransaction();

          Customer cust12id = new Customer("custname","custaddr",0,getCurrentTimeStamp(),null,null);

        session.save(cust12id);
        Customer c=new Customer();
        addActionMessage("inserted successfully");
        tx.commit();
        if(cus==null)
          addActionMessage("inserted successfully");
        else
            addActionMessage("insert not successfully");
         return "success";
        }catch (HibernateException e) {
         if (tx!=null) 
            tx.rollback();
         e.printStackTrace(); 
      }finally {
         session.close(); 
      }

      return "success"; 
    }

    /**
     * @return the custname
     */
    public String getCustname() {
        return custname;
    }

    /**
     * @param custname the custname to set
     */
    public void setCustname(String custname) {
        this.custname = custname;
    }

    /**
     * @return the custaddr
     */
    public String getCustaddr() {
        return custaddr;
    }

    /**
     * @param custaddr the custaddr to set
     */
    public void setCustaddr(String custaddr) {
        this.custaddr = custaddr;
    }


}
4

1 回答 1

0

好吧,他们应该进行一些代码更改以使其正常工作...

Integer cus =session.save(cust12id); 
  System.out.println(cus);


 Now commit and retrieve the same object(for testing purpose only this approach) 
于 2013-11-13T17:13:52.987 回答