一个hibernate登录注册工程.doc

上传人:jian****018 文档编号:8132086 上传时间:2020-03-27 格式:DOC 页数:11 大小:35KB
返回 下载 相关 举报
一个hibernate登录注册工程.doc_第1页
第1页 / 共11页
一个hibernate登录注册工程.doc_第2页
第2页 / 共11页
一个hibernate登录注册工程.doc_第3页
第3页 / 共11页
点击查看更多>>
资源描述
一个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;
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 建筑环境 > 建筑工程


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!