DOWNLOAD THE ENTIRE WORKING PROJECT FROM HERE(netbeans project file)
click on clone or download
OR follow yourself
Follow the steps carefully-
1. create a new netbeans project (name it as emp_management, or anything of your choice)
2. now create a package named as emp.gui (it is named as PROJECTNAME.PACKAGENAME).
3. now create a new JFrame Form and create the UI of the project.
4. before dragging the elements first place a PANEL in the frame and select colors then start placing the elements
5. Now create the following UI-
1 option frame- name it as option_frame
2 add employee frame name it as add_emp
3 search emp name it as search_emp
4 view all emp name it as view_all_emp
5. Now we will create a directory structure as following-
(Notice here how the package names are given packagename.source)
please make sure you name your package and java classes as mentioned
dao-data access objects
pojo-plain old java objects
6. create the required package and the required classes as shown in the diagram and create a table in SQL with the command-
create table emp_table(empno number(5),empname varchar2(20), salary number(10));
desc emp_table;
select * from emp_table;
7. now we will start off with pojo (most easiest)
contains the data elements getter and setters
CODE-
public class Emp {
private int empno;
private String ename;
private double sal;
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}
}
8. now we will open a JDBC connection- in dbuits.dbconnection
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
/**
*
* @author Naman Khurpia
*/
public class DBConnection {
private static Connection conn=null;
static
{
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
conn=DriverManager.getConnection("jdbc:oracle:thin:@//pc-name:1521/xe","username","password");
JOptionPane.showMessageDialog(null,"Successfully loaded","Sucess!",JOptionPane.INFORMATION_MESSAGE);
}
catch(ClassNotFoundException e)
{
JOptionPane.showMessageDialog(null,"class not found exception"+ e.getMessage(),"Exception",JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
catch (SQLException ex)
{
JOptionPane.showMessageDialog(null,"SQL Exception"+ ex.getMessage(),"Exception",JOptionPane.ERROR_MESSAGE);
ex.printStackTrace();
}
}
public static Connection getConnection()//method will return us the connection object when we need it in other classes
{
return(conn);
}
public static void closeConnection()//method will close connection after use
{
if(conn!=null)
{
try{
conn.close();
}
catch(SQLException ex)
{
JOptionPane.showMessageDialog(null,"Error while disconnecting","Exception",JOptionPane.ERROR_MESSAGE);
}
}
}
}
9. now we will code in EmpDao.java
import emp.dbuitl.DBConnection;
import emp.pojo.Emp;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.swing.JOptionPane;
/**
*
* @author Naman Khurpia
*/
//data access objects
public class EmpDao {
//function for adding the employee
public static boolean AddEmployee(Emp e)throws SQLException
{
Connection conn=DBConnection.getConnection();
PreparedStatement ps=conn.prepareStatement("insert into emp_table values(?,?,?)");
ps.setInt(1,e.getEmpno());
ps.setString(2,e.getEname());
ps.setDouble(3,e.getSal());
int count=ps.executeUpdate();
if(count==0)
{
return false;
}
else
{
return true;
}
}
//function for show all employees
public static ArrayList<Emp> getAllEmp()throws SQLException
{
Connection conn=DBConnection.getConnection();
ArrayList<Emp> empList=new ArrayList<>();
Statement st=conn.createStatement();
ResultSet rs=st.executeQuery("select empno,empname,salary from emp_table");
while(rs.next())
{
Emp e=new Emp();
e.setEmpno(rs.getInt("empno"));
e.setEname(rs.getString("empname"));
e.setSal(rs.getDouble("salary"));
empList.add(e);
}
return empList;
}
//function for update employee
public static boolean UpdateEmployee(Emp e) throws SQLException
{
Connection conn=DBConnection.getConnection();
PreparedStatement ps=conn.prepareStatement("update emp_table set empname=? , salary=? where empno=?");
ps.setString(1,e.getEname());
ps.setDouble(2,e.getSal());
ps.setInt(3,e.getEmpno());
int c=ps.executeUpdate();
return c != 0;//returns true if c is 1
}
//function for delete employee
public static boolean DeleteEmp(int enumber)throws SQLException
{
Connection conn=DBConnection.getConnection();
PreparedStatement ps=conn.prepareStatement("Delete from emp_table where empno=?");
ps.setInt(1,enumber);
int c=ps.executeUpdate();
return c!=0;
}
//function to perform search
public static Emp SearchEmp(int enumber)throws SQLException,NullPointerException
{
Emp e=null;
Connection conn=DBConnection.getConnection();
//PreparedStatement ps=conn.prepareStatement("select empname , salary,empno from emp_table where empno=?");
//ps.setInt(1, enumber);
Statement st=conn.createStatement();
ResultSet rs=st.executeQuery("select empname , salary,empno from emp_table where empno="+enumber);
while(rs.next())
{
e=new Emp();
e.setEname(rs.getString("empname"));
e.setSal(rs.getDouble("salary"));
}
return e;
}
}
10. now we will code in GUI-
Option frame-
CODE- //copy paste correctly i have removed initComponents(none of your business) from my code-
import javax.swing.JOptionPane;
/**
*
* @author Naman Khurpia
*/
public class optionFrame extends javax.swing.JFrame {
public optionFrame() {
initComponents();
this.setLocationRelativeTo(this);
}
private void dotaskActionPerformed(java.awt.event.ActionEvent evt) {
if(jrbAddEmp.isSelected())
{
add_empoyee_frame add=new add_empoyee_frame();
add.setVisible(true);
this.dispose();
}
else if (jrbSearchEmp.isSelected())
{
Search_frame search=new Search_frame();
search.setVisible(true);
this.dispose();
}
else if(jrbShowAllEmp.isSelected())
{
Showall_frame show=new Showall_frame();
show.setVisible(true);
this.dispose();
}
else if(jrbUpdateEmp.isSelected())
{
UpdateEmp_frame update=new UpdateEmp_frame();
update.setVisible(true);
this.dispose();
}
else if(jrbDeleteEmp.isSelected())
{
DeleteEmp_frame delete=new DeleteEmp_frame();
delete.setVisible(true);
this.dispose();
}
else if(jrbQuit.isSelected())
{
int ans=0;
ans=JOptionPane.showConfirmDialog(null, "Are you sure?", "Quitting", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
if(ans==JOptionPane.YES_OPTION)
{
System.exit(0);
}
}
else
{
JOptionPane.showMessageDialog(null,"Please select an option","Error",JOptionPane.ERROR_MESSAGE);
}
}
11. now we will code in add employee-
CODE- (Copy paste correctly)
import emp.dao.EmpDao;
import emp.dbuitl.DBConnection;
import emp.pojo.Emp;
import java.sql.SQLException;
import javax.swing.JOptionPane;
/**
*
* @author Naman Khurpia
*/
public class add_empoyee_frame extends javax.swing.JFrame {
public add_empoyee_frame() {
initComponents();
this.setLocationRelativeTo(this);
//DBConnection.getConnection();
}
private void AddEmpActionPerformed(java.awt.event.ActionEvent evt) {
if(validateInput()==false)
{
JOptionPane.showMessageDialog(null,"Please enter all details","Input not entered",JOptionPane.ERROR_MESSAGE);
return;
}
else
{
try
{
Emp e=new Emp();
int empNo=Integer.parseInt(txtEmpNo.getText());
String eName=txtEmpName.getText();
double sal=Double.parseDouble(txtSal.getText());
e.setEmpno(empNo);
e.setEname(eName);
e.setSal(sal);
if(EmpDao.AddEmployee(e))//a method that returns true if all correct values inserted
{
JOptionPane.showMessageDialog(null, "Record inserted Successfully","Success!",JOptionPane.INFORMATION_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(null,"Please enter correct values","Input incorrect",JOptionPane.ERROR_MESSAGE);
}
}
catch(SQLException e)
{
JOptionPane.showMessageDialog(null,"SQL Exception ","Exception",JOptionPane.ERROR_MESSAGE);
}
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//back button
optionFrame option=new optionFrame();
option.setVisible(true);
this.dispose();
}
//create at end
private boolean validateInput()
{
if(txtEmpNo.getText().isEmpty() || txtEmpName.getText().isEmpty() || txtSal.getText().isEmpty())
{
return false;
}
else
{
return true;
}
}
}
12. Now we will create a new page for view all employees
for that we will code in DAO (as it doesn't interact with the user)
and creation of the object is minimized.
CODE- for viewing all employee we will code in GUI frame ShowAllEmp
import emp.dao.EmpDao;
import emp.pojo.Emp;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JOptionPane;
/**
*
* @author Naman Khurpia
*/
public class Showall_frame extends javax.swing.JFrame {
public Showall_frame() {
initComponents();
this.setLocationRelativeTo(this);
}
private void btnShowAllEmpActionPerformed(java.awt.event.ActionEvent evt) {
//show all recortds
try {
ArrayList<Emp> empList=EmpDao.getAllEmp();
if(empList.isEmpty())
{
JOptionPane.showMessageDialog(null,"This table has no records","Empty Table",JOptionPane.INFORMATION_MESSAGE);
}
else
{
String str=new String();
txtAllEmp.setText("");
str="EMPNO \t ENAME \t SALARY\n\n";
for(Emp e:empList)
{
str=str+e.getEmpno()+"\t"+e.getEname()+"\t"+e.getSal()+"\n";
}
txtAllEmp.setText(str);
}
}
catch(SQLException e)
{
JOptionPane.showMessageDialog(null,"SQLException here","Exception",JOptionPane.ERROR_MESSAGE);
}
}
}
13. For Update employees-
CODE-
import emp.dao.EmpDao;
import emp.pojo.Emp;
import java.sql.SQLException;
import javax.swing.JOptionPane;
/**
*
* @author Naman Khurpia
*/
public class Update_emp_frame extends javax.swing.JFrame {
public Update_emp_frame() {
initComponents();
this.setLocationRelativeTo(this);
}
private void btnUpdateEmpActionPerformed(java.awt.event.ActionEvent evt) {
//update butoon
if(validateInput()==false)
{
JOptionPane.showMessageDialog(null,"Please enter Emp number to update details","Input not entered",JOptionPane.ERROR_MESSAGE);
return;
}
else
{
try
{
Emp e=new Emp();
int empNo=Integer.parseInt(txtEmpNo.getText());
String eName=txtEmpName.getText();
double sal=Double.parseDouble(txtSal.getText());
e.setEmpno(empNo);
e.setEname(eName);
e.setSal(sal);
if(EmpDao.UpdateEmployee(e))//a method that returns true if all correct values inserted
{
JOptionPane.showMessageDialog(null, "Record updated Successfully","Success!",JOptionPane.INFORMATION_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(null,"Please enter correct values","Input incorrect",JOptionPane.ERROR_MESSAGE);
}
}
catch(SQLException e)
{
JOptionPane.showMessageDialog(null,"SQL Exception here"+e.getMessage(),"Exception",JOptionPane.ERROR_MESSAGE);
}
catch(NumberFormatException e)
{
JOptionPane.showMessageDialog(null,"NumberFormat Exception ","Exception",JOptionPane.ERROR_MESSAGE);
}
}
// TODO add your handling code here:
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
optionFrame option=new optionFrame();
option.setVisible(true);
this.dispose();
}
//at the bottom
private boolean validateInput()
{
if(txtEmpNo.getText().isEmpty())
{
return false;
}
else
{
return true;
}
}
}
14. To create delete frame
CODE-
import emp.dao.EmpDao;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
/**
*
* @author Naman Khurpia
*/
public class DeleteEmp_frame extends javax.swing.JFrame {
public DeleteEmp_frame() {
initComponents();
this.setLocationRelativeTo(this);
}
private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {
if(txtEmpNo.getText().isEmpty())
{
JOptionPane.showMessageDialog(null,"Please enter the emp no","Input not entered",JOptionPane.ERROR_MESSAGE);
}
else
{
try {
int empno=Integer.parseInt(txtEmpNo.getText());
if(EmpDao.DeleteEmp(empno))
{
JOptionPane.showMessageDialog(null, "Record Successfully Deleted","Success!",JOptionPane.INFORMATION_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(null, "Record deosn't exist","Error!",JOptionPane.ERROR_MESSAGE);
}
}
catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "SQL Exception","Exception",JOptionPane.ERROR_MESSAGE);
}
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
optionFrame option=new optionFrame();
option.setVisible(true);
this.dispose();
}
15. Now for Searching
CODE-
import emp.dao.EmpDao;
import emp.pojo.Emp;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
/**
*
* @author Naman Khurpia
*/
public class Search_frame extends javax.swing.JFrame {
public Search_frame() {
initComponents();
this.setLocationRelativeTo(this);
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
//back
optionFrame option=new optionFrame();
option.setVisible(true);
this.dispose();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if(txtEmpNumber.getText().isEmpty())
{
JOptionPane.showMessageDialog(null,"Please enter emp name","Input not entered",JOptionPane.ERROR_MESSAGE);
}
else
{
try
{
int e=Integer.parseInt(txtEmpNumber.getText());
Emp emp=EmpDao.SearchEmp(e);
if(emp==null)
{
JOptionPane.showMessageDialog(null,"No Record Exist","Norecord",JOptionPane.INFORMATION_MESSAGE);
}
else
{
txtEmpName.setText(emp.getEname());
txtSalary.setText(Double.toString(emp.getSal()));//converting the received double into string by calling toString()
}
}
catch (SQLException ex)
{
JOptionPane.showMessageDialog(null,"SQL Exception here","Exception",JOptionPane.ERROR_MESSAGE);
}
}
}
HOPE THE CODE IS COMPLETE !! Obviously it is.
HAPPY CODING !!