0% found this document useful (0 votes)
14 views24 pages

Spring Boot 4.0 Web App Lab Guide

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)
14 views24 pages

Spring Boot 4.0 Web App Lab Guide

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

Faculty of Engineering & Technology

Subject : Enterprise Prog. using JAVA- Lab


Subject code: 303105310 [Link]. CSE Year 3 Semester 5

Practical – 7
Aim - Create a web application to store the data in database with spring boot.
INPUT:
Springboot
[Link]

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


<project xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="[Link] [Link]
[Link]">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>4.0.0-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>springboot</groupId>
<artifactId>[Link]</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>[Link]</name>
<description>Demo project for Spring Boot</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<[Link]>21</[Link]>
</properties>
<dependencies>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>[Link]</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>9.0.0</version><!--$NO-MVN-MAN-VER$--> <!-- or latest stable -->
</dependency>

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>c3p0</artifactId>
<version>0.11.2</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>[Link]</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>[Link]
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>[Link]
<releases>
<enabled>false</enabled>
</releases>
</pluginRepository>
</pluginRepositories>

</project>

[Link]

package [Link];

import [Link];
import [Link];

import [Link];
import [Link];

import [Link];
import [Link];
import [Link];
@Repository
public class StudentDao {

@PersistenceContext
private EntityManager entityManager; // injected by Spring

// Retrieve entire data


@Transactional(readOnly = true)
public List<Student> find() {
List<Student> data = [Link]("FROM Student", [Link]).getResultList();
[Link]("data fetched successfully");
return data;
}

// Retrieve data by id
@Transactional(readOnly = true)
public Student find(Integer id) {
Student s = [Link]([Link], id);
[Link]("data fetched successfully");
return s;
}

// Insert new student


@Transactional
public void insert(String name, long enroll_no, Date date, String dept) {
Student record = new Student(name, enroll_no, date, dept);
[Link](record);
[Link]("data inserted successfully");
}

// Update existing student


@Transactional
public void update(Student s) {
[Link](s);
[Link]("data updated successfully");
}
// Remove student
@Transactional
public void remove(Student s) {
[Link]([Link](s) ? s : [Link](s));
[Link]("data removed successfully");
}
}

[Link]

package [Link];

import [Link];

import [Link];
import [Link];
import [Link];
import [Link];

@Entity
public class Student {

@Id
@GeneratedValue(strategy = [Link])
Integer id;
String name;
long enrollment_no;
Date dob;
String department;

public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String name, long enrollment_no, Date dob, String department) {
super();
[Link] = name;
this.enrollment_no = enrollment_no;
[Link] = dob;
[Link] = department;
}

public Integer getId() {


return id;
}

public void setId(Integer id) {


[Link] = id;
}

public String getName() {


return name;
}

public void setName(String name) {


[Link] = name;
}

public long getEnrollment_no() {


return enrollment_no;
}

public void setEnrollment_no(long enrollment_no) {


this.enrollment_no = enrollment_no;
}

public Date getDob() {


return dob;
}
public void setDob(Date dob) {
[Link] = dob;
}

public String getDepartment() {


return department;
}

public void setDepartment(String department) {


[Link] = department;
}
}

[Link]

package [Link];

import [Link];

import [Link];
import [Link];
import [Link];
import [Link];

@Entity
public class Student {

@Id
@GeneratedValue(strategy = [Link])
Integer id;
String name;
long enrollment_no;
Date dob;
String department;
public Student() {
super();
// TODO Auto-generated constructor stub
}

public Student(String name, long enrollment_no, Date dob, String department) {


super();
[Link] = name;
this.enrollment_no = enrollment_no;
[Link] = dob;
[Link] = department;
}

public Integer getId() {


return id;
}

public void setId(Integer id) {


[Link] = id;
}

public String getName() {


return name;
}

public void setName(String name) {


[Link] = name;
}

public long getEnrollment_no() {


return enrollment_no;
}

public void setEnrollment_no(long enrollment_no) {


this.enrollment_no = enrollment_no;
}
public Date getDob() {
return dob;
}

public void setDob(Date dob) {


[Link] = dob;
}

public String getDepartment() {


return department;
}

public void setDepartment(String department) {


[Link] = department;
}
}

[Link]

[Link]=[Link]

# ========================
# Data source
# ========================
[Link]=jdbc:mysql://localhost:3306/parul1
[Link]=root
[Link]=root
[Link]-class-name=[Link]

[Link]-auto=update
[Link]-sql=true

# CONNECTION POOL (C3P0)


# ===============================
[Link]=[Link]

Name: Het Chauhan


Enrollment no: 2403051057013 Page no: 44
Faculty of Engineering & Technology
Subject : Enterprise Prog. using JAVA- Lab
Subject code: 303105310 [Link]. CSE Year 3 Semester 5

</licenses>

<developers>

<developer />

</developers>

<scm>

<connection />

<developerConnection />

<tag />

<url />

</scm>

<properties>

<[Link]>21</[Link]>

</properties>

<dependencies>

<dependency>

<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-data-jpa</artifactId>

</dependency>

<dependency>

<groupId>[Link]</groupId>

<artifactId>mysql-connector-j</artifactId>

<scope>runtime</scope>

</dependency>

<dependency>

<groupId>[Link]</groupId>

<artifactId>lombok</artifactId>

<optional>true</optional>

</dependency>

Name: Het Chauhan


Enrollment no: 2403051057013 Page no: 45
Faculty of Engineering & Technology
Subject : Enterprise Prog. using JAVA- Lab
Subject code: 303105310 [Link]. CSE Year 3 Semester 5

<!--

[Link] -->

<dependency>

<groupId>[Link]</groupId>

<artifactId>c3p0</artifactId>

<version>0.11.2</version>

</dependency>

<dependency>

<groupId>[Link]</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>

</dependencies>

<build>

<plugins>

<plugin>

<groupId>[Link]</groupId>

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

<configuration>

<annotationProcessorPaths>

<path>

Name: Het Chauhan


Enrollment no: 2403051057013 Page no: 46
Faculty of Engineering & Technology
Subject : Enterprise Prog. using JAVA- Lab
Subject code: 303105310 [Link]. CSE Year 3 Semester 5

<groupId>[Link]</groupId>

<artifactId>lombok</artifactId>

</path>

</annotationProcessorPaths>

</configuration>

</plugin>

<plugin>

<groupId>[Link]</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

<configuration>

<excludes>

<exclude>

<groupId>[Link]</groupId>

<artifactId>lombok</artifactId>

</exclude>

</excludes>

</configuration>

</plugin>

</plugins>

</build>

<repositories>

<repository>

<id>spring-snapshots</id>

<name>Spring Snapshots</name>

<url>[Link]

<releases>

Name: Het Chauhan


Enrollment no: 2403051057013 Page no: 47
Faculty of Engineering & Technology
Subject : Enterprise Prog. using JAVA- Lab
Subject code: 303105310 [Link]. CSE Year 3 Semester 5

<enabled>false</enabled>

</releases>

</repository>

</repositories>

<pluginRepositories>

<pluginRepository>

<id>spring-snapshots</id>

<name>Spring Snapshots</name>

<url>[Link]

<releases>

<enabled>false</enabled>

</releases>

</pluginRepository>

</pluginRepositories>

</project>

[Link]

package [Link];

import [Link];

import [Link];

import [Link];

import

[Link];

import [Link];

import [Link];

import [Link];

Name: Het Chauhan


Enrollment no: 2403051057013 Page no: 48
Faculty of Engineering & Technology
Subject : Enterprise Prog. using JAVA- Lab
Subject code: 303105310 [Link]. CSE Year 3 Semester 5

@Repository

public class StudentDao {

@PersistenceContext

private EntityManager entityManager; // injected by

Spring

// Retrieve entire data

@Transactional(readOnly = true)

public List<Student> find() {

List<Student> data =

[Link]("FROM Student",

[Link]).getResultList();

[Link]("data fetched successfully");

return data;

// Retrieve data by id

@Transactional(readOnly = true)

public Student find(Integer id) {

Student s = [Link]([Link], id);

[Link]("data fetched successfully");

return s;

// Insert new student

@Transactional

public void insert(String name, long enroll_no, Date

date, String dept) {

Student record = new Student(name, enroll_no, date,

dept);

[Link](record);

Name: Het Chauhan


Enrollment no: 2403051057013 Page no: 49
Faculty of Engineering & Technology
Subject : Enterprise Prog. using JAVA- Lab
Subject code: 303105310 [Link]. CSE Year 3 Semester 5

[Link]("data inserted successfully");

// Update existing student

@Transactional

public void update(Student s) {

[Link](s);

[Link]("data updated successfully");

// Remove student

@Transactional

public void remove(Student s) {

[Link]([Link](s) ? s

: [Link](s));

[Link]("data removed successfully");

[Link]

package [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

Name: Het Chauhan


Enrollment no: 2403051057013 Page no: 50
Faculty of Engineering & Technology
Subject : Enterprise Prog. using JAVA- Lab
Subject code: 303105310 [Link]. CSE Year 3 Semester 5

@Entity

public class Student {

@Id

@GeneratedValue(strategy =

[Link])

Integer id;

String name;

long enrollment_no;

Date dob;

String department;

public Student() {

super();

// TODO Auto-generated constructor stub

public Student(String name, long enrollment_no, Date

dob, String department) {

super();

[Link] = name;

this.enrollment_no = enrollment_no;

[Link] = dob;

[Link] = department;

public Integer getId() {

return id;

Name: Het Chauhan


Enrollment no: 2403051057013 Page no: 51
Faculty of Engineering & Technology
Subject : Enterprise Prog. using JAVA- Lab
Subject code: 303105310 [Link]. CSE Year 3 Semester 5

public void setId(Integer id) {

[Link] = id;

public String getName() {

return name;

public void setName(String name) {

[Link] = name;

public long getEnrollment_no() {

return enrollment_no;

public void setEnrollment_no(long enrollment_no) {

this.enrollment_no = enrollment_no;

public Date getDob() {

return dob;

public void setDob(Date dob) {

[Link] = dob;

public String getDepartment() {

return department;

Name: Het Chauhan


Enrollment no: 2403051057013 Page no: 52
Faculty of Engineering & Technology
Subject : Enterprise Prog. using JAVA- Lab
Subject code: 303105310 [Link]. CSE Year 3 Semester 5

public void setDepartment(String department) {

[Link] = department;

[Link]

package [Link];

import [Link];

import [Link];

import [Link];

import

[Link]

ation;

import [Link];

import [Link];

import [Link];

@SpringBootApplication

public class Application {

public static void main(String[] args) {

ApplicationContext container =

[Link]([Link], args);

StudentDao dao =

[Link]([Link]);

Name: Het Chauhan


Enrollment no: 2403051057013 Page no: 53
Faculty of Engineering & Technology
Subject : Enterprise Prog. using JAVA- Lab
Subject code: 303105310 [Link]. CSE Year 3 Semester 5

// insert single record

[Link]("Ajay", 230510500, new Date(),

"CSE");

// insert 10 records

for (int i = 0; i < 10; i++) {

[Link]("name " + i, (long) (230510500 +

i), new Date(), "CSE");

// Retrieve and print record

List<Student> data = [Link]();

[Link]();

for (int i = 0; i < [Link](); i++) {

[Link]([Link](i).getId() + " | ");

[Link]([Link](i).getName() + " |

");

[Link]([Link](i).getEnrollment_no() + " | ");

[Link]([Link](i).getDob() + " | ");

[Link]([Link](i).getDepartment()

+ "\n");

[Link]();

// update 1st record

Name: Het Chauhan


Enrollment no: 2403051057013 Page no: 54
Faculty of Engineering & Technology
Subject : Enterprise Prog. using JAVA- Lab
Subject code: 303105310 [Link]. CSE Year 3 Semester 5

if (![Link]()) {

Student s = [Link](); // Java 21

method

[Link]("AIML");

[Link](s);

// print the updated row}

s = [Link]([Link]());

[Link]("\nupdated details : \n" +

[Link]() + " | ");

[Link]([Link]() + " | ");

[Link](s.getEnrollment_no() + " |

");

[Link]([Link]() + " | ");

[Link]([Link]() + "\n");

} else {

[Link]("No students found in

DB.");

// delete last row

if (![Link]()) {

Student s = [Link]();

[Link]("\nDeleted Record details

Name: Het Chauhan


Enrollment no: 2403051057013 Page no: 55
Faculty of Engineering & Technology
Subject : Enterprise Prog. using JAVA- Lab
Subject code: 303105310 [Link]. CSE Year 3 Semester 5

Name: Het Chauhan


Enrollment no: 2403051057013 Page no: 56
Faculty of Engineering & Technology
Subject : Enterprise Prog. using JAVA- Lab
Subject code: 303105310 [Link]. CSE Year 3 Semester 5

Name: Het Chauhan


Enrollment no: 2403051057013 Page no: 57
Faculty of Engineering & Technology
Subject : Enterprise Prog. using JAVA- Lab
Subject code: 303105310 [Link]. CSE Year 3 Semester 5
OUTPUT:

Name: Het Chauhan


Enrollment no: 2403051057013 Page no: 58

You might also like