You are on page 1of 7

//Assign.

java

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

package student;

import java.util.Scanner;

import javax.persistence.Query;

import javax.persistence.Persistence;

import javax.persistence.EntityManager;

import javax.persistence.EntityManagerFactory;

/**

* @author bvcoew

*/

public class Assign {

public static void main(String[] args) {

Student s;

EntityManagerFactory emf=Persistence.createEntityManagerFactory("$objectdb/db/st3.odb");

EntityManager em = emf.createEntityManager();

int i,n,id,marks,div,age;

String name;

em.getTransaction().begin();

Scanner sc=new Scanner(System.in);

System.out.println("Enter no of students:\n");

n=sc.nextInt();

for(i=0;i<=n;i++)

System.out.println("Student "+(i+1)+ "\nEnter student id:");

id=sc.nextInt();

System.out.println(" ");
System.out.println("Enter student name:");

name=sc.next();

System.out.println(" ");

System.out.println("Enter student marks:");

marks=sc.nextInt();

System.out.println(" ");

System.out.println("Enter student division:");

div=sc.nextInt();

System.out.println(" ");

System.out.println("Enter student age:");

age=sc.nextInt();

System.out.println(" ");

s=new Student(id,name,marks,div,age);

em.persist(s);

sc.close();

em.getTransaction().commit();

Query q1 = em.createQuery("select count(s) from Student s");

Query q2 = em.createQuery("select name from Student");

Query q3 = em.createQuery("select name from Student where marks>40");

Query q4 = em.createQuery("select marks from Student where div=1");

Query q5 = em.createQuery("select avg(age) from Student");

Query q6 = em.createQuery("select min(marks) from Student ");

Query q7 = em.createQuery("select name from Student where marks>40 and div=1");

Query q8 = em.createQuery("select max(marks) from Student");

Query q9 = em.createQuery("select avg(age) from Student where div=2");

System.out.println("Total number of students:\n"+q1.getSingleResult());

System.out.println("Name of all students:\n"+q2.getResultList());

System.out.println("Name of students whose marks>40:\n"+q3.getResultList());

System.out.println("Marks of students where div=1:\n"+q4.getResultList());

System.out.println("Average age of all students:\n"+q5.getSingleResult());

System.out.println(" Minimuum marks:\n"+q6.getSingleResult());

System.out.println("Name of students whose marks>40 and div=1:\n"+q7.getResultList());


System.out.println("Maximuum marks of student:\n"+q8.getSingleResult());

System.out.println("Average age of all students from div=2:\n"+q9.getSingleResult());

// TODO code application logic here

//Student.java

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

package student;

import javax.persistence.Entity;

import javax.persistence.Id;

/**

* @author bvcoew

*/

@Entity

public class Student {

@Id

private int sid;

private String name;

private int marks;

private int div;

private int age;

public Student()

{}

Student(int id,String nm,int marks,int div,int age)

{
this.sid=id;

this.name=nm;

this.marks=marks;

this.div=div;

this.age=age;

public int getSid()

return sid;

public String getName()

return name;

public int getMarks()

return marks;

public int getDiv()

return div;

public int getAge()

return age;

@Override

public String toString()

return String.format("(sid:%d,name:%s,marks:%d,div:%d,age:%d\n)",this.sid,this.name,this.marks,this.div,this.age);

/**

* @param args the command line arguments

*/

}
OUTPUT
run:
Enter no of students:

1
Student 1
Enter student id:
1

Enter student name:


anu

Enter student marks:


43

Enter student division:


1

Enter student age:


21

Student 2
Enter student id:
2

Enter student name:


pri
Enter student marks:
75

Enter student division:


2

Enter student age:


23

Total number of students:


2
Name of all students:
[anu, pri]
Name of students whose marks>40:
[anu, pri]
Marks of students where div=1:
[43]
Average age of all students:
22.0
Minimuum marks:
43
Name of students whose marks>40 and div=1:
[anu]
Maximuum marks of student:
75
Average age of all students from div=2:
23.0
BUILD SUCCESSFUL (total time: 31 seconds)