-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;
}
}*/