0% found this document useful (0 votes)
29 views44 pages

Ajava From Pract 8 To Last

Uploaded by

ketanjnpdubey
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)
29 views44 pages

Ajava From Pract 8 To Last

Uploaded by

ketanjnpdubey
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/ 44

Practical No.

8
Program 8.1
Aim: Write a JSP program that demonstrate the use of JSP declaration,
scriplet, directives, expression, header and footer.
Code:
Newfile.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>

<%@ include file="header.html" %>


<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>JSP Demo Page</title>
</head>
<body>
<h1> Enter the message</h1>
<form action="responce.jsp" method="post">
Enter message : <input type="text" value="" name="text">
<input type="submit" value="submit" />
</form>
</body>
</html>
<%@ include file="footer.html" %>
Header.jsp:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>This is my first JSP page</title>
</head>
<body>
<h1>Welcome to first JSP Application</h1>
</body>
</html>
Footer.jsp:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>footer</title>

Name: Sakshi Kudu Roll No : 16


</head>
<body>
<h4>Hi i am sakshi</h4>
</body>
</html>

Response.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>

<%@ include file="header.html" %>


<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Response</title>
</head>

<body>
<% String s1=request.getParameter("text"); %>
<%=s1 %>

</body>
</html>
<%@include file="footer.html" %>

Output:

Name: Sakshi Kudu Roll No : 16


Program 8.2
Aim: Write a JSP program to collect mark of student and declare the result with
aggregate percentage. (with our own Assumption)
Code:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Grade</title>
</head>
<body>
<h2>Grade Calculator-------Main content of the page</h2>
<form action="" mothod="post">
<table>
<tr>
<td>program:MCA</td>
<td></td>
</tr>
<tr>
<td>Java(out of 100):</td>
<td><input type="text" value="" name="text1"></td>
</tr>
<tr>
<td>ADBMS(out of 100):</td>
<td><input type="text" value="" name="text2"></td>
</tr>
<tr>
<td>SPM(out of 100):</td>
<td><input type="text" value="" name="text3"></td>
</tr>
<tr>
<td>MATHS(out of 100):</td>

Name: Sakshi Kudu Roll No : 16


<td><input type="text" value="" name="text4"></td>
</tr><tr>
<td></td>
<td><input type="submit" value="sumbit" name="sumbnit" /></td>
</tr>
<tr>
</table>
</form>
<%
try {String res = "";

int t1 = Integer.parseInt(request.getParameter("text1"));
int t2 = Integer.parseInt(request.getParameter("text2"));
int t3 = Integer.parseInt(request.getParameter("text3"));
int t4 = Integer.parseInt(request.getParameter("text4"));
float r, av;
r = t1 + t2 + t3 + t4;
av = r / 4;
if (av > 75 && av <= 100) {
res = "You have passed with O Grade.";
} else if (av > 60 && av <= 75) {
res = "You have passed with A Grade.";
} else if (av > 50 && av <= 60) {
res = "You have passed with B Grade.";
} else if (av > 40 && av <= 50) {
res = "You have passed with C Grade.";
} else {
res = "You have Failed.";
}%>
<%=av%><br>
<%=res%>
<%
} catch (Exception e) {}%>
</body></html>

Output:

Name: Sakshi Kudu Roll No : 16


Practical No. 9
Aim: WAP to Insert records in Student Master.

9.1 Insert record.

Code –
student.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>student master</h1>
<form action="response.jsp" method="post">
<table>
<tr><td>Student Name :</td>
<td><input type="text" name="text1" /></td></tr>
<tr><td>Roll No : </td>
<td><input type="text" name="text2" /></td></tr>
<tr><td>Name of the College:</td>
<td><input type="text" name="text3" /></td></tr>
<tr><td>Address :</td>
<td><input type="text" name="text4" /></td></tr>
</table>
<input type="submit" value="Submit" />
</form>
</body>
</html>

response.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html> <head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head> <body>
<%@page import="java.sql.*" %>
<% try {
Class.forName("org.postgresql.Driver");
System.out.println("Drivers Registered");

Name: Sakshi Kudu Roll No : 16


Connection con=DriverManager.getConnection(
"jdbc:postgresql://localhost:5433/postgres","postgres","abc");
String t1, t2, t3, t4;
t1=request.getParameter("text1");
t2=request.getParameter("text2");
t3=request.getParameter("text3");
t4=request.getParameter("text4");
PreparedStatement ps;
ps=con.prepareStatement("insert into student(sname,sroll,scoll,sadd) values(?, ?, ?, ?);");
ps.setString(1, t1);
ps.setString(2, t2);
ps.setString(3, t3);
ps.setString(4, t4);
ps.executeUpdate();
out.println("Record inserted successfully.");
Statement st=con.createStatement();
String sql="select * from student";
ResultSet rs=st.executeQuery(sql);
while(rs.next()) {
String n1=rs.getString(1);
String n2=rs.getString(2);
String n3=rs.getString(3);
String n4=rs.getString(4);
%> <br>
<% out.println(n1+" "+n2+" "+n3+" "+n4);}
} catch(Exception e){ out.println(e); } %>
</body>
</html>
Student table
CREATE TABLE student(
sname varchar(10),
srollno varchar(50),
scoll varchar (50),
sadd varchar(50)
);
select * from student;

Output:

Name: Sakshi Kudu Roll No : 16


9.2 WAP to Delete records in Student Master.

Code –
Delete.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html> <head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head> <body>
<h1>student master</h1>
<form action="response1.jsp" method="post">

Name: Sakshi Kudu Roll No : 16


<table> <tr><td>Student Name :</td>
<td><input type="text" name="text1" /></td></tr>
<tr><td>Roll No : </td>
<td><input type="text" name="text2" /></td></tr>
<tr><td>Name of the College:</td>
<td><input type="text" name="text3" /></td></tr>
<tr><td>Address :</td>
<td><input type="text" name="text4" /></td></tr>
</table>
<input type="submit" value="Submit" />
</form> </body> </html>
response1.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html> <head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head> <body>
<%@page import="java.sql.*" %>
<% try {
Class.forName("org.postgresql.Driver");
System.out.println("Drivers Registered");
Connection con=DriverManager.getConnection(
"jdbc:postgresql://localhost:5433/postgres","postgres","abc");
String t1, t2, t3, t4;
t1=request.getParameter("text1");
t2=request.getParameter("text2");
t3=request.getParameter("text3");
t4=request.getParameter("text4");
PreparedStatement ps;
//insert into student(sname,srollno,scoll,sadd) values(?, ?, ?, ?);
ps=con.prepareStatement("DELETE FROM student where sroll= ?;");
ps.setString(1, t2);
/*ps.setString(1, t1);
ps.setString(3, t3);
ps.setString(4, t4);*/
ps.executeUpdate();
//out.println("Record inserted successfully.");
Statement st=con.createStatement();
String sql="select * from student";
ResultSet rs=st.executeQuery(sql);
while(rs.next()) {
String n1=rs.getString(1);
String n2=rs.getString(2);
String n3=rs.getString(3);

Name: Sakshi Kudu Roll No : 16


String n4=rs.getString(4);
%> <br>
<% out.println(n1+" "+n2+" "+n3+" "+n4 ); }
} catch(Exception e){ out.println(e); } %>
</body> </html>
Output –

9.3 Aim: Write a program to create registration form [JSP to Database]

Code-

register table
CREATE TABLE register(
uid varchar(20),
pass varchar(10),
fname varchar(50),
lname varchar (50),
email varchar(50)
);
Select * from register;
register.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html> <head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head> <body>
<h1>Registration Form</h1>
<form action="response.jsp" method="post">
<table> <tr><td>UserID:</td>
<td><input type="text" name="text1" /></td></tr>
<tr><td>Password:</td>
<td><input type="password" name="text2" /></td></tr>

Name: Sakshi Kudu Roll No : 16


<tr><td>First Name:</td>
<td><input type="text" name="text3" /></td></tr>
<tr><td>Last Name:</td>
<td><input type="text" name="text4" /></td></tr>
<tr><td>Email-id:</td>
<td><input type="email" name="text5" /></td></tr>
</table>
<input type="submit" value="Submit" />
</form> </body> </html>
response.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html> <head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head> <body>
<%@page import="java.sql.*" %>
<% try {
Class.forName("org.postgresql.Driver");
System.out.println("Registered");
Connection con=DriverManager.getConnection(
"jdbc:postgresql://localhost:5433/postgres","postgres","abc");
String t1, t2, t3, t4, t5;
t1=request.getParameter("text1");
t2=request.getParameter("text2");
t3=request.getParameter("text3");
t4=request.getParameter("text4");
t5=request.getParameter("text5");
PreparedStatement ps;
ps=con.prepareStatement("insert into register(uid, pass, fname, lname, email) values(?, ?, ?, ?, ?);");
ps.setString(1, t1);
ps.setString(2, t2);
ps.setString(3, t3);
ps.setString(4, t4);
ps.setString(5, t5);
ps.executeUpdate();
out.println("Record inserted successfully.");
Statement st=con.createStatement();
String sql="select * from register";
ResultSet rs=st.executeQuery(sql);
while(rs.next()) {
String n1=rs.getString(1);
String n2=rs.getString(2);
String n3=rs.getString(3);
String n4=rs.getString(4);

Name: Sakshi Kudu Roll No : 16


String n5=rs.getString(5);
%> <br>
<% out.println(n1+" "+n2+" "+n3+" "+n4+" "+n5);
} catch(Exception e){ out.println(e); } %>
</body>
</html>

Output –

Name: Sakshi Kudu Roll No : 16


Practical 10
Aim10.1: Write a program to display name and age of employee using beans –
Application Context Program
Code:
Simple.java
package simplemsg;
public class Simple {
//Attributes
String name;
int age;
//getter and setter
/*** @return the name*/
public String getName() {
return name; }
/*** @param name the name to set*/
public void setName(String name) {
this.name = name; }
/*** @return the age*/
public int getAge() {
return age; }
/*** @param age the age to set*/
public void setAge(int age) {
this.age = age; }
//utility method
void displayInfo() {
System.out.println("Name : "+name+"\n Age: "+age); } }

applicationcontext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="simplemsg" class="simplemsg.Simple">
<property name="name" value="priya"></property>
<property name="age" value="10"></property> </bean>
</beans>

SimpleTest.java
package simplemsg;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SimpleTest {
private static ApplicationContext ctx;

Name: Sakshi Kudu Roll No : 16


public static void main(String[] args) {
// TODO Auto-generated method stub
ctx=new ClassPathXmlApplicationContext("applicationcontext.xml");
Simple s1=(Simple)ctx.getBean("simplemsg");
s1.displayInfo();
}
}

OUTPUT:

Aim 10.2: Write a program to display name and age of employee using beans
– Bean Factory Program
Code:
Student.java
package org.viva;
public class Student {
//Attributes
String name;
int rollno;
//getter and setter
/*** @return the name */
public String getName() {
return name; }
/*** @param name the name to set*/
public void setName(String name) {
this.name = name; }
/*** @return the rollno */
public int getRollno() {
return rollno; }
/*** @param rollno the rollno to set*/
public void setRollno(int rollno) {
this.rollno = rollno; }

Name: Sakshi Kudu Roll No : 16


void displayInfo() {
System.out.println("Name : "+name+"\nRoll No :"+rollno); } }

applicationcontext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="studbean" class="org.viva.Student">
<property name="name" value="Ananaya"></property>
<property name="rollno" value="1"></property>
</bean> </beans>
StudentTest.java
package org.viva;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class StudentTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
XmlBeanFactory appCon = new XmlBeanFactory (new
ClassPathResource("applicationcontext.xml"));
Student factory=(Student)appCon.getBean("studbean");
factory.displayInfo();
factory.setName("Viaan");
factory.setRollno(2);
factory.displayInfo();
}
OUTPUT:

Name: Sakshi Kudu Roll No : 16


Aim 10.3: Write a program to demonstrate dependency injection via
Constructor for primitive values. (Bank Application) (CONFIUGRE FILE->
CONSTRUCTOR ARG)

Code:
Account.java

package org.viva;
public class Account {
int acNo;
String acName;
double acbalance;
/*** @return the acNo*/
public int getAcNo() {
return acNo; }
/*** @param acNo the acNo to set*/
public void setAcNo(int acNo) {
this.acNo = acNo; }
/*** @return the acName*/
public String getAcName() {
return acName; }
/*** @param acName the acName to set*/
public void setAcName(String acName) {
this.acName = acName; }
/*** @return the acbalance*/
public double getAcbalance() {
return acbalance; }
/*** @param acbalance the acbalance to set*/
public void setAcbalance(double acbalance) {
this.acbalance = acbalance;
}
public Account(int acNo, String acName, double acbalance) {
super();
this.acNo = acNo;
this.acName = acName;
this.acbalance = acbalance; }
public Account() {
super(); } }

Appctx.xml

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


<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance

Name: Sakshi Kudu Roll No : 16


xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="Account" class="org.viva.Account">
<constructor-arg type="int" value="000001" ></constructor-arg>
<constructor-arg type="String" value="Priya"></constructor-arg>
<constructor-arg type="double" value="2300"></constructor-arg>
</bean> </beans>

AcoountTest.java

package org.viva;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.ApplicationContext;
class AcoountTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ctx=new ClassPathXmlApplicationContext("appctx.xml");
Account a1=(Account) ctx.getBean("Account");
System.out.println("Ac NO:"+a1.getAcNo());
System.out.println("Ac Name:"+a1.getAcName());
System.out.println("Ac Balance:"+a1.getAcbalance());
}}
OUTPUT:

Aim 10.4: Write a program to demonstrate dependency injection via Constructor


for dependent object/Reference Type. (Employee Application)

Code:
FullName.java
package org.viva;
public class FullName {
private String Fname;
private String Mname;
private String Lname;
public FullName(String fname, String mname, String lname) {
super();

Name: Sakshi Kudu Roll No : 16


Fname = fname;
Mname = mname;
Lname = lname; }
public String toString(){
return Fname+" "+Mname+" "+Lname; } }

Employee.java
package org.viva;
public class Employee {
int id;
FullName name;
public Employee(int id, FullName name) {
super();
this.id = id;
this.name = name; }
void show(){
System.out.println("Id:"+id);
System.out.println("Full Name:"+name); } }

Context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="a1" class="org.viva.FullName">
<constructor-arg value="JASMINE"></constructor-arg>
<constructor-arg value="ANU"></constructor-arg>
<constructor-arg value="DUBEY"></constructor-arg>
</bean>
<bean id="e" class="org.viva.Employee">
<constructor-arg value="11" type="int"></constructor-arg>
<constructor-arg><ref bean="a1"/></constructor-arg>
</bean> </beans>

EmployeeTest.java
package org.viva;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class EmployeeTest {
private static ApplicationContext appCon;
public static void main(String[] args) {
// TODO Auto-generated method stub
appCon = new ClassPathXmlApplicationContext("Context.xml");
Employee factory=(Employee)appCon.getBean("e");
factory.show(); } }

Name: Sakshi Kudu Roll No : 16


OUTPUT:

Aim 10.5: Write a program to demonstrate dependency injection via setter method
for dependent object/reference type (Shape Application).

Code:
Shape.java

package org.viva;
public interface Shape {
void show();
}
Circle.java
package org.viva;
public class Circle implements Shape {
@Override
public void show() {
// TODO Auto-generated method stub
System.out.println("I am Circle"); } }

ShapeManager.java

package org.viva;
public class ShapeManager {
Shape myShape;
public void show() {
this.myShape.show(); }
public Shape getMyShape() {
return myShape; }
public void setMyShape(Shape myShape) {
this.myShape = myShape; }
}

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

Name: Sakshi Kudu Roll No : 16


<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="Circle" class="org.viva.Circle"></bean>
<bean id="ShapeMan" class="org.viva.ShapeManager">
<property name="myShape" ref="Circle"></property>
</bean> </beans>

ShapeManagerTest.java

package org.viva;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ShapeManagerTest {
private static ApplicationContext appCon;
public static void main(String[] args) {
// TODO Auto-generated method stub
appCon=new
ClassPathXmlApplicationContext(“appctx.xml”);
ShapeManagerfactory= (ShapeManager)appCon.getBeans(“ShapeMan”);
Factory.shoe();
} }

OUTPUT:

Name: Sakshi Kudu Roll No : 16


Practical No 11
Aim11.1: : Write a program to demonstrate Spring AOP – before advice
Code:
Logging.java
package org.mca;

public class Logging {


public void beforeAdvice() {
System.out.println("Going to setup student profile.");
}
}

Student.java

package org.mca;
public class Student {
int age;
String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
StudentTest.java
package org.mca;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.*;

public class StudentTest {


private static ApplicationContext context;
public static void main(String[] args) {
// TODO Auto-generated method stub
context = new ClassPathXmlApplicationContext("appctx.xml");
Student student = (Student) context.getBean("student");
String abc=student.getName();

Name: Sakshi Kudu Roll No : 16


System.out.println("Name is:"+abc);
int ag=student.getAge();
System.out.println("Age is:"+ag);
}

}
In src/main/resources
appctx.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">

<aop:config>
<aop:aspect id = "log" ref = "logging">
<aop:pointcut id = "selectAll"
expression = "execution(* org.mca1.Student.getName(..))"/>

<aop:before pointcut-ref="selectAll" method="beforeAdvice"/>


</aop:aspect>
</aop:config>
<bean id = "student" class = "org.mca1.Student">
<property name = "name" value = "Zara" />
<property name = "age" value = "11"/>
</bean>

<!-- Definition for logging aspect -->


<bean id = "logging" class = "org.mca1.Logging"/>
</beans>

Pom.xml
<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 https://maven.apache.org/xsd/maven-
4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.mca1</groupId>
<artifactId>org.mca1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

Name: Sakshi Kudu Roll No : 16


<name>AopStudent</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->


<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.19</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->


<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.6</version>

Name: Sakshi Kudu Roll No : 16


</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.6</version>
</dependency>

</dependencies>
</project>

Output:

Aim11.2: Write a program to demonstrate Spring AOP – after advice.


Code:
Logging.java

package org.mca2;

public class Logging {


/**
* This is the method which I would like to execute
* after a selected method execution.
*/
public void afterAdvice(){
System.out.println("Student profile setup complete.");
} }

Student.java
package org.mca2;
public class Student {
int age;
String name;
public int getAge() {
return age;

Name: Sakshi Kudu Roll No : 16


}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

StudentTest.java

package org.mca2;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.*;
public class StudentTest {
private static ApplicationContext context;
public static void main(String[] args) {
// TODO Auto-generated method stub
context = new ClassPathXmlApplicationContext("appctx.xml");
Student student = (Student) context.getBean("student");
String abc=student.getName();
System.out.println("Name is:"+abc);
int ag=student.getAge();
System.out.println("Age is:"+ag);
}
}
In src/main/resources
appctx.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">

Name: Sakshi Kudu Roll No : 16


<aop:config>
<aop:aspect id = "log" ref = "logging">
<aop:pointcut id = "selectAll"
expression = "execution(* org.mca2.Student.getName(..))"/>
<aop:after pointcut-ref="selectAll" method="afterAdvice"/>
</aop:aspect>
</aop:config>
<bean id = "student" class = "org.mca2.Student">
<property name = "name" value = "Zara" />
<property name = "age" value = "11"/>
</bean>
<!-- Definition for logging aspect -->
<bean id = "logging" class = "org.mca2.Logging"/>
</beans>
Pom.xml

<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 https://maven.apache.org/xsd/maven-
4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.mca2</groupId>
<artifactId>org.mca2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>AopStudent</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>

Name: Sakshi Kudu Roll No : 16


<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.19</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.6</version>
</dependency>
</dependencies>
</project>
Output:

Name: Sakshi Kudu Roll No : 16


Aim11.3: Write a program to demonstrate Spring AOP – after returning advice.
Code:
Student.java:
package org.mca;
public class Student {
private Integer age;
private String name;
public void setAge(Integer age) {
this.age = age;
}
public Integer getAge() {
System.out.println("Age : " + age );
return age;

}
public void setName(String name) {
this.name = name;
}
public String getName() {
System.out.println("Name : " + name );
return name;
}
public void printThrowException(){
System.out.println("Exception raised");
throw new IllegalArgumentException();
}
}

Logging.java:
package org.mca;

public class Logging {


public void afterReturningAdvice(Object retVal){
System.out.println("Returning:" + retVal.toString() );
}
}

Beans.xml:
<?xml version = "1.0" encoding = "UTF-8"?>
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop = "http://www.springframework.org/schema/aop"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

Name: Sakshi Kudu Roll No : 16


<aop:config>
<aop:aspect id = "log" ref = "logging">
<aop:pointcut id = "selectAll"
expression = "execution(* org.mca.*.*(..))"/>
<aop:after-returning pointcut-ref = "selectAll"
method = "afterReturningAdvice" returning = "retVal"/>
</aop:aspect>
</aop:config>

<!-- Definition for student bean -->


<bean id = "student" class = " org.mca.Student">
<property name = "name" value = "Zara" />
<property name = "age" value = "11"/>
</bean>

<!-- Definition for logging aspect -->


<bean id = "logging" class = " org.mca.Logging"/>
</beans>

MainApp.java:
package org.mca;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {


public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");

Student student = (Student) context.getBean("student");


student.getName();
student.getAge();
}
}

Pom.xml

<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.tutorialspoint</groupId>
<artifactId>AfterReturningAdvise</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>AopStudent</name>
<url>http://maven.apache.org</url>

Name: Sakshi Kudu Roll No : 16


<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.19</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.2.8.RELEASE</version>

</dependency>

<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->


<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->

Name: Sakshi Kudu Roll No : 16


<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.6</version>
</dependency>
</dependencies>
</project>

Output:

Aim11.4: Write a program to demonstrate Spring AOP –around advice.


Code:

Student.java
package aroundAdvice;
public class Student {
private Integer age;
private String name;
public void setAge(Integer age) {
this.age = age;
}
public Integer getAge() {
System.out.println("Age : " + age );
return age;

Name: Sakshi Kudu Roll No : 16


}
public void setName(String name) {
this.name = name;
}
public String getName() {
System.out.println("Name : " + name );
return name;
}
public void printThrowException(){
System.out.println("Exception raised");
throw new IllegalArgumentException();
}
}

Logging.java
package aroundAdvice;
import org.aspectj.lang.ProceedingJoinPoint;
public class Logging {
/**
* This is the method which I would like to execute
* around a selected method execution.
*/
public String aroundAdvice(ProceedingJoinPoint jp) throws Throwable{
System.out.println("Around advice");
Object[] args = jp.getArgs();
if(args.length>0){
System.out.print("Arguments passed: " );
for (int i = 0; i < args.length; i++) {
System.out.print("arg "+(i+1)+": "+args[i]);
}
}
Object result = jp.proceed(args);
System.out.println("Returning " + result);
return result.toString();
}
}
Appctx.xml

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


<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop = "http://www.springframework.org/schema/aop"
xsi:schemaLocation = "http://www.springframework.org/schema/beans

Name: Sakshi Kudu Roll No : 16


http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

<aop:config>
<aop:aspect id = "log" ref = "logging">
<aop:pointcut id = "selectName"
expression = "execution(* aroundAdvice.Student.getName(..))"/>
<aop:around pointcut-ref = "selectName" method = "aroundAdvice"/>
</aop:aspect>
</aop:config>

<!-- Definition for student bean -->


<bean id = "student" class = "aroundAdvice.Student">
<property name = "name" value = "Zara" />
<property name = "age" value = "11"/>
</bean>

<!-- Definition for logging aspect -->


<bean id = "logging" class = "aroundAdvice.Logging"/>
</beans>
StudentTest.java
package aroundAdvice;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class StudentTest {

public static void main(String[] args) {


// TODO Auto-generated method stub
ApplicationContext context = new
ClassPathXmlApplicationContext("Appctx.xml");

Student student = (Student) context.getBean("student");


student.getName();
}}

Name: Sakshi Kudu Roll No : 16


Output:

Aim11.5: Write a program to demonstrate Spring AOP –After Throwing


advice.
Code:
Student.java:
package org.mca;

public class Student {


private Integer age;
private String name;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void printThrowException(){
System.out.println("Exception raised");
throw new IllegalArgumentException();
}}

Name: Sakshi Kudu Roll No : 16


Logging.java:
package org.mca;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
@Aspect

public class Logging {


public void afterThrowingAdvice(JoinPoint jp, Throwable error){
System.out.println("Method Signature: " + jp.getSignature());
System.out.println("Exception: "+error);
}
}

Beans.xml:
<?xml version = "1.0" encoding = "UTF-8"?>
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop = "http://www.springframework.org/schema/aop"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<aop:aspectj-autoproxy/>
<!-- Definition for student bean -->
<bean id = "student" class = " org.mca.Student">
<property name = "name" value = "Zara" />
<property name = "age" value = "11"/> </bean>
<!-- Definition for logging aspect -->
<bean id = "logging" class = " org.mca.Logging"/>
</beans>
MainApp.java:
package org.mca;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {


public static void main(String[] args) {
ApplicationContext context = new
ClassPathXmlApplicationContext("Beans.xml");
Student student = (Student) context.getBean("student");
student.printThrowException(); }}

Name: Sakshi Kudu Roll No : 16


Pom.xml
<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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tutorialspoint</groupId>
<artifactId>AfterThrowing</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>AopStudent</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.19</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>

Name: Sakshi Kudu Roll No : 16


<artifactId>spring-jdbc</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->


<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.6</version>
</dependency>
</dependencies>
</project>
Output:

Name: Sakshi Kudu Roll No : 16


Practical 12
Aim12.1 : Write a program to demonstrate Spring Jdbc Template class to
store data in database table.

Code:
On pgAdmin4
create table mymovies(mid int,title varchar,actor varchar);
select * from mymovies;
Movies.java
package org.viva;
public class Movies {
int mid;
String title;
String actor;
public Movies(int mid, String title, String actor) {
super();
this.mid = mid;
this.title = title;
this.actor = actor; }
/*** @return the mid */
public int getMid() {
return mid; }
/*** @param mid the mid to set */
public void setMid(int mid) {
this.mid = mid; }
/*** @return the title*/
public String getTitle() {
return title; }
/*** @param title the title to set*/
public void setTitle(String title) {
this.title = title; }
/*** @return the actor */
public String getActor() {
return actor; }
/*** @param actor the actor to set */
public void setActor(String actor) {
this.actor = actor; } }

MoviesDao.java
package org.viva;
import org.springframework.jdbc.core.*;
public class MovieDao {
JdbcTemplate jdbcTemplate;
public JdbcTemplate getJdbcTemplate() {

Name: Sakshi Kudu Roll No : 16


return jdbcTemplate; }
public void JdbcTemplate setJdbcTemplate(JdbcTemplate
jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public int insMovie(Movies m1)
{
String insSql="insert into mymovies values("+m1.getMid()
+",'"+m1.getTitle()+"','"+m1.getActor()+"')";
return jdbcTemplate.update(insSql);//returns rows affected
}}

Appctx.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="ds"
class="org.springframework.jdbc.datasource.DriverManagerDataSourc
e">
<property name="driverClassName" value="org.postgresql.Driver"
/>
<property name="url"
value="jdbc:postgresql://localhost:5432/postgres" />
<property name="username" value="postgres" />
<property name="password" value="abc" />
</bean>
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="ds"></property>
</bean>
<bean id="moviebean" class="org.viva.MovieDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
</beans>

MovieTest.java
package org.viva;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContex
t;
public class MovieTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
appCon = new ClassPathXmlApplicationContext("appctx.xml");

Name: Sakshi Kudu Roll No : 16


MovieDao m1=(MovieDao)appCon.getBean("moviebean");
Movies t1=new Movies(1,"A Beautiful Mind","Russel Crow");
System.out.println(m1.insMovie(t1));
}
}

Output:

Aim12.2: Write a program to demonstrate RowMapper interface to fetch the records from
the database.

Code:
On pgAdmin
create table employee(id int,name varchar, salary int );
select * from employee;
Employee.java
package org.viva;
public class Employee {
private int id;
private String name;
private int salary;
public Employee() {
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 int getSalary() {
return salary; }
public void setSalary(int salary) {
this.salary = salary;}
public Employee(int id, String name, int salary) {
super();

Name: Sakshi Kudu Roll No : 16


this.id = id;
this.name = name;
this.salary = salary; }
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", salary=" + salary
+ "]"; } }

EmployeeDao.java
package org.viva;
import java.sql.ResultSet;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
public class EmployeeDao {
private JdbcTemplate jdbcTemplate;
public EmployeeDao() {
super();
// TODO Auto-generated constructor stub
}
public EmployeeDao(JdbcTemplate jdbcTemplate) {
super();
this.jdbcTemplate =jdbcTemplate;
}
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate; }
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate; }
public List<Employee> getAllEmployees(){
return jdbcTemplate.query("select * from employee",new
ResultSetExtractor<List<Employee>>(){ @Override
public List<Employee> extractData(ResultSet rs) throws SQLException,
DataAccessException {
List<Employee> list=new ArrayList<Employee>();
while(rs.next()){
Employee e=new Employee();
e.setId(rs.getInt(1));
e.setName(rs.getString(2));
e.setSalary(rs.getInt(3));
list.add(e); }
return list; } }); } }
Appctx.xml

Name: Sakshi Kudu Roll No : 16


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="ds"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://localhost:5433/postgres"
/>
<property name="username" value="postgres" />
<property name="password" value="abc" />
</bean>
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="ds"></property>
</bean>
<bean id="edao" class="org.viva.EmployeeDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
</beans>
EmployeeTest.java
package org.viva;
import java.util.List;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public class EmployeeTest {
public static void main(String[] args) {
ApplicationContext ctx=new
ClassPathXmlApplicationContext("Appctx.xml");
EmployeeDao dao=(EmployeeDao)ctx.getBean("edao");
List<Employee> list=dao.getAllEmployees();
for(Employee e:list)
System.out.println(e); }}
Output:

Practical 13

Name: Sakshi Kudu Roll No : 16


Aim: Write a procedure to demonstrate the Spring Boot concept.
Steps: File-> new-> maven project-> default(internal -> Quick start-> 1.1) next->group_ID->
artifact_ID->go for project-> expand the project-> src-> main.JAVA->
org.mca.Spring_Boot_Demo->App.java(open )
Code:
package org.mca.Spring_Boot_Demo;
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}
Output:

Practical 14

Name: Sakshi Kudu Roll No : 16


Aim: Write a program to demonstrate a restful Web Services API
CODE:
Application.java

package com.spring_first_app.com.spring_first_app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

public static void main(String[] args) {


SpringApplication.run(Application.class, args);
}}
WelcomeController.java

package com.spring_first_app.com.spring_first_app;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class WelcomeController {
@GetMapping("/welcome")
public String welcome()
{
return "welcome to springboot dear";
}}
OUTPUT:

Name: Sakshi Kudu Roll No : 16


Name: Sakshi Kudu Roll no: 16

You might also like