Professional Documents
Culture Documents
Java Programming (Cse1007) Digital Assignment 1: Name: Aashish Sharma Reg. No: 19BCE0971 Slot: G2
Java Programming (Cse1007) Digital Assignment 1: Name: Aashish Sharma Reg. No: 19BCE0971 Slot: G2
DIGITAL ASSIGNMENT 1
Name : Aashish Sharma
Reg. No: 19BCE0971
Slot : G2
--------------------------------------------------------------------------------------------------------------------------------------
2. Create a JAVAFX program that allows the user to store the records of employees name, age,
salary and experience into the database. And also, a function to search the employee name with
his name.
Solution:
package sample.util;
import com.sun.rowset.CachedRowSetImpl;
import java.sql.*;
/**
*/
//Connection
//Connection String
//Connect to DB
Class.forName(JDBC_DRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw e;
try {
conn = DriverManager.getConnection(connStr);
} catch (SQLException e) {
e.printStackTrace();
throw e;
//Close Connection
try {
conn.close();
throw e;
try {
dbConnect();
//Create statement
stmt = conn.createStatement();
resultSet = stmt.executeQuery(queryStmt);
//CachedRowSet Implementation
crs.populate(resultSet);
} catch (SQLException e) {
throw e;
} finally {
if (resultSet != null) {
//Close resultSet
resultSet.close();
if (stmt != null) {
//Close Statement
stmt.close();
//Close connection
dbDisconnect();
}
//Return CachedRowSet
return crs;
try {
dbConnect();
//Create Statement
stmt = conn.createStatement();
stmt.executeUpdate(sqlStmt);
} catch (SQLException e) {
throw e;
} finally {
if (stmt != null) {
//Close statement
stmt.close();
//Close connection
dbDisconnect();
package sample.model;
import javafx.beans.property.*;
import java.sql.Date;
/**
*/
//Constructor
public Employee() {
//employee_id
public int getEmployeeId() {
return employee_id.get();
this.employee_id.set(employeeId);
return employee_id;
//first_name
return first_name.get();
this.first_name.set(firstName);
return first_name;
//last_name
return last_name.get();
this.last_name.set(lastName);
return last_name;
this.email.set(email);
return email;
//phone_number
return phone_number.get();
this.phone_number.set(phoneNumber);
return phone_number;
//hire_date
return hire_date.get();
this.hire_date.set(hireDate);
return hire_date;
//job_id
return job_id.get();
}
this.job_id.set(jobId);
return job_id;
//salary
return salary.get();
this.salary.set(salary);
return salary;
//commission_pct
return commission_pct.get();
this.commission_pct.set(commissionPct);
return commission_pct;
//manager_id
return manager_id.get();
}
public void setManagerId(int managerId){
this.manager_id.set(managerId);
return manager_id;
//department_id
return department_id.get();
this.manager_id.set(departmentId);
return department_id;
package sample.model;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import sample.util.DBUtil;
import java.sql.ResultSet;
import java.sql.SQLException;
//*******************************
//SELECT an Employee
//*******************************
try {
return employee;
} catch (SQLException e) {
System.out.println("While searching an employee with " + empId + " id, an error occurred: " + e);
//Return exception
throw e;
//Use ResultSet from DB as parameter and set Employee Object's attributes and return employee
object.
if (rs.next()) {
emp.setEmployeeId(rs.getInt("EMPLOYEE_ID"));
emp.setFirstName(rs.getString("FIRST_NAME"));
emp.setLastName(rs.getString("LAST_NAME"));
emp.setEmail(rs.getString("EMAIL"));
emp.setPhoneNumber(rs.getString("PHONE_NUMBER"));
emp.setHireDate(rs.getDate("HIRE_DATE"));
emp.setJobId(rs.getString("JOB_ID"));
emp.setSalary(rs.getInt("SALARY"));
emp.setCommissionPct(rs.getDouble("COMMISSION_PCT"));
emp.setManagerId(rs.getInt("MANAGER_ID"));
emp.setDepartmantId(rs.getInt("DEPARTMENT_ID"));
return emp;
//*******************************
//SELECT Employees
//*******************************
try {
return empList;
} catch (SQLException e) {
//Return exception
throw e;
ClassNotFoundException {
emp.setEmployeeId(rs.getInt("EMPLOYEE_ID"));
emp.setFirstName(rs.getString("FIRST_NAME"));
emp.setLastName(rs.getString("LAST_NAME"));
emp.setEmail(rs.getString("EMAIL"));
emp.setPhoneNumber(rs.getString("PHONE_NUMBER"));
emp.setHireDate(rs.getDate("HIRE_DATE"));
emp.setJobId(rs.getString("JOB_ID"));
emp.setSalary(rs.getInt("SALARY"));
emp.setCommissionPct(rs.getDouble("COMMISSION_PCT"));
emp.setManagerId(rs.getInt("MANAGER_ID"));
emp.setDepartmantId(rs.getInt("DEPARTMENT_ID"));
empList.add(emp);
return empList;
//*************************************
//*************************************
public static void updateEmpEmail (String empId, String empEmail) throws SQLException,
ClassNotFoundException {
String updateStmt =
"BEGIN\n" +
" COMMIT;\n" +
"END;";
try {
DBUtil.dbExecuteUpdate(updateStmt);
} catch (SQLException e) {
throw e;
//*************************************
//DELETE an employee
//*************************************
String updateStmt =
"BEGIN\n" +
" COMMIT;\n" +
"END;";
try {
DBUtil.dbExecuteUpdate(updateStmt);
} catch (SQLException e) {
throw e;
//*************************************
//INSERT an employee
//*************************************
public static void insertEmp (String name, String lastname, String email) throws SQLException,
ClassNotFoundException {
String updateStmt =
"BEGIN\n" +
"VALUES\n" +
"END;";
try {
DBUtil.dbExecuteUpdate(updateStmt);
} catch (SQLException e) {
throw e;
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
fx:controller="sample.controller.EmployeeController">
<children>
wrapText="true" />
<font>
</font></Label>
<children>
</children>
</VBox>
<children>
</VBox>
prefWidth="393.0" tableMenuButtonVisible="true">
<columns>
</columns>
</TableView>
</children>
</AnchorPane>
package sample.controller;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import sample.model.Employee;
import sample.model.EmployeeDAO;
import java.sql.Date;
import java.sql.SQLException;
/**
*/
@FXML
@FXML
@FXML
@FXML
@FXML
@FXML
@FXML
@FXML
@FXML
@FXML
@FXML
@FXML
private TableColumn<Employee, String> empPhoneNumberColumn;
@FXML
//Search an employee
@FXML
try {
populateAndShowEmployee(emp);
} catch (SQLException e) {
e.printStackTrace();
throw e;
@FXML
try {
populateEmployees(empData);
throw e;
}
//Initializing the controller class.
//This method is automatically called after the fxml file has been loaded.
@FXML
/*
The setCellValueFactory(...) that we set on the table columns are used to determine
which field inside the Employee objects should be used for the particular column.
The arrow -> indicates that we're using a Java 8 feature called Lambdas.
We're only using StringProperty values for our table columns in this example.
*/
empIdColumn.setCellValueFactory(cellData ->
cellData.getValue().employeeIdProperty().asObject());
empPhoneNumberColumn.setCellValueFactory(cellData ->
cellData.getValue().phoneNumberProperty());
//Populate Employee
@FXML
empData.add(emp);
employeeTable.setItems(empData);
}
//Set Employee information to Text Area
@FXML
@FXML
if (emp != null) {
populateEmployee(emp);
setEmpInfoToTextArea(emp);
} else {
@FXML
employeeTable.setItems(empData);
//Update employee's email with the email which is written on newEmailText field
@FXML
try {
EmployeeDAO.updateEmpEmail(empIdText.getText(),newEmailText.getText());
resultArea.setText("Email has been updated for, employee id: " + empIdText.getText() + "\n");
} catch (SQLException e) {
@FXML
try {
EmployeeDAO.insertEmp(nameText.getText(),surnameText.getText(),emailText.getText());
} catch (SQLException e) {
throw e;
@FXML
try {
EmployeeDAO.deleteEmpWithId(empIdText.getText());
} catch (SQLException e) {
throw e;
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import java.io.IOException;
@Override
this.primaryStage = primaryStage;
initRootLayout();
showEmployeeView();
try {
loader.setLocation(Main.class.getResource("view/RootLayout.fxml"));
Scene scene = new Scene(rootLayout); //We are sending rootLayout to the Scene.
controller.setMain(this);*/
} catch (IOException e) {
e.printStackTrace();
try {
loader.setLocation(Main.class.getResource("view/EmployeeView.fxml"));
rootLayout.setCenter(employeeOperationsView);
} catch (IOException e) {
e.printStackTrace();
launch(args);
Output: