You are on page 1of 3

ANDRES, MONICA

BSCS-4A
EMPLOYEE DATABASE
}
package employee; private static void createConnection()
import java.util.*; {
import java.sql.*; try{
import java.util.logging.Level;
import java.util.logging.Logger; Class.forName("org.apache.derby.jdbc.ClientDriver").ne
import javax.swing.JOptionPane; wInstance();
public class Employee { conn = DriverManager.getConnection(dbURL);
private static String dbURL = }
catch(Exception except)
"jdbc:derby://localhost:1527/Employee;create=true;use {
r=me;password=me"; except.printStackTrace();
private static Connection conn = null; }
private static Statement stmt = null; }
public static void selectEmployee(){
public static void main(String[] args) { try{
char x; stmt = conn.createStatement();
do{ ResultSet resultEmployee =
Scanner input = new Scanner(System.in); stmt.executeQuery("SELECT * FROM
createConnection(); EMPLOYEE_INFORMATION");
System.out.println("-----EMPLOYEE-----"); ResultSetMetaData rsmd =
System.out.println("Transactions :"); resultEmployee.getMetaData();
System.out.println("1. SELECT" + "\n" + "2. int numbercols = rsmd.getColumnCount();
INSERT" + "\n" + "3. DELETE" + "\n" + "4. UPDATE" + "\ for (int i=1; i<=numbercols; i++)
n" + "5. JOINING"); {
int choice = input.nextInt(); System.out.print(rsmd.getColumnLabel(i) + "\t\
t");
if (choice == 1){ }
selectEmployee(); System.out.println();
} while (resultEmployee.next()){
else if (choice == 2){ int empid = resultEmployee.getInt(1);
insertEmployee(); String name = resultEmployee.getString(2);
} String address = resultEmployee.getString(3);
else if (choice == 3){ String contact = resultEmployee.getString(4);
deleteEmployee(); System.out.println(empid + "\t\t" + name + "\
} t\t" + address + "\t\t" + contact);
else if (choice == 4){ }
updateEmployee(); resultEmployee.close();
} stmt.close();
else if (choice == 5){ }
joining(); catch(SQLException sqlExcept){
} sqlExcept.printStackTrace();
else{ }
System.out.println("ERROR INPUTTING OF }
DATA"); public static void insertEmployee(){
} try{
System.out.println("do you want to continue??? Scanner input = new Scanner(System.in);
[Y|N]"); System.out.println("INSERT EMPLOYEE");
x = input.next().charAt(0); System.out.println("Enter id :");
}while(x == 'y' || x == 'Y'); int id = input.nextInt();
ANDRES, MONICA
BSCS-4A
EMPLOYEE DATABASE
System.out.println("Enter name :"); + ", EMPCONTACT = '" + contact + "' WHERE
String name = input.next(); EMPID = "+ id +"");
System.out.println("Enter address :"); stmt.close();
String address = input.next(); }
System.out.println("Enter contact :"); catch (SQLException sqlExcept){
String contact = input.next(); sqlExcept.printStackTrace();
stmt = conn.createStatement(); }
stmt.execute("insert into }
EMPLOYEE_INFORMATION values (" + public static void joining(){
id + ",'" + name + "','" + address + "','" + try{
contact + "')"); stmt = conn.createStatement();
stmt.close(); ResultSet resultEmployee =
} stmt.executeQuery("select e.EMPID, e.EMPNAME,
catch (SQLException sqlExcept){ p.BASIC, p.ALLOWANCE,"
sqlExcept.printStackTrace(); + "(p.BASIC + p.ALLOWANCE) as SALARY,
} ((p.BASIC + p.ALLOWANCE) * 0.12) as TAX"
} + " from EMPLOYEE_INFORMATION e,
public static void deleteEmployee(){ EMPLOYEE_PAYROLL p"
try{ + " WHERE p.EMPID = e.EMPID");
selectEmployee();
stmt = conn.createStatement(); ResultSetMetaData rsmd =
Scanner input = new Scanner(System.in); resultEmployee.getMetaData();
System.out.println("DELETE EMPLOYEE"); int numbercols = rsmd.getColumnCount();
System.out.println("Enter id to delete :"); for (int i=1; i<=numbercols; i++)
int id = input.nextInt(); {
stmt.execute("DELETE FROM System.out.print(rsmd.getColumnLabel(i) + "\t\
EMPLOYEE_INFORMATION WHERE EMPID = "+ id +""); t");
stmt.close(); }
} System.out.println();
catch (SQLException sqlExcept){ while (resultEmployee.next()){
sqlExcept.printStackTrace(); int empid = resultEmployee.getInt(1);
} String name = resultEmployee.getString(2);
} int basic = resultEmployee.getInt(3);
public static void updateEmployee(){ int allowance = resultEmployee.getInt(4);
try{ int salary = resultEmployee.getInt(5);
selectEmployee(); int tax = resultEmployee.getInt(6);
stmt = conn.createStatement();
Scanner input = new Scanner(System.in); System.out.println(empid + "\t\t" + name + "\
System.out.println("UPDATE EMPLOYEE"); t\t" + basic + "\t\t" + allowance + "\t\t" + salary + "\t\t"
System.out.println("Enter id to update :"); + tax);
int id = input.nextInt(); }
System.out.println("Enter name :"); resultEmployee.close();
String name = input.next(); stmt.close();
System.out.println("Enter address :"); }
String address = input.next(); catch(SQLException sqlExcept){
System.out.println("Enter contact :"); sqlExcept.printStackTrace();
String contact = input.next(); }
stmt.execute("UPDATE }
EMPLOYEE_INFORMATION SET EMPNAME = '" + name + }
"', EMPADDRESS = '" + address + "'"
ANDRES, MONICA
BSCS-4A
EMPLOYEE DATABASE

You might also like