0% found this document useful (0 votes)
16 views

Ex No 12

Uploaded by

dominichendry28
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Ex No 12

Uploaded by

dominichendry28
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Ex no 12 : Build a simple application with Hibernate

Aim :To Build a simple application with Hibernate

Building a simple registration application using Hibernate

Program with procedure :

Step 1: Create a New Maven Project

1. Open Eclipse and go to File > New > Maven Project.

2. Click Next and choose Create a simple project

3. Fill in the following details:

Group Id: com.example

Artifact Id: registration-app

Version: 1.0-SNAPSHOT

4. Click Finish.
Step 2: Update pom.xml

Open the pom.xml file and add dependencies for Hibernate and MySQL:

Code:

<project xmlns="http://maven.apache.org/POM/4.0.0"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-
4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>

<artifactId>registration-app</artifactId>

<version>1.0-SNAPSHOT</version>

<properties>

<hibernate.version>5.6.14.Final</hibernate.version>

<java.version>1.8</java.version>

</properties>

<dependencies>

<dependency>

<groupId>org.hibernate</groupId>

<artifactId>hibernate-core</artifactId>

<version>${hibernate.version}</version>
</dependency>

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>8.0.28</version>

</dependency>

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>javax.servlet-api</artifactId>

<version>4.0.1</version>

<scope>provided</scope>

</dependency>

<dependency>

<groupId>javax.servlet.jsp</groupId>

<artifactId>javax.servlet.jsp-api</artifactId>

<version>2.3.3</version>

<scope>provided</scope>

</dependency>

<dependency>

<groupId>javax.servlet.jsp.jstl</groupId>

<artifactId>javax.servlet.jsp.jstl-api</artifactId>

<version>1.2.1</version>

<scope>provided</scope>

</dependency>

<dependency>
<groupId>org.slf4j</groupId>

<artifactId>slf4j-api</artifactId>

<version>1.7.32</version>

</dependency>

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-simple</artifactId>

<version>1.7.32</version>

</dependency>

</dependencies>

<build>

<plugins>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<version>3.8.1</version>

<configuration>

<source>${java.version}</source>

<target>${java.version}</target>

</configuration>

</plugin>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-war-plugin</artifactId>
<version>3.3.1</version>

</plugin>

</plugins>

</build>

</project>

Step 3: Create Hibernate Configuration File

1. Create a new folder called src/main/resources.

2. Create a file named hibernate.cfg.xml in the src/main/resources folder:

Program

<?xml version="1.0" encoding="utf-8"?>

<!DOCTYPE hibernate-configuration PUBLIC

"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>

<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>

<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/registration_db</
property>

<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">your_password</property>

<property name="hibernate.hbm2ddl.auto">update</property>

<property name="show_sql">true</property>

</session-factory>

</hibernate-configuration>

Step 4: Create User Entity Class

1. Create a new package named com.example.model in src/main/java.

2. Create a class named User.java in the com.example.model package:

Program

package com.example.model;

import javax.persistence.*;

@Entity

@Table(name = "users")

public class User {

@Id

@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(name = "username", nullable = false, unique = true)

private String username;

@Column(name = "password", nullable = false)

private String password;

@Column(name = "email", nullable = false, unique = true)

private String email;

// Getters and Setters

public Long getId() {

return id;

public void setId(Long id) {

this.id = id;

public String getUsername() {

return username;

}
public void setUsername(String username) {

this.username = username;

public String getPassword() {

return password;

public void setPassword(String password) {

this.password = password;

public String getEmail() {

return email;

public void setEmail(String email) {

this.email = email;

Step 5: Create User Registration Logic

1. Create a new package named com.example.service in src/main/java.


2. Create a class named UserService.java in the com.example.service package:

Program

package com.example.service;

import com.example.model.User;

import org.hibernate.Session;

import org.hibernate.Transaction;

import org.hibernate.cfg.Configuration;

public class UserService {

public void registerUser(User user) {

Transaction transaction = null;

try (Session session = new Configuration().configure().buildSessionFactory().openSession()) {

transaction = session.beginTransaction();

session.save(user);

transaction.commit();

} catch (Exception e) {

if (transaction != null) {

transaction.rollback();

}
e.printStackTrace();

Step 6: Create a Servlet for Registration

1. Create a new package named com.example.servlet in src/main/java.

2. Create a class named RegistrationServlet.java in the com.example.servlet package:

Program

package com.example.servlet;

import com.example.model.User;

import com.example.service.UserService;

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 java.io.IOException;
@WebServlet("/register")

public class RegistrationServlet extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws


ServletException, IOException {

String username = request.getParameter("username");

String password = request.getParameter("password");

String email = request.getParameter("email");

User user = new User();

user.setUsername(username);

user.setPassword(password);

user.setEmail(email);

UserService userService = new UserService();

userService.registerUser(user);

response.sendRedirect("success.jsp");

Step 7: Create the Registration Form

1. Create a Web Content Directory:


Right-click on the project, select New > Folder. Name it src/main/webapp.

2. Create a JSP file named registration.jsp in the src/main/webapp folder:

Program

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>User Registration</title>

</head>

<body>

<h2>User Registration</h2>

<form action="register" method="post">

<label for="username">Username:</label>

<input type="text" name="username" required><br><br>

<label for="password">Password:</label>

<input type="password" name="password" required><br><br>

<label for="email">Email:</label>

<input type="email" name="email" required><br><br>

<input type="submit" value="Register">

</form>
</Body>

</Html>

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy