[go: up one dir, main page]

0% found this document useful (0 votes)
20 views15 pages

2

Uploaded by

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

2

Uploaded by

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

[1]To demonstrate the web development by using MVC design pattern.

index.jsp(View)

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

Name:<input type="text" name="name"><br>

Password:<input type="password" name="password"><br>

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

</form>

login-success.jsp(view)

<%@page import="com.pu.LoginBean"%>

<p>You are successfully logged in!</p>

<% LoginBean bean=(LoginBean)request.getAttribute("bean");

out.print("Welcome, "+bean.getName());

%>

login-error.jsp(View)

<p>Sorry! username or password error</p>

<%@ include file="index.jsp" %>


LoginBean.java(Model)

package com.pu;

import java.lang.*;

public class LoginBean {

private String name,password;

public String getName() {

return name;

public void setName(String name) {

this.name = name;

public String getPassword() {

return password;

public void setPassword(String password) {


this.password = password;

public boolean validate(){

if(password.equals("admin")){

return true;

else{

return false;

}}}

ControllerServlet.java(Controller)

response.setContentType("text/html");

PrintWriter out=response.getWriter();

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

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

LoginBean bean=new LoginBean();

bean.setName(name);
bean.setPassword(password);

request.setAttribute("bean",bean);

boolean status=bean.validate();

if(status){

RequestDispatcher rd=request.getRequestDispatcher("login-success.jsp");

rd.forward(request,response);

else{

RequestDispatcher rd=request.getRequestDispatcher("login-error.jsp");

rd.forward(request,response);

[2]To develop a Java console application that demonstrates the connection with MySQL database
and perform various operations on it.

import java.sql.*;

import java.io.*;

public class Temp {


public static void main(String[] args) {

// TODO Auto-generated method stub

try{

Class.forName("com.mysql.jdbc.Driver");

Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/employee?characterEncoding=lati
n1","root","root");

Statement stmt=con.createStatement();

int ans=1;

do {

System.out.println("1. Insert a record ");

System.out.println("2. Delete a record ");

System.out.println("3. Modify/Edit a record ");

System.out.println("4. Display list of records ");

Scanner sc = new Scanner(System.in);

System.out.println("Enter your choice:");


int ch = sc.nextInt();

String ename;

int eno,age;

String query="";

switch(ch) {

case 1:

System.out.println("Enter employee number:");

eno = sc.nextInt();

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

ename = sc.next();

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

age = sc.nextInt();

query = "INSERT INTO emp " + "VALUES (" + eno+ ",'" + ename+"',"+ age+")";

stmt.executeUpdate(query);
break;

case 2:

System.out.println("Enter employee number:");

eno = sc.nextInt();

query = "delete from emp where eno='"+eno+"'";

stmt.executeUpdate(query);

System.out.println("Record is deleted from the table successfully..................");

break;

case 3:

PreparedStatement ps = null;

query = "update emp set name=? where eno=? ";

ps = con.prepareStatement(query);

System.out.println("Enter employee number:");

eno = sc.nextInt();

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

ename = sc.next();
ps.setString(1, ename);

ps.setInt(2, eno);

ps.executeUpdate();

System.out.println("Record is updated successfully......");

break;

case 4:

ResultSet rs=stmt.executeQuery("select * from emp");

while(rs.next())

System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getInt(3));

System.out.println("Enter another(1/0)");

ans = sc.nextInt();

}while(ans==1);

con.close();
}catch(Exception e){ System.out.println(e);}

[3] To implement the collection framework by develop a java console application.

import java.io.*;

import java.util.*;

class Student {

int rollno;

String name;

float fees;

String branch;

int year;

int sem;

int age;

static String clg;


public Student(int rollno,String name,float fees,String branch,int year,int sem,int age) {

this.rollno = rollno;

this.name = name;

this.fees = fees;

this.branch = branch;

this.year = year;

this.sem = sem;

this.age = age;

clg="PU";

public String toString() {

return rollno + " "+ name + " " + fees + " " + branch + " " + year + sem + " " + age + " " + clg +
"\n";

}
}

class AgeComparator implements Comparator {

public int compare(Object o1, Object o2) {

Student s1=(Student)o1;

Student s2=(Student)o2;

if(s1.age==s2.age)

return 0;

else if(s1.age>s2.age)

return 1;

else

return -1;

class NameComparator implements Comparator{

public int compare(Object o1, Object o2) {

Student s1=(Student)o1;

Student s2=(Student)o2;
return s1.name.compareTo(s2.name);

class FeesComparator implements Comparator {

public int compare(Object o1,Object o2) {

Student s1=(Student)o1;

Student s2=(Student)o2;

if(s1.fees==s2.fees)

return 0;

else if(s1.fees>s2.fees)

return 1;

else

return -1;

public class Temp1 {


public static void main(String[] args) {

ArrayList sl=new ArrayList();

sl.add(new Student(1,"Shruthi",10000.00f,"cse",1,1,18));

sl.add(new Student(2,"Venky",15000.00f,"ise",1,2,20));

sl.add(new Student(3,"Bella",17000.00f,"ece",1,1,19));

sl.add(new Student(3,"Alina",12000.00f,"eee",1,1,19));

sl.add(new Student(3,"Riya” 11000.00f,"mech",1,1,21));

System.out.println("Sorting by Name");

System.out.println("_______________");

Collections.sort(sl,new NameComparator());

Iterator itr=sl.iterator();

while(itr.hasNext()){

Student st=(Student)itr.next();

System.out.println(st. );

}
System.out.println("Sorting by age");

System.out.println("______________");

Collections.sort(sl,new AgeComparator());

Iterator itr2=sl.iterator();

while(itr2.hasNext()) {

Student st=(Student)itr2.next();

System.out.println(st );

System.out.println("Sorting by fees");

System.out.println("______________");

Collections.sort(sl,new FeesComparator());

Iterator itr1=sl.iterator();

while(itr1.hasNext()){

Student st=(Student)itr1.next();

System.out.println(st);

}
}

You might also like