注册成功后会跳转到注册成功地页面
进入登陆页面
登陆成功后进入增删改查的页面在文本框输入相应的内容,然后点击相应的按钮可是实现hibernate的增删改查功能。增删改查后的数据库的内容可以实时显示在页面上。
这是程序的整体分布图
package com.imooc.dao;
import java.util.List;
import javax.swing.JOptionPane;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;
import com.imooc.entity.User;
import com.imooc.util.HibernateUtils;
public class LoginRegisterInfo {
public LoginRegisterInfo() {
}
public String saveInfo(User user) {
String mess = "error";
Session session = HibernateUtils.openSession();
try {
Transaction transaction = session.beginTransaction();
session.save(user);
transaction.commit();
mess = "success";
return mess;
} catch (Exception e) {
message("RegisterInfo error:" + e);
e.printStackTrace();
return null;
}finally {
session.close();
}
}
/*
* 删除
*/
public void delete(int uid) {
Session session = HibernateUtils.openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
Object user = session.get(User.class, uid); // 要先获取到这个对象
session.delete(user); // 删除的是实体对象
transaction.commit();
} catch (RuntimeException e) {
transaction.rollback();
throw e;
} finally {
session.close();
}
}
/*
* 根据id查询一个User数据
*/
public User getById(int uid) {
Session session = HibernateUtils.openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
User user = (User) session.get(User.class, uid);// 操作
transaction.commit();
return user;
} catch (RuntimeException e) {
transaction.rollback();
throw e;
} finally {
session.close();
}
}
/*
* 更新
*/
public void update(User user) {
Session session = HibernateUtils.openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
session.update(user);// 操作
transaction.commit();
} catch (RuntimeException e) {
transaction.rollback();
throw e;
} finally {
session.close();
}
}
/*
* 查询所有
*/
// private List list; // 一页的数据
// public List getList() {
// return list;
// }
// public void setList(List list) {
// this.list = list;
// }
public List findAll() {
Session session = HibernateUtils.openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
// 方式一:使用HQL语句
List list = session.createQuery("FROM User").list(); // 使用HQL查询
transaction.commit();
return list;
} catch (RuntimeException e) {
transaction.rollback();
throw e;
} finally {
session.close();
}
}
private void message(String mess) {
int type = JOptionPane.YES_NO_OPTION;
String title = "提示信息";
JOptionPane.showMessageDialog(null, mess, title, type);
}
}
package com.imooc.entity;
import java.io.Serializable;
public class User implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Integer uid;
private String username;
private String password;
private String nickname;
private String email;
private Integer state;
private String code;
public User() {
}
public User(String username, String password, String nickname, String email, Integer state, String code) {
this.username = username;
this.password = password;
this.nickname = nickname;
this.email = email;
this.state = state;
this.code = code;
}
public Integer getUid() {
return this.uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public String getNickname() {
return this.nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getState() {
return this.state;
}
public void setState(Integer state) {
this.state = state;
}
public String getCode() {
return this.code;
}
public void setCode(String code) {
this.code = code;
}
@Override
public String toString() {
return "[User: uid=" + uid + ", username=" + username + "]";
}
// @Override
// public String toString() {
// return "[User: uid=" + uid + ", username=" + username + ", password=" + password+"]";
// }
}
package com.imooc.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.imooc.dao.LoginRegisterInfo;
import com.imooc.entity.User;
/**
* Servlet implementation class JiemianServlet
*/
@WebServlet("/JiemianServlet")
public class JiemianServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String zhi = request.getParameter("anniu");
if(zhi.equals("save")) {
save(request, response);
}
else if(zhi.equals("delete")) {
delete(request, response);
}
else if(zhi.equals("update")) {
update(request, response);
}
else {
find(request, response);
}
}
private void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
response.setContentType("text/html;charset=utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
User user = new User();
LoginRegisterInfo info = new LoginRegisterInfo();
user.setUsername(username);
user.setPassword(password);
info.saveInfo(user);
List list = info.findAll();
for (User user2 : list) {
System.out.println(user2);
}
request.setAttribute("list", list);
request.getRequestDispatcher("JSP/jiemian.jsp").forward(request, response);
}
// response.sendRedirect("JSP/jiemian.jsp");
private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
response.setContentType("text/html;charset=utf-8");
String uid = request.getParameter("uid");
int i;
i=Integer.parseInt(uid);
LoginRegisterInfo info = new LoginRegisterInfo();
info.delete(i);
List list = info.findAll();
for (User user2 : list) {
System.out.println(user2);
}
request.setAttribute("list", list);
request.getRequestDispatcher("JSP/jiemian.jsp").forward(request, response);
// response.sendRedirect("http://www.baidu.com");
// response.sendRedirect("JSP/jiemian.jsp?id=1");
}
private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
response.setContentType("text/html;charset=utf-8");
String uid = request.getParameter("uid");
String username = request.getParameter("username");
String password = request.getParameter("password");
int i;
i=Integer.parseInt(uid);
LoginRegisterInfo info = new LoginRegisterInfo();
User user = info.getById(i);
user.setUsername(username);
user.setPassword(password);
info.update(user);
List list = info.findAll();
for (User user2 : list) {
System.out.println(user2);
}
request.setAttribute("list", list);
request.getRequestDispatcher("JSP/jiemian.jsp").forward(request, response);
}
private void find(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
response.setContentType("text/html;charset=utf-8");
LoginRegisterInfo info = new LoginRegisterInfo();
List list = info.findAll();
for (User user : list) {
System.out.println(user);
}
request.setAttribute("list", list);
request.getRequestDispatcher("JSP/jiemian.jsp").forward(request, response);
//跳转到success.jsp页面
}
}
package com.imooc.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.imooc.dao.LoginRegisterInfo;
import com.imooc.entity.User;
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
String password = request.getParameter("password");
User user = new User();
LoginRegisterInfo info = new LoginRegisterInfo();
if (username == null || username.length() == 0) {
out.print("");
} else {
List list = info.findAll();
for (int i = 0; i < list.size(); i++) {
user = list.get(i);
if (user.getUsername().equals(username)) {
if (user.getPassword().equals(password)) {
out.print("");
return;
} else {
out.print("");
}
}
}
out.print("");
}
}
}
package com.imooc.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.imooc.dao.LoginRegisterInfo;
import com.imooc.entity.User;
@WebServlet("/RegistServlet")
public class RegistServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
String password = request.getParameter("password");
String repassword = request.getParameter("repassword");
User user = new User();
LoginRegisterInfo info = new LoginRegisterInfo();
if (username == null || username.length() == 0) {
out.print("");
} else {
List list = info.findAll();
for (int i = 0; i < list.size(); i++) {
user = list.get(i);
if (user.getUsername().equals(username)) {
out.print("");
}
}
}
if (password == null || password.length() == 0) {
out.print("");
} else if (!password.equals(repassword)) {
out.print("");
}
user.setUsername(username);
user.setPassword(password);
info.saveInfo(user);
out.println("");
out.println("");
out.println("");
out.println("");
out.println("新增会员成功 ");
out.println("");
out.println("");
out.println("会员" + request.getParameter("username") + "注册成功
");
out.println("回首页登录");
out.println("");
out.println("");
}
}
package com.imooc.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtils {
// SessionFactory全局只需要有一个就可以了,因为它的创建和销毁需要消耗大量的资源,初始化信息会比较多,并且它是线程安全的,可以在多线程的环境下使用它
private static SessionFactory sessionFactory;
static {
// 初始化SessionFactory方式一:
/*
Configuration cfg = new Configuration(); // 代表配置文件的一个对象
cfg.configure(); // 读取默认的配置文件(hibernate.cfg.xml)
// cfg.configure("hibernate.cfg.xml"); // 读取指定位置的配置文件
sessionFactory = cfg.buildSessionFactory();
*/
// 初始化SessionFactory方式二:
sessionFactory = new Configuration() //
.configure() //
.buildSessionFactory(); // 方法链
}
/**
* 获取全局唯一的SessionFactory
*
* @return
*/
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
/**
* 从全局唯一的SessionFactory中打开一个Session
*
* @return
*/
public static Session openSession() {
return sessionFactory.openSession();
}
}
update
MySQL
org.hibernate.dialect.MySQLDialect
321
root
jdbc:mysql://localhost:3366/hibernate?serverTimezone=GMT%2B8
com.mysql.jdbc.Driver
true
true
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
新的界面
增删改查
查询结果:<%= request.getAttribute("list") %>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
登陆页面
登 录
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
注册页面
注 册
源码的下载连接https://download.csdn.net/download/qq_44257240/11441162