资源描述
一个hibernate登录注册工程 HibernateMySQLJSPXMLSQL项目名称:loginandregister. 其目录: loginandregister |-src | -hibernate.yan | -LoginServlet.java |-RegisterServlet.java | - User.java | -WriterDB.java | -user.hbm.xml | -hibernate.cfg.xml |-jre sytem library |-J2EE 1.4 Library |-Referenced Library |-WebRoot |-Weta-inf |-WEB-INF |-lib |-web.xml |-login.jsp |-register.jsp 新建web project,命名为loginandregister,右击工程名,MyeclispeAdd hibernate capabilities 将hibernate的jar包加入,当然也可以通过项目属性构建路径,加入。 web.xml内容为: login.jsp RegisterServlet hibernate.yan.RegisterServlet RegisterServlet /RegisterServlet LoginServlet hibernate.yan.LoginServlet LoginServlet /LoginServlet user.hbm.xml内容为: hibernate.cfg.xml为: org.hibernate.dialect.MySQLDialect jdbc:mysql:/localhost/lrdatabase?useUnicode=true&characterEncoding=UTF-8 root 123456 com.mysql.jdbc.Driver thread org.hibernate.cache.NoCacheProvider true login.jsp: 登录页面 form action=/LoginServlet 用户登录 username: password: 注 册 register.jsp: 注册页面 注册 form id=form1 name=form1 method=post action=/RegisterServlet 请输入名称: 请输入密码: 请选择手机型号: phone1 phone2 phone3 loginServlet.java: public class LoginServlet extends HttpServlet protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException / TODO Auto-generated method stub doPost(req, resp); protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException / TODO Auto-generated method stub resp.setContentType(text/html;charset=utf-8); String name=req.getParameter(username); String ssname = new String(name.getBytes(iso-8859-1),utf-8); System.out.println(ss=: + ssname); String password=req.getParameter(password); Transaction tx=null; Session session=null; PrintWriter out=resp.getWriter(); String sql=from User where name=+ssname+ and password=+password+; if(true) try Configuration conf=new Configuration().configure(); SessionFactory sf=conf.buildSessionFactory(); session=sf.openSession(); tx=session.beginTransaction(); System.out.println(sql); Query query=session.createQuery(sql); List list=(List) query.list(); tx.commit(); Iterator it=list.iterator(); if(it.hasNext() User user=(User) it.next(); System.out.println(user.getName()+ +user.getPassword()+ +user.getPhonetype(); out.println(); out.println(login); out.println(); out.println(successfully login: name:+user.getName()+ password:+user.getPassword()+ phonetype:+user.getPhonetype(); out.println(); out.close(); else out.println(); out.println(login); out.println(); out.println(不存在此用户信息); out.println(); out.close(); catch(Exception e) e.printStackTrace(); else out.println(); out.println(login); out.println(); out.println(请输入信息完整); out.println(); out.close(); RegisterServlet.java public class RegisterServlet extends HttpServlet protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException / TODO Auto-generated method stub doPost(req, resp); protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException / TODO Auto-generated method stub resp.setContentType(text/html;charset=utf-8); String name=req.getParameter(name); String password=req.getParameter(password); String phonetype=req.getParameter(phonetype); String ssname = new String(name.getBytes(iso-8859-1),utf-8); System.out.println(ssname+ +password); /int numofphonetype=Integer.getInteger(phonetype).intValue();/这句话导致异常 int a=Integer.parseInt(phonetype); PrintWriter out=resp.getWriter(); try if(ssname!=&password!=&phonetype!=) WriteDB writer=new WriteDB(); writer.WriteLogToDB(ssname, password, a); out.print(注册成功); else out.print(请填写完整!); catch(Exception e) out.print(Error :+e.getMessage(); public void destroy() / TODO Auto-generated method stub super.destroy(); public void init() throws ServletException / TODO Auto-generated method stub super.init(); WriterDB.java public class WriteDB public static SessionFactory sf; static try Configuration conf=new Configuration().configure(); sf=conf.buildSessionFactory(); System.out.println(sf=conf.buildSessionFactory(); catch(Exception e) e.printStackTrace(); public void WriteLogToDB(String name,String password,int phonetype) User user=new User(); user.setName(name); user.setPassword(password); user.setPhonetype(phonetype); Session sessions=sf.openSession(); Transaction tran=null; try tran=sessions.beginTransaction(); sessions.save(user); tran.commit();/提交 / System.out.println(已正常提交数据:+con.getId()+ ,+con.getLogcontent()+ ,+con.getLogdate(); catch(Exception e) if(tran!=null) tran.rollback(); finally sessions.close(); User.java public class User private int id; private String name; private String password; private int phonetype; public User() public int getId() return id; public void setId(int id) this.id = id; public String getName() return name; public void setName(String name) this.name = name; public String getPassword() return password; public void setPassword(String password) this.password = password; public int getPhonetype() return phonetype; public void setPhonetype(int phonetype) this.phonetype = phonetype; 数据库脚本: /* MySQL Data Transfer Source Host: localhost Source Database: lrdatabase Target Host: localhost Target Database: lrdatabase Date: 2010-4-1 14:51:51 */ SET FOREIGN_KEY_CHECKS=0; - - - Table structure for user - - CREATE TABLE user ( id int(11) NOT NULL AUTO_INCREMENT, name varchar(20) CHARACTER SET utf8 DEFAULT NULL, password varchar(20) CHARACTER SET utf8 DEFAULT NULL, phonetype tinyint(11) DEFAULT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf-8;
展开阅读全文