0% found this document useful (0 votes)
25 views19 pages

Employee Management System UI

Information technology

Uploaded by

HÁRSH YÓGI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views19 pages

Employee Management System UI

Information technology

Uploaded by

HÁRSH YÓGI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

MainUI.

java

public class MainUI extends javax.swing.JFrame {


public MainUI() {
initComponents();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
AboutMe.setVisible(false);
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
new EmpAddUI().setVisible(true);

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {


new EmpDelUI().setVisible(true);
}

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {


new EmpEditUI().setVisible(true);
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
AboutMe.setVisible(true);
}
private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
new EmpNavUI().setVisible(true);
}
private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {
new EmpSearchUI().setVisible(true);
}
private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {
new EmpTable().setVisible(true);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MainUI().setVisible(true);

}
});
}
// Variables declaration - do not modify
private javax.swing.JDialog AboutMe;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JButton jButton8;
private javax.swing.JButton jButton9;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel10;
private javax.swing.JLabel jLabel11;
private javax.swing.JLabel jLabel12;
private javax.swing.JLabel jLabel13;
private javax.swing.JLabel jLabel14;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
private javax.swing.JPanel jPanel1;
private javax.swing.JSeparator jSeparator1;
// End of variables declaration

}
EmpAddUI.java
import java.sql.*;
import javax.swing.JOptionPane;
public class EmpAddUI extends javax.swing.JFrame {
public EmpAddUI() {
initComponents();
}
private void cmdExitActionPerformed(java.awt.event.ActionEvent evt) {
this.setVisible(false);
new MainUI().setVisible(true);
}

private void formWindowGainedFocus(java.awt.event.WindowEvent evt) {


txtNo.setEditable(false);

// Deactivate the Save button when form loads


cmdSave.setEnabled(false);
}

private void cmdNewActionPerformed(java.awt.event.ActionEvent evt) {


// Activate the Save button when New button clicked
cmdSave.setEnabled(true);
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","raj");
Statement stmt = null;
ResultSet rs = null; // ResultSet for publisher table.
String SQL = "SELECT * FROM emp";
stmt = con.createStatement(); // Connection string for ResultSet - rs.
rs = stmt.executeQuery(SQL);
int pno = 1;
int PID=0;
while (rs.next()) {
PID = rs.getInt("empid");
pno++;
}
PID++;
pno = PID;
txtNo.setText(Integer.toString(pno));
txtPName.setFocusable(true);
con.close();
rs.close();
stmt.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(this,e.getMessage());

}
}
private void cmdSaveActionPerformed(java.awt.event.ActionEvent evt) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","raj");
Statement stmt = null;
ResultSet rs = null;
String SQL = "SELECT * FROM emp";
stmt = con.createStatement();
rs = stmt.executeQuery(SQL);
int no = Integer.parseInt(txtNo.getText());
String nam = txtPName.getText();
String city = txtCity.getText();
String ph = txtPhone.getText();
char sex='Y';
if (jRadioButton1.isSelected())
sex = 'M';
else
sex='F';
int code = JOptionPane.showConfirmDialog(this, "Are you sure to add?", "Confirmation Dialog
Box", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);
if (code == JOptionPane.YES_OPTION) {
String strSQL = "INSERT INTO emp(empID, EmpName, EmpCity, EmpPhone, EmpSex) VALUES
("+(no)+", '"+(nam)+"', '"+(city)+"', '"+(ph)+"', '"+(sex)+"')";
stmt.executeUpdate(strSQL);
JOptionPane.showMessageDialog(this, "Record added successfully into Employee table");
}
con.close();
stmt.close();
rs.close();
cmdSave.setEnabled(false);
} catch (Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}

private void cmdClearActionPerformed(java.awt.event.ActionEvent evt) {


txtNo.setText("");
txtPName.setText("");
txtCity.setText("");
txtPhone.setText("");
cmdSave.setEnabled(false);
}
// Variables declaration - do not modify
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JButton cmdClear;
private javax.swing.JButton cmdExit;
private javax.swing.JButton cmdNew;
private javax.swing.JButton cmdSave;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JRadioButton jRadioButton2;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JSeparator jSeparator2;
private javax.swing.JTextField txtCity;
private javax.swing.JTextField txtNo;
private javax.swing.JTextField txtPName;
private javax.swing.JTextField txtPhone;
// End of variables declaration
}

EmpDelUI.java

import java.sql.*;
import javax.swing.JOptionPane;
import javax.swing.DefaultListModel;
public class EmpDelUI extends javax.swing.JFrame {

public EmpDelUI() {
initComponents();
}
Statement stmt = null;
ResultSet rs = null;
String SQL = "SELECT * FROM emp";

private void cmdDeleteActionPerformed(java.awt.event.ActionEvent evt) {


try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","raj");
stmt = con.createStatement();
rs = stmt.executeQuery(SQL);
int pno = Integer.parseInt(txtNo.getText().trim());
// Steps to confirm deletion
int opt = JOptionPane.showConfirmDialog(null, "Are you sure to delete this record ?");
if (opt == JOptionPane.YES_OPTION)
{
try {
String strSQL = "Delete from emp where empId = " + (pno);
int rowsEffected = stmt.executeUpdate(strSQL);
if (rowsEffected == 0)
JOptionPane.showMessageDialog(this, "Record does not exists");
else
{
JOptionPane.showMessageDialog(this,"Record Deleted");
// Text boxes cleared
txtNo.setText("");
txtName.setText("");
txtAdd.setText("");
txtPhone.setText("");
txtNo.setEditable(true);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Unable to delete");
}
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}

private void cmdExitActionPerformed(java.awt.event.ActionEvent evt) {


this.setVisible(false);
//new MainUI().setVisible(true);
}
private void jList1MouseClicked(java.awt.event.MouseEvent evt) {
// getSelectedValue() method extracts the current cursor location value into a variable
String st = (String) jList1.getSelectedValue();
// Extract the first 4 characters as roll number into a variable
String Id =st.trim().substring(0, 3);
String query = "SELECT * FROM emp WHERE empId = " + Id + ";";
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = (Connection)
DriverMnager.getConnection("jdbc:mysql://localhost:3306/test","root","raj");
// Create SQL statement and execute query.
stmt = con.createStatement();
rs = stmt.executeQuery(query);
if (rs.next()) {
String MName = rs.getString("empName");
String MCity = rs.getString("empCity");
String MPh = rs.getString("EmpPhone");
String MSex = rs.getString("EmpSex");
// Displaying the contents in respective text boxes.
txtNo.setText(Id);
txtName.setText(MName);
txtAdd.setText(MCity);
txtPhone.setText(MPh);
if (MSex.equals("M"))
jRadioButton1.setSelected(true);
else
jRadioButton2.setSelected(true);
txtNo.setEditable(false);
} else {
JOptionPane.showMessageDialog(null, "Record does not found in Student table");
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
private void formWindowGainedFocus(java.awt.event.WindowEvent evt) {
txtNo.setEditable(false);
// Creating a ListModel object dModel to perform DefaultListModel
DefaultListModel dModel = (DefaultListModel) jList1.getModel();
// Method to add elements into jList1 control
dModel.clear();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","raj");
stmt = con.createStatement();
rs = stmt.executeQuery(SQL);
while (rs.next()) {
String Pno = rs.getString("empId");
String PName = rs.getString("empName");
// To make the publisher no. as 4 digit because we will extract 4 digit from list value
// in mouse click event.
if (Pno.length() < 4)
{
int x = Pno.length();
int nl = 4 - x;
while (nl > 0){
Pno = Pno + " ";
nl--;
}
}
dModel.addElement(Pno + "- " + PName);
}
jList1.setModel(dModel);
con.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(this,e.getMessage());
e.printStackTrace();
}
}

// Variables declaration - do not modify


private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JButton cmdDelete;
private javax.swing.JButton cmdExit;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JList jList1;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JRadioButton jRadioButton2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField txtAdd;
private javax.swing.JTextField txtName;
private javax.swing.JTextField txtNo;
private javax.swing.JTextField txtPhone;
// End of variables declaration

}
EmpEditUI.java
import java.sql.*;
import javax.swing.JOptionPane;
import javax.swing.DefaultListModel;

public class EmpEditUI extends javax.swing.JFrame {


public EmpEditUI() {
initComponents();
}

private void formWindowGainedFocus(java.awt.event.WindowEvent evt) {


txtNo.setEditable(false);
DefaultListModel dModel = (DefaultListModel) jList1.getModel();
// Method to add elements into jList1 control
dModel.clear();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","raj");
Statement stmt = null;
ResultSet rs = null;
String SQL = "SELECT * FROM emp";
stmt = con.createStatement();
rs = stmt.executeQuery(SQL);
while (rs.next()) {
String Pno = rs.getString("empid");
String PName = rs.getString("empname");
// To make the publisher no. as 4 digit because we will extract 4 digit from list value
// in mouse click event.
if (Pno.length() < 4)
{
int x = Pno.length();
int nl = 4 - x;
while (nl > 0){
Pno = Pno + " ";
nl--;
}
}
dModel.addElement(Pno + "- " + PName);
}
jList1.setModel(dModel);
con.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(this,e.getMessage());

}
}

private void jList1MouseClicked(java.awt.event.MouseEvent evt) {


// getSelectedValue() method extracts the current cursor location value into a variable
String MID = (String) jList1.getSelectedValue();
// Extract the first 4 characters as publisher ID into a variable
String PN =MID.trim().substring(0, 3);
String query = "SELECT * FROM emp WHERE empId = " + PN + ";";
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","raj");
// Create SQL statement and execute query.
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
if (rs.next()) {
//PubN = rs.getString("pub_no");
String EName = rs.getString("empname");
String EAdd = rs.getString("empcity");
String EPh = rs.getString("empphone");
String ESex = rs.getString("empsex");
// Displaying the contents in respective text boxes.
txtNo.setText(PN);
txtName.setText(EName);
txtCity.setText(EAdd);
txtPh.setText(EPh);
if(ESex.equals("M"))
jRadioButton1.setSelected(true);
else
jRadioButton2.setSelected(true);
txtNo.setEditable(false);
// Close the operational object for Student
con.close();
stmt.close();
rs.close();
} else {
JOptionPane.showMessageDialog(null, "Record does not found in Employee table");
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}

private void cmdExitActionPerformed(java.awt.event.ActionEvent evt) {


this.setVisible(false);
//new MainUI().setVisible(true);
}

private void cmdUpdateActionPerformed(java.awt.event.ActionEvent evt) {


try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","raj");
Statement stmt = null;
ResultSet rs = null;
String SQL = "SELECT * FROM emp";
stmt = con.createStatement();
rs = stmt.executeQuery(SQL);
int pno = Integer.parseInt(txtNo.getText().trim());
String EName = txtName.getText();
String ECty = txtCity.getText();
String EPh = txtPh.getText();
String PSex="M";
if(jRadioButton1.isSelected())
PSex = "M";
else
PSex="F";
String strSQL = "Update emp set empname ='"+(EName)+"',empcity = '"+(ECty)+"', empphone =
'"+(EPh)+"', empsex = '"+(PSex)+"' where empid = " + (pno);
int rowsEffected = stmt.executeUpdate(strSQL);
if (rowsEffected == 0)
JOptionPane.showMessageDialog(this, "Record does not exists");
else
JOptionPane.showMessageDialog(this,"Record modified");
con.close();
stmt.close();
rs.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}

// Variables declaration - do not modify


private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JButton cmdExit;
private javax.swing.JButton cmdUpdate;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JList jList1;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JRadioButton jRadioButton2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField txtCity;
private javax.swing.JTextField txtName;
private javax.swing.JTextField txtNo;
private javax.swing.JTextField txtPh;
// End of variables declaration
}

EmpNavUI.java

import java.sql.*;
import javax.swing.JOptionPane;

public class EmpNavUI extends javax.swing.JFrame {


/** Creates new form PubNavUI */
public EmpNavUI() {
initComponents();
}
Statement stmt = null;
ResultSet rs = null;
String SQL = "SELECT * FROM emp";
public void disable_textfields() {
txtNo.setEditable(false);
txtName.setEditable(false);
txtAdd.setEditable(false);
txtPh.setEditable(false);

private void cmdFirstActionPerformed(java.awt.event.ActionEvent evt) {


try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","raj");
if (rs.first()) {
String PN = rs.getString("empid");
String PName = rs.getString("empname");
String PAdd = rs.getString("empcity");
String PPh = rs.getString("empphone");
String PSex = rs.getString("empsex");
// Displaying the contents in respective text boxes.
txtNo.setText(PN);
txtName.setText(PName);
txtAdd.setText(PAdd);
txtPh.setText(PPh);
if(PSex.equals("M"))
jRadioButton1.setSelected(true);
else
jRadioButton2.setSelected(true);
cmdFirst.setEnabled(false);
cmdNext.setEnabled(true);
cmdPrev.setEnabled(false);
cmdLast.setEnabled(true);
} else {
cmdFirst.setEnabled(false);
cmdNext.setEnabled(false);
cmdPrev.setEnabled(false);
cmdLast.setEnabled(false);
JOptionPane.showMessageDialog(this, "Rhere is no record in table", "Student",0);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}

private void cmdNextActionPerformed(java.awt.event.ActionEvent evt) {


try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","raj");
if (rs.next()) {
String PN = rs.getString("empid");
String PName = rs.getString("empname");
String PAdd = rs.getString("empcity");
String PPh = rs.getString("empphone");
String PSex = rs.getString("empsex");
// Displaying the contents in respective text boxes.
txtNo.setText(PN);
txtName.setText(PName);
txtAdd.setText(PAdd);
txtPh.setText(PPh);
if(PSex.equals("M"))
jRadioButton1.setSelected(true);
else
jRadioButton2.setSelected(true);
cmdFirst.setEnabled(true);
cmdNext.setEnabled(true);
cmdPrev.setEnabled(true);
cmdLast.setEnabled(true);
} else {
cmdNext.setEnabled(false);
JOptionPane.showMessageDialog(this, "You are at last record position", "Student",0);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}

private void cmdExitActionPerformed(java.awt.event.ActionEvent evt) {


this.setVisible(false);
}

private void cmdPrevActionPerformed(java.awt.event.ActionEvent evt) {


try {

if (rs.previous()) {
String PN = rs.getString("empid");
String PName = rs.getString("empname");
String PAdd = rs.getString("empcity");
String PPh = rs.getString("empphone");
String PSex = rs.getString("empsex");
// Displaying the contents in respective text boxes.
txtNo.setText(PN);
txtName.setText(PName);
txtAdd.setText(PAdd);
txtPh.setText(PPh);
if(PSex.equals("M"))
jRadioButton1.setSelected(true);
else
jRadioButton2.setSelected(true);
cmdFirst.setEnabled(true);
cmdNext.setEnabled(true);
cmdPrev.setEnabled(true);
cmdLast.setEnabled(true);
} else {
cmdPrev.setEnabled(false);
JOptionPane.showMessageDialog(this, "You are at first position", "Student",0);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}

private void cmdLastActionPerformed(java.awt.event.ActionEvent evt) {


try {
if (rs.last()) {
String PN = rs.getString("empid");
String PName = rs.getString("empname");
String PAdd = rs.getString("empcity");
String PPh = rs.getString("empphone");
String PSex = rs.getString("empsex");
// Displaying the contents in respective text boxes.
txtNo.setText(PN);
txtName.setText(PName);
txtAdd.setText(PAdd);
txtPh.setText(PPh);
if(PSex.equals("M"))
jRadioButton1.setSelected(true);
else
jRadioButton2.setSelected(true);
cmdFirst.setEnabled(true);
cmdNext.setEnabled(false);
cmdPrev.setEnabled(true);
cmdLast.setEnabled(false);
} else {
JOptionPane.showMessageDialog(this, "You are already at last record", "Student",0);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}

private void formWindowGainedFocus(java.awt.event.WindowEvent evt) {


disable_textfields();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","raj");
stmt = con.createStatement();
rs = stmt.executeQuery(SQL);
if (rs.first())
{
String PN = rs.getString("empid");
String PName = rs.getString("empname");
String PAdd = rs.getString("empcity");
String PPh = rs.getString("empphone");
String PSex = rs.getString("empsex");
// Displaying the contents in respective text boxes.
txtNo.setText(PN);
txtName.setText(PName);
txtAdd.setText(PAdd);
txtPh.setText(PPh);
if(PSex.equals("M"))
jRadioButton1.setSelected(true);
else
jRadioButton2.setSelected(true);
cmdFirst.setEnabled(false);
cmdNext.setEnabled(true);
cmdPrev.setEnabled(false);
cmdLast.setEnabled(true);
}
else
{
cmdFirst.setEnabled(false);
cmdNext.setEnabled(false);
cmdPrev.setEnabled(false);
cmdLast.setEnabled(false);
JOptionPane.showMessageDialog(this, "There is no record in table", "Student",0);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new EmpNavUI().setVisible(true);
}
});
}

// Variables declaration - do not modify


private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JButton cmdExit;
private javax.swing.JButton cmdFirst;
private javax.swing.JButton cmdLast;
private javax.swing.JButton cmdNext;
private javax.swing.JButton cmdPrev;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JRadioButton jRadioButton2;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JSeparator jSeparator2;
private javax.swing.JTextField txtAdd;
private javax.swing.JTextField txtName;
private javax.swing.JTextField txtNo;
private javax.swing.JTextField txtPh;
// End of variables declaration

EmpSearchUI.java
import java.sql.*;
import javax.swing.JOptionPane;
public class EmpSearchUI extends javax.swing.JFrame {
public EmpSearchUI() {
initComponents();
}

Statement stmt = null;


ResultSet rs = null;
private void cmdDeleteActionPerformed(java.awt.event.ActionEvent evt) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost/test","root","raj");
String PN =TextSearch.getText().trim();
String SQL = "SELECT * FROM emp WHERE empname like '%"+(PN)+"%'";
stmt = con.createStatement();
rs = stmt.executeQuery(SQL);
if (rs.next()!= rs.isAfterLast()) {
String PID = rs.getString("empID");
String PName = rs.getString("empname");
String PAdd = rs.getString("empcity");
String PPh = rs.getString("empphone");
String PSex = rs.getString("empsex");
// Displaying the contents in respective text boxes.
txtPno.setText(PID);
txtPName.setText(PName);
txtPAdd.setText(PAdd);
txtPh1.setText(PPh);
if (PSex.equals("M"))
jRadioButton1.setSelected(true);
else
jRadioButton2.setSelected(true);
}
else
JOptionPane.showMessageDialog(null,"Search string "+PN+"..not found");
}
catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage()+"Unable to find Record");
}
}
private void cmdExitActionPerformed(java.awt.event.ActionEvent evt) {
this.setVisible(false);
//new MainUI().setVisible(true);
}
// Variables declaration - do not modify
private javax.swing.JTextField TextSearch;
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JButton cmdDelete;
private javax.swing.JButton cmdExit;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JRadioButton jRadioButton2;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField txtPAdd;
private javax.swing.JTextField txtPName;
private javax.swing.JTextField txtPh1;
private javax.swing.JTextField txtPno;
// End of variables declaration
}
EmpTable.java (Display all records of Emp Table)

import java.sql.*;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
public class EmpTable extends javax.swing.JFrame {
/** Creates new form PubDelUI */
public EmpTable() {
initComponents();
}
private void cmdExitActionPerformed(java.awt.event.ActionEvent evt) {
this.setVisible(false);
//new MainUI().setVisible(true);
}

private void formWindowGainedFocus(java.awt.event.WindowEvent evt) {


DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
// Clear the existing table
int rows = model.getRowCount();
if (rows > 0) {
for (int i = 0; i < rows; i++) {
model.removeRow(0);
}
}
// SQL Query
String query = "SELECT * FROM emp";
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost/test","root","raj");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
// Iterate through the result and display on screen
while (rs.next()) {
String Mno = rs.getString("empId");
String MName = rs.getString("empname");
String MAdd = rs.getString("empcity");
String MPh1 = rs.getString("empphone");
String MDate = rs.getString("empsex");
model.addRow(new Object[] {Mno, MName, MAdd, MPh1, MDate});
}
}
catch (Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
// Variables declaration - do not modify
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JButton cmdExit;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
private javax.swing.JTextField jTextField2;
// End of variables declaration

You might also like