Mini Project – 1

Develop a mini project, a console based Java Application, which can provide below options to manage Employee details in Database.

1. Create New Employee.
2. Delete an Employee, based on Employee ID
3. View Details of an Employee, based on Employee ID
4. View Details of all Employee
5. Exit from Application.

Whenever user selects an option, that specific Task need to be performed, and again above options need to be displayed, until user selects option 5, above.

import java.io.BufferedReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
import java.io.*;

public class Project {
	public static void main(String[] args) throws IOException {
		Scanner scanner=new Scanner(System.in);
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		
		int choice='5';
		try{
			Class.forName("com.mysql.jdbc.Driver");
			Connection con=DriverManager.getConnection("jdbc:mysql://localhost/companydetails","root","cherry");
		System.out.println("Connection is Established...");
		System.out.println("\n");
		Statement st=con.createStatement();
		do{
			System.out.println("Employees Menu");
			System.out.println("1.To Create New Employees.");
			System.out.println("2.To Delete An Employees.");
			System.out.println("3.To Display Details of one Employees.");
			System.out.println("4.To Display Details of all Employees.");
			
			System.out.println("5.Exit.");
			System.out.println("Enter Your Choice.");
			choice=scanner.nextInt();
			switch(choice)
			{
			case 1:
				System.out.println("Enter Employees id");
				System.out.println("Enter Employees Name");
				System.out.println("Enter Employees salary");
				System.out.println("Enter Employees Age");
				String eid = br.readLine();
				String ename = br.readLine();
				String esalary=br.readLine();
				String eage=br.readLine();
				st.executeUpdate("insert into employees values(eid,'"+ename+"',"+eage+" ,"+esalary+" )");
				System.out.println("Succesfully Employees Added.");
				
			case 2:
				System.out.println("Enter Employees id");
			    String Eid = br.readLine();
				st.executeUpdate("delete from employees where eid="+Eid);
				System.out.println("Succesfully Employees Deleted.");
				break;
			case 3:
				System.out.println("Enter Employees id");
			    String Eid1 = br.readLine();
				//st.executeQuery("select * from employees where eid="+Eid1);
				Statement st4=con.createStatement();
            	
            	String sql5="select eid,ename,eage,esalary from employees where eid="+Eid1+"";
            	
            	ResultSet rs5=st4.executeQuery(sql5);
            while(rs5.next())
            	{
            		
            		int id1=rs5.getInt("eid");
            		
            		String name=rs5.getString("ename");
            		
            		int age=rs5.getInt("eage");
            		
            		int salary=rs5.getInt("esalary");
            		
            		System.out.println("Emp Id:"+Eid1);
            		
            		System.out.println("Emp Name:"+name);
            		
            		System.out.println("Emp Age:"+age);
            		
            		System.out.println("Emp Salary:"+salary);
            		
            		System.out.println("Verification sucessfull!!!");
            	}
            		break;
			
			case 4:
				ResultSet rs=st.executeQuery("select * from employees");
				while(rs.next())
				{
					System.out.println(rs.getInt(1)+"\t\t"+rs.getString(2)+"\t\t"+rs.getInt(3)+"\t\t"+rs.getInt(4));
				}
				break;
			}
		}while(choice!=6);
		con.close();
		}catch(ClassNotFoundException e)
		{
			e.printStackTrace();
			
		}catch(SQLException e)
		{
			e.printStackTrace();
			}

	}
		
	}