Je suis nouveau ici. Aussi nouveau au printemps.Je veux insérer des données dans la base de données en utilisant le printemps et hiberner
Je souhaite insérer des données dans ma base de données en utilisant le mode Spring et Hibernate. pouvez-vous s'il vous plaît dites-moi ce qui ne va pas avec mon code?
//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;
}
}
Voici ma classe de contrôleur, je ne suis pas capable de créer un objet dao dans la classe de contrôleur. comment appeler la classe dao pour inter-données dans la base de données.
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;
}
}*/