2
2
index.jsp(View)
</form>
login-success.jsp(view)
<%@page import="com.pu.LoginBean"%>
out.print("Welcome, "+bean.getName());
%>
login-error.jsp(View)
package com.pu;
import java.lang.*;
return name;
this.name = name;
return password;
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");
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.*;
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 {
String ename;
int eno,age;
String query="";
switch(ch) {
case 1:
eno = sc.nextInt();
ename = sc.next();
age = sc.nextInt();
query = "INSERT INTO emp " + "VALUES (" + eno+ ",'" + ename+"',"+ age+")";
stmt.executeUpdate(query);
break;
case 2:
eno = sc.nextInt();
stmt.executeUpdate(query);
break;
case 3:
PreparedStatement ps = null;
ps = con.prepareStatement(query);
eno = sc.nextInt();
ename = sc.next();
ps.setString(1, ename);
ps.setInt(2, eno);
ps.executeUpdate();
break;
case 4:
while(rs.next())
System.out.println("Enter another(1/0)");
ans = sc.nextInt();
}while(ans==1);
con.close();
}catch(Exception e){ System.out.println(e);}
import java.io.*;
import java.util.*;
class 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";
return rollno + " "+ name + " " + fees + " " + branch + " " + year + sem + " " + age + " " + clg +
"\n";
}
}
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;
Student s1=(Student)o1;
Student s2=(Student)o2;
return s1.name.compareTo(s2.name);
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;
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));
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);
}
}