gpt4 book ai didi

java - Hibernate Jsp Servlet 注册表单不起作用

转载 作者:行者123 更新时间:2023-12-02 09:08:55 25 4
gpt4 key购买 nike

当我运行应用程序时,我的数据没有放入数据库中,我不知道问题是什么,我会寻求一些帮助。这是我的 hibernate 实用程序:

    public class HibernateUtil {
private static SessionFactory sessionFactory;

public static SessionFactory getSessionFactory() {
if (sessionFactory == null) {
try {
Configuration configuration = new Configuration();

// Hibernate settings equivalent to hibernate.cfg.xml's properties
Properties settings = new Properties();
settings.put(Environment.DRIVER, "com.mysql.jdbc.Driver");
settings.put(Environment.URL, "jdbc:mysql://localhost:3306/testdb");
settings.put(Environment.USER, "root");
settings.put(Environment.PASS, "theworldismine");
settings.put(Environment.DIALECT, "org.hibernate.dialect.MySQLDialect");

settings.put(Environment.SHOW_SQL, "true");

settings.put(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread");

settings.put(Environment.HBM2DDL_AUTO, "create-drop");

configuration.setProperties(settings);
configuration.addAnnotatedClass(User.class);

ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
System.out.println("Hibernate Java Config serviceRegistry created");
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;

} catch (Exception e) {
e.printStackTrace();
}
}
return sessionFactory;
}

这是我的用户 Controller :

    public class UserController {

private userDao userdao;

public void init() {
userdao = new userDao();
}

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
register(request, response);
}

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.getRequestDispatcher("/registration.jsp").forward(request, response);
}

private void register(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");
String username = request.getParameter("username");
String password = request.getParameter("password");

User user = new User();
user.setFirstName(firstName);
user.setLastName(lastName);
user.setUsername(username);
user.setPassword(password);

userdao.saveUser(user);

RequestDispatcher dispatcher = request.getRequestDispatcher("register-success.jsp");
dispatcher.forward(request, response);
}

这是我的 userDao:

    public class userDao {
public void saveUser(User user){

Transaction tx= null;

try{
Session session= HibernateUtil.getSessionFactory().openSession();

session.save(user);
}catch(Exception e){

if(tx!=null){
tx.rollback();

}
e.printStackTrace();
}
}

这是我的 web.xml

    <servlet>
<servlet-name>validate1</servlet-name>
<servlet-class>validate1</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>validate1</servlet-name>
<url-pattern>/validate1</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file> register.jsp</welcome-file>
</welcome-file-list>

最佳答案

您忘记了交易:

try (Session session = HibernateUtil.getSessionFactory().openSession())
{
session.beginTransaction();

session.save(user);

session.getTransaction().commit();
}

关于java - Hibernate Jsp Servlet 注册表单不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59571134/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com