Professional Documents
Culture Documents
Input:: Sohini Paul 19070122169 Cs C2 PBL Assignment 8
Input:: Sohini Paul 19070122169 Cs C2 PBL Assignment 8
19070122169
CS C2
PBL ASSIGNMENT 8
INPUT:
//Database
package Assignment08;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
//Employee.java
package Assignment08;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.ResultSet;
import java.util.Scanner;
try{
con = Database.createConnection();
String sqlUpdate = "insert into employee values(?,?,?,?,?)";
preparedStatement = con.prepareStatement(sqlUpdate);
preparedStatement.setInt(1,empno);
preparedStatement.setString(2,ename);
preparedStatement.setString(3,department);
preparedStatement.setString(4,job);
preparedStatement.setDouble(5,salary);
preparedStatement.executeUpdate();
return true;
} catch(SQLException E)
{
E.printStackTrace();
return false;
}
finally{
try{
if(preparedStatement!=null)
preparedStatement.close();
if(con!=null)
con.close();
} catch(SQLException E)
{
E.printStackTrace();
}
}
}
try{
con=Database.createConnection();
statement = con.createStatement();
resultSet = statement.executeQuery("Select * from
employee");
while(resultSet.next())
{
System.out.println("-------------------------------------------
----------------");
System.out.println("Employee no: " +
resultSet.getInt("empno"));
System.out.println("Employee name: " +
resultSet.getString("ename"));
System.out.println("Department: " +
resultSet.getString("department"));
System.out.println("Job:" + resultSet.getString("job"));
System.out.println("Salary: " +
resultSet.getDouble("salary"));
}
} catch(SQLException E)
{
E.printStackTrace();
}
finally{
try{
if(resultSet!=null)
resultSet.close();
if(statement!=null)
statement.close();
if(con!=null)
con.close();
} catch(SQLException E)
{
E.printStackTrace();
}
}
}
try{
con=Database.createConnection();
statement = con.createStatement();
resultSet = statement.executeQuery("Select * from
employee where empno=" + empno);
if(resultSet.next())
{
Employee E = new Employee();
E.setDepartment(resultSet.getString("department"));
E.setJob(resultSet.getString("job"));
E.setEmpno(resultSet.getInt("empno"));
E.setEname(resultSet.getString("ename"));
E.setSalary(resultSet.getDouble("salary"));
System.out.println("-------------------------------------------
----------------");
System.out.println("Employee no: " + E.getEmpno());
System.out.println("Employee name: " + E.getEname());
System.out.println("Department: " + E.getDepartment());
System.out.println("Job:" + E.getJob());
System.out.println("Salary: " + E.getSalary());
return E;
}
} catch(SQLException E)
{
E.printStackTrace();
}
finally{
try{
if(resultSet!=null)
resultSet.close();
if(statement!=null)
statement.close();
if(con!=null)
con.close();
} catch(SQLException E)
{
E.printStackTrace();
}
}
return null;
}
try{
con=Database.createConnection();
String sql="Select * from employee where ename=?";
preparedStatement = con.prepareStatement(sql);
preparedStatement.setString(1,empName);
resultSet = preparedStatement.executeQuery();
while(resultSet.next())
{
System.out.println("-------------------------------------------
----------------");
System.out.println("Employee no: " +
resultSet.getInt("empno"));
System.out.println("Employee name: " +
resultSet.getString("ename"));
System.out.println("Department: " +
resultSet.getString("department"));
System.out.println("Job:" + resultSet.getString("job"));
System.out.println("Salary: " +
resultSet.getDouble("salary"));
}
} catch(SQLException E)
{
E.printStackTrace();
}
finally{
try{
if(resultSet!=null)
resultSet.close();
if(preparedStatement!=null)
preparedStatement.close();
if(con!=null)
con.close();
} catch(SQLException E)
{
E.printStackTrace();
}
}
}
try{
con=Database.createConnection();
String sql="update employee set salary=? where empno=?";
preparedStatement = con.prepareStatement(sql);
preparedStatement.setInt(2,this.empno);
preparedStatement.setDouble(1,this.salary);
preparedStatement.executeUpdate();
return true;
} catch(SQLException E)
{
E.printStackTrace();
}
finally{
try{
if(preparedStatement!=null)
preparedStatement.close();
if(con!=null)
con.close();
} catch(SQLException E)
{
E.printStackTrace();
}
}
return false;
}
try{
con=Database.createConnection();
//Main.java
package Assignment08;
import java.util.Scanner;
int opt;
while(true) {
System.out.println("\n\nEmployee Database Menu");
System.out.println("1.Add new Employee");
System.out.println("2.Display all employees");
System.out.println("3.Search using employee name");
System.out.println("4.Search using employee no");
System.out.println("5.Update salary");
System.out.println("6.Delete employee");
System.out.println("0.Exit");
System.out.println("Enter choice: ");
opt = scanner.nextInt();
scanner.nextLine();
switch(opt)
{
case 0:
System.out.println("Manual Exit!");
System.exit(0);
case 1:
Employee E = new Employee();
System.out.println("Enter employee no : ");
empno=scanner.nextInt();
scanner.nextLine();
System.out.println("Enter employee name: ");
ename=scanner.nextLine();
System.out.println("Enter department: ");
department=scanner.nextLine();
System.out.println("Enter job: ");
job=scanner.nextLine();
System.out.println("Enter salary: ");
salary=scanner.nextDouble();
scanner.nextLine();
E.setEmpno(empno);
E.setJob(job);
E.setDepartment(department);
E.setEname(ename);
E.setSalary(salary);
if(E.addToDB())
{
System.out.println(empno+" added to database!");
}
else
System.out.println(empno + " not added!");
break;
case 2:
Employee.display();
break;
case 3:
System.out.println("Enter employee name to search: ");
ename = scanner.nextLine();
Employee.search(ename);
break;
case 4:
System.out.println("Enter employee no to search: ");
empno = scanner.nextInt();
scanner.nextLine();
E = new Employee();
E = Employee.search(empno);
if(E==null)
{
System.out.println("Employee not found!");
}
break;
case 5:
System.out.println("Enter employee no to search: ");
empno=scanner.nextInt();
E = new Employee();
E = Employee.search(empno);
if(E==null)
{
System.out.println("Employee not found!");
break;
}
System.out.println("Enter updated salary: ");
salary = scanner.nextDouble();
E.setSalary(salary);
if(E.update())
System.out.println("Record updated succesfully!");
else
System.out.println("Record updating failed!");
break;
case 6:
System.out.println("Enter employee no to search: ");
empno=scanner.nextInt();
E = new Employee();
E = Employee.search(empno);
if(E==null)
{
System.out.println("Employee not found!");
break;
}
if(Employee.delete(empno))
System.out.println("Record deleted successfully!");
else
System.out.println("Record deletion failed!");
break;
default:
System.out.println("Invalid choice!");
}
}
}
}
OUTPUT: