2017-10-16 107 views
-1

我是新來的。春天也是新的。我想用spring和hibernate插入數據到數據庫中

我想使用spring和hibernate將數據插入到我的數據庫中。你能告訴我我的代碼有什麼問題嗎?

//this is my bean class!!!!!!!!! 

    package com.bean; 

    public class FormBean { 

private String name; 
private String email; 
private String contact; 
private String address; 
private String gender; 
private String items; 

/*public FormBean(String name, String email, String contact, String address, 
     String gender, String items) { 
    super(); 
    this.name = name; 
    this.email = email; 
    this.contact = contact; 
    this.address = address; 
    this.gender = gender; 
    this.items = items; 
}*/ 


public String getName() { 
    return name; 
} 


public void setName(String name) { 
    this.name = name; 
} 


public String getEmail() { 
    return email; 
} 


public void setEmail(String email) { 
    this.email = email; 
} 


public String getContact() { 
    return contact; 
} 


public void setContact(String contact) { 
    this.contact = contact; 
} 


public String getAddress() { 
    return address; 
} 


public void setAddress(String address) { 
    this.address = address; 
} 


public String getGender() { 
    return gender; 
} 


public void setGender(String gender) { 
    this.gender = gender; 
} 


public String getItems() { 
    return items; 
} 


public void setItems(String items) { 
    this.items = items; 
} 

    } 

這裏是我的控制器類,我無法在控制器類中創建dao對象。 如何調用dao類將數據導入數據庫。

 package com.controller; 

     import java.util.List; 

     import org.springframework.beans.factory.annotation.Autowired; 
     import org.springframework.stereotype.Controller; 
     import org.springframework.ui.ModelMap; 
     import org.springframework.web.bind.annotation.ModelAttribute; 
    import org.springframework.web.bind.annotation.PathVariable; 
    import org.springframework.web.bind.annotation.RequestMapping; 
    import org.springframework.web.bind.annotation.RequestMethod; 
    import org.springframework.web.bind.annotation.SessionAttributes; 
import org.springframework.web.servlet.ModelAndView; 

    import com.bean.FormBean; 
import com.dao.FormBeanDAO; 

@SessionAttributes 
@Controller 
public class PController{ 
@Autowired 
FormBeanDAO formbeandao; 



/*@RequestMapping("/hello") 
public ModelAndView hello(){ 
    String message="just to display this message..."; 
    return new ModelAndView("hello","message", message); 
}*/ 


/*@RequestMapping(value="/page1", method=RequestMethod.GET) 
public ModelAndView page1(){ 
    String msg="main jsp page"; 
    System.out.println(msg); 
    return new ModelAndView("","command",new FormBean());  
    }*/ 


    @RequestMapping(value = "/page1", method = RequestMethod.POST) 
     public String addformdetails(@ModelAttribute("page1")FormBean form, ModelMap model){ 
     model.addAttribute("name", form.getName()); 
     model.addAttribute("email", form.getEmail()); 
     model.addAttribute("contact", form.getContact()); 
     model.addAttribute("address", form.getAddress()); 
     model.addAttribute("gender", form.getGender()); 
     model.addAttribute("items", form.getItems()); 
     System.out.println(form.getName()+" "+form.getEmail()+" "+form.getContact()+" "+form.getAddress()+" "+form.getGender()+" "+form.getItems()); 
     //formbeandao. 
     return "userinput"; 

    } 


    /*@RequestMapping(value="/save",method = RequestMethod.POST) 
    public ModelAndView save(@ModelAttribute("emp") FormBean emp){ 
     dao.save(emp); 
     return new ModelAndView("redirect:/viewemp");//will redirect to viewemp request mapping 
    } 


    @RequestMapping("/viewemp") 
    public ModelAndView viewemp(){ 
     List<FormBean> list=dao.get; 
     return new ModelAndView("viewemp","list",list); 
    } 

     @SuppressWarnings("unused") 
     PrintWriter pw=res.getWriter(); 
     @SuppressWarnings("unused") 
     PrintWriter out = res.getWriter(); 

     RequestDispatcher rd = null;  

     String name = req.getParameter("fullname"); 
     String email = req.getParameter("email"); 
     String contact = req.getParameter("contact"); 
     String address = req.getParameter("address"); 
     String gender = req.getParameter("gender"); 
     String items = req.getParameter("items"); 

     System.out.println(name+" "+email+" "+contact+" "+address+" "+gender+" "+items); 


     FormBean fb = new FormBean(name,email,contact,address,gender,items);  
     FormBeanDAO fbd = new FormBeanDAO(); 

     boolean b=fbd.registration(fb); 
     if(b==true){ 
      rd=req.getRequestDispatcher("onejsp.jsp"); 
      rd.forward(req, res); 
      System.out.println("success "); 
     } 
     else{ 
     rd=req.getRequestDispatcher("twojsp.jsp"); 
     rd.forward(req, res); 
     System.out.println("failure"); 
     }   
        */ 
    } 




// below is DAO class 

package com.dao; 

//import java.sql.*; 
//import java.sql.PreparedStatement; 

import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.Transaction; 
import org.hibernate.cfg.Configuration; 
/*import java.sql.ResultSet; 
import java.sql.Statement; 
import java.util.LinkedList; 
import java.util.List; 
*/ 
import com.bean.FormBean; 

//import com.util.DBUtil; 


public class FormBeanDAO { 

public void registration (FormBean ab){ 


    System.out.println("starting of dao class"); 
    Configuration cfg=new Configuration(); 
    cfg.configure("hibernate.cfg.xml"); 
    SessionFactory factory=cfg.buildSessionFactory(); 
    Session session=factory.openSession(); 
    Transaction t=session.beginTransaction(); 

    session.save(ab); 

    t.commit(); 
    session.close(); 
    System.out.println("dao class is working fine"); 



} 

} 


    /*Session session=new Configuration().configure().buildSessionFactory().openSession(); 
    Transaction t=session.beginTransaction(); 
    t.begin(); 
    */ 



    //boolean result=false; 
    //Connection con = DBUtil.getConObject(); 



/*Session session=new Configuration().configure().buildSessionFactory ().openSession(); 

    Transaction t=session.beginTransaction(); 
    t.begin(); 

    i=(Integer)session.save(u); 

    t.commit(); 
    session.close();*/ 

    /* try{  
    PreparedStatement ps = null; 
    ps=con.prepareStatement("insert into miniproject values(?,?,?,?,?,?)"); 

    ps.setString(1,ab.getName());  
    ps.setString(2,ab.getEmail()); 
    ps.setString(3,ab.getContact()); 
    ps.setString(4,ab.getAddress()); 
    ps.setString(5,ab.getGender());  
    ps.setString(6,ab.getItems()); 


    int noofrows = ps.executeUpdate(); 
    con.commit(); 

    if(noofrows==1){ 
     result=true; 
    } 
} 
    catch(Exception e){ 
      e.printStackTrace(); 
      try{ 
      //con.rollback(); 
      }catch(Exception e1){ 
       e1.printStackTrace(); 
      } 

     }finally{ 
      try{ 
      con.close(); 
      }catch(Exception e){ 
       e.printStackTrace(); 
      }`enter code here` 
     } 
    return result; 
} 

}*/ 

enter image description here

回答

0

您不需要添加所有的reuqest參數應用在model.What是用呢?剛剛設置的FormBean類對象的參數應用和持久化對象,

第一創建一個方法,將綁定您的模型與表單屬性

@RequestMapping("/hello") //it will open the registeration page 
public Stringhello(Model model){ 
    model.addAttribute("page1",new FormBean()); 
    return "jsp corresponding to /page1 action" 
} 


    @RequestMapping(value = "/page1", method = RequestMethod.POST) 
      public String addformdetails(@ModelAttribute("page1")FormBean form, ModelMap model){ 


form.setname(form.getName()); 
form.setMail(form.getMail()); 
//do for every field 
      System.out.println(form.getName()+" "+form.getEmail()+" "+form.getContact()+" "+form.getAddress()+" "+form.getGender()+" "+form.getItems()); 
      formbeandao.registration(form) 
      return "userinput"; 

     } 
相關問題