Professional Documents
Culture Documents
Aman Anand Sessional-3
Aman Anand Sessional-3
Roll- 2101330100033
-------------------------------------------------------------------------------------------------
11) Create a project showing the implementation of Spring MVC using model
and view class for finding the area of rectangle.
Rectangle.java(Model class):
package com.springmvc.model;
RectangleController.java(Controller class)
import ch.qos.logback.core.model.Model;
@Controller
@RequestMapping("/rectangle")
public class RectangleController {
// Show the form to input rectangle dimensions
@GetMapping("/form")
public String showForm() {
return "rectangleForm";
}
// Handle the form submission and calculate the area
@PostMapping("/calculate")
public String calculateArea(@RequestParam int length, @RequestParam int width, Model
model) {
Rectangle rectangle = new Rectangle();
rectangle.setLength(length);
rectangle.setWidth(width);
model.addAttribute("rectangle", rectangle);
model.addAttribute("area", area);
return "result";
}
}
<label for="width">Width:</label>
<input type="text" id="width" name="width" required><br>
</body>
</html>
<h2>Result</h2>
</body>
</html>
OUTPUT:
12) Create a rest service based web application using Spring boot and fetch data from
internal or external databases with JPA and send API request to POSTMAN client or
browser
Application.java
package com.springboot.jpa;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import com.springboot.jpa.dao.UserRepository;
import com.springboot.jpa.dao.User;
@SpringBootApplication
public class SpringbootjpaexampleApplication {
while(iterator.hasNext()) {
User user= iterator.next();
System.out.println("Name: "+user.getName() + " City:
"+user.getCity()+" Status: "+user.getStatus());
}
User.java
package com.springboot.jpa.dao;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
private String city;
private String status;
public User(int id, String name, String city, String status) {
super();
this.id = id;
this.name = name;
this.city = city;
this.status = status;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
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 getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
UserRepository.java
package com.springboot.jpa.dao;
import org.springframework.data.repository.CrudRepository;
Application.properties
spring.datasource.name=springbootjdbc
spring.datasource.url=jdbc:mysql://localhost:3306/springbootjdbc
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
server.port=8008
Output:
13) WAP to create simple web application in Spring Boot to build a
registration form with the help of JSP and show the data on view page.
Pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
User.java
package com.spring.ques.entity;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
UserRepository.java
package com.spring.ques.services;
import org.apache.catalina.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
UserController.java
package com.spring.ques.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.spring.ques.entity.User;
import com.spring.ques.services.UserService;
import java.util.List;
@Controller
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/register")
public String register() {
return "register";
}
@PostMapping("/register")
public String register(@RequestParam("name") String name,
@RequestParam("email") String email,
Model model) {
User user = new User();
user.setName(name);
user.setEmail(email);
userService.save(user);
List<User> users = userService.findAll();
model.addAttribute("users", users);
return "view";
}
}
UserService.java
package com.spring.ques.services;
import java.util.List;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
@Autowired
return userRepository.save(user);
}
public List<User> findAll() {
return userRepository.findAll();
return userRepository.findById(id).orElse(null);
userRepository.deleteById(id);
Register.jsp
Output:
14) WAP to perform insert delete and update query into a database with the help of
creating entity class and using its controller repository and services
SpringbootjpaApplication.java:
package com.springboot.jpa;
import java.util.List;
import org.apache.el.stream.Optional;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import com.springboot.jpa.dao.UserRepository;
import com.springboot.jpa.dao.User;
@SpringBootApplication
public class SpringbootjpaApplication {
public static void main(String[] args) {
ApplicationContext context =
SpringApplication.run(SpringbootjpaApplication.class, args);
UserRepository userRepository =
context.getBean(UserRepository.class);
// insert
User user1=new User();
user1.setName("qwerty");
user1.setCity("Up");
user1.setStatus("kcgsdg");
User resultUser=userRepository.save(user1);
System.out.println(resultUser);
//Update
java.util.Optional<User> optional= userRepository.findById(202);
User user =optional.get();
user.setName("whjdwedw");
User result22 =userRepository.save(user);
//delete
userRepository.deleteById(202);
System.out.print("Deleted");
Iterable<User> result = userRepository.findAll();
result.forEach(user ->{
System.out.println(user);
userRepository.deleteAll(result);
});
}
}
User.java:
package com.springboot.jpa.dao;
mport org.springframework.data.repository.CrudRepository;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
private String city;
private String status;
public User(int id, String name, String city, String statUserReposius) {
super();
this.id = id;
this.name = name;
this.city = city;
this.status = status;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
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 getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
UserRepository:
package com.springboot.jpa.dao;
import org.springframework.data.repository.CrudRepository;
}
Application.properties:
spring.datasource.name=test
spring.datasource.url=jdbc:mysql://localhost:3306/springbootjdbc
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
OUTPUT:
Insertion:
Update:
Deletion: