0% found this document useful (0 votes)
10 views21 pages

Document

The document is a Java Swing application that provides a graphical interface for managing employee records in a database. It includes functionalities to insert, update, delete, and navigate through employee data using buttons and text fields. The application establishes a connection to a MySQL database and performs CRUD operations on an 'employee' table.

Uploaded by

Tanisha Waichal
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)
10 views21 pages

Document

The document is a Java Swing application that provides a graphical interface for managing employee records in a database. It includes functionalities to insert, update, delete, and navigate through employee data using buttons and text fields. The application establishes a connection to a MySQL database and performs CRUD operations on an 'employee' table.

Uploaded by

Tanisha Waichal
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/ 21

package pack2;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import java.awt.*;
import java.awt.event.*;
import java.sql.*;

public class Swingtable4 extends JFrame {


private JButton btnNext,
btnPrevious,btnfirst,btnlast,btnins,btnup,btnd
el,btnclear;
private JLabel l1,l2,l3,l4,l5,l6,l7;
JTextField tf1,tf2,tf3,tf4,tf5,tf6;
JTextField id, name,hire,sal,pos,no ;
private DefaultTableModel model;
private Connection connection;
private Statement statement;
private ResultSet resultSet;

public Swingtable4() {

l7 = new JLabel("Displaying Emp Data:");


l7.setForeground(Color.red);
l7.setFont(new Font("Serif", Font.BOLD,
20));
btnNext = new JButton("Next");
btnPrevious = new JButton("Previous");
btnfirst = new JButton("Go to First");
btnlast = new JButton("Go to Last");
btnins= new JButton("Insert record");
btnup= new JButton("Update record");
btndel= new JButton("Delete record");
btnclear= new JButton("Clear All");
l1 = new JLabel("Emp No : ");
l2 = new JLabel("Employee Name : ");
l3 = new JLabel("Hiredate : ");
l4 = new JLabel("Salary : ");
l5 = new JLabel("Position : ");
l6 = new JLabel("Dept No: ");

setVisible(true);
setLayout(null);
l1.setBounds(20, 110, 200, 20);
l2.setBounds(20, 140, 200, 20);
l3.setBounds(20, 170, 200, 20);
l4.setBounds(20, 200, 200, 20);
l5.setBounds(20, 230, 200, 20);
l6.setBounds(20, 260, 200, 20);
l7.setBounds(100, 70, 200, 20);

btnfirst.setBounds(20, 300, 100, 20);


btnPrevious.setBounds(130, 300, 100,
20);
btnNext.setBounds(240, 300, 100, 20);
btnlast.setBounds(350, 300, 100, 20);
btnins.setBounds(20, 340, 115, 20);
btnup.setBounds(145, 340, 125, 20);
btndel.setBounds(280, 340, 125, 20);
btnclear.setBounds(20,380,125,20);
tf1 = new JTextField();
tf2 = new JTextField();
tf3 = new JTextField();
tf4 = new JTextField();
tf5 = new JTextField();
tf6 = new JTextField();

tf1.setBounds(80, 110, 200, 20);


tf2.setBounds(120, 140, 200, 20);
tf3.setBounds(80, 170, 200, 20);
tf4.setBounds(80, 200, 200, 20);
tf5.setBounds(80, 230, 200, 20);
tf6.setBounds(80, 260, 200, 20);

add(l7);
add(l1);
add(tf1);
add(l2);
add(tf2);
add(l3);
add(tf3);
add(l4);
add(tf4);
add(l5);
add(tf5);
add(l6);
add(tf6);

add(btnfirst);
add(btnPrevious);
add(btnNext);
add(btnlast);
add(btnins);
add(btnup);
add(btndel);
add(btnclear);
// Action Listeners for buttons
btnins.addActionListener(new
ActionListener() {
public void
actionPerformed(ActionEvent e) {
insertEmployee();
}
});
btnclear.addActionListener(new
ActionListener() {
public void
actionPerformed(ActionEvent e) {
clearFields();
}
});

btndel.addActionListener(new
ActionListener() {
public void
actionPerformed(ActionEvent e) {
deleteUser();
}
});

btnup.addActionListener(new
ActionListener() {
public void
actionPerformed(ActionEvent e)
{
updateRecord();
}
});

btnNext.addActionListener(new
ActionListener() {
public void
actionPerformed(ActionEvent e) {
showNextRecord();
}
});
btnPrevious.addActionListener(new
ActionListener() {
public void
actionPerformed(ActionEvent e) {
showPreviousRecord();
}
});

btnfirst.addActionListener(new
ActionListener() {
public void
actionPerformed(ActionEvent e) {
loadFirstRecord();
}
});
btnlast.addActionListener(new
ActionListener() {
public void
actionPerformed(ActionEvent e) {
loadlastRecord();
}
});

initializeDbConnection();

loadFirstRecord();

setTitle("JDBC Navigation Example");


setSize(500, 500);

setDefaultCloseOperation(JFrame.EXIT_ON_CL
OSE);
setVisible(true);
}

private void initializeDbConnection() {


try {

Class.forName("com.mysql.cj.jdbc.Driver");

connection =
DriverManager.getConnection("jdbc:mysql://lo
calhost:3306/data1", "root", "System@123");

statement =
connection.createStatement(ResultSet.TYPE_S
CROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);

resultSet =
statement.executeQuery("SELECT * FROM
employee");
} catch (Exception e) {
e.printStackTrace();
}
}

private void insertEmployee() {


String eid = tf1.getText();
String empname = tf2.getText();
String hired = tf3.getText();
String salar = tf4.getText();
String posi = tf5.getText();
String num = tf6.getText();

if (eid.isEmpty() || empname.isEmpty() ||
hired.isEmpty() || salar.isEmpty() ||
posi.isEmpty() || num.isEmpty()) {
JOptionPane.showMessageDialog(this,
"Please fill all fields.");
return;
}
try {
String sql = "INSERT INTO employee
(empno, ename,
hiredate,salary,position,deptno) VALUES
(?, ?, ?, ?,?,?)";
PreparedStatement stmt =
connection.prepareStatement(sql);
stmt.setInt(1,
Integer.parseInt(tf1.getText()));
stmt.setString(2, tf2.getText());
stmt.setString(3, tf3.getText());
stmt.setInt(4,
Integer.parseInt(tf4.getText()));
stmt.setString(5, tf5.getText());
stmt.setInt(6,
Integer.parseInt(tf6.getText()));
stmt.executeUpdate();
JOptionPane.showMessageDialog(this,
"Record inserted successfully!");
// Add to the table view
//model.addRow(new Object[]{tf1, tf2,
tf3,tf4, tf5,tf6});
clearFields();
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this,
"Error inserting data into database.");
}
}

private void updateRecord() {


String eid = tf1.getText();
String empname = tf2.getText();
String hired = tf3.getText();
String salar = tf4.getText();
String posi = tf5.getText();
String num = tf6.getText();
if (eid.isEmpty() || empname.isEmpty() ||
hired.isEmpty() || salar.isEmpty() ||
posi.isEmpty() || num.isEmpty()) {
JOptionPane.showMessageDialog(this,
"Please fill all fields.");
return;
}

try {
String query = "UPDATE employee SET
ename = ?, hiredate = ?, salary = ?, position
= ?, deptno = ? WHERE empno = ?";

PreparedStatement
preparedStatement =
connection.prepareStatement(query);

preparedStatement.setString(1,
empname);
preparedStatement.setString(2, hired);
preparedStatement.setInt(3,
Integer.parseInt(salar));
preparedStatement.setString(4, posi);
preparedStatement.setInt(5,
Integer.parseInt(num));
preparedStatement.setInt(6,
Integer.parseInt(eid));

int rowsAffected =
preparedStatement.executeUpdate();

if (rowsAffected > 0) {

JOptionPane.showMessageDialog(this, "Record
updated successfully!");
} else {

JOptionPane.showMessageDialog(this, "No
record found to update!");
}
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this,
"Error updating the record: " +
e.getMessage());
}
}
private void deleteUser() {

try {

String id = tf1.getText().toString();
String query = "DELETE FROM
employee WHERE empno=?";
PreparedStatement
preparedStatement =
connection.prepareStatement(query);
preparedStatement.setInt(1,
Integer.parseInt(id));
preparedStatement.executeUpdate();
clearFields();
JOptionPane.showMessageDialog(this,
"Employee deleted successfully!");
} catch (SQLException e) {
e.printStackTrace();
}
}

private void loadFirstRecord() {


try {
if (resultSet.first()) {
displayRecord();
}
} catch (SQLException e) {
e.printStackTrace();
}
}

private void loadlastRecord() {


try {
if (resultSet.last()) {
displayRecord();
}
} catch (SQLException e) {
e.printStackTrace();
}
}

private void showNextRecord() {


try {
if (resultSet.next()) {
displayRecord();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
private void showPreviousRecord() {
try {
if (resultSet.previous()) {
displayRecord();
}
} catch (SQLException e) {
e.printStackTrace();
}
}

private void displayRecord() {


try {
int e1 =resultSet.getInt("empno");
String e2 =
resultSet.getString("ename");
String e3 =
resultSet.getString("hiredate");
int e4 = resultSet.getInt("salary");
String e5 =
resultSet.getString("position");
int e6 = resultSet.getInt("deptno");
tf1.setText(" "+ e1);
tf2.setText(" " +e2);
tf3.setText(" " +e3);
tf4.setText(" " +e4);
tf5.setText(" " +e5);
tf6.setText(" " +e6);
} catch (SQLException e) {
e.printStackTrace();
}
}

private void clearFields() {


tf1.setText("");
tf2.setText("");
tf3.setText("");
tf4.setText("");
tf5.setText("");
tf6.setText("");
}

public static void main(String[] args) {


new Swingtable4();
}
}

You might also like