Document (1)
Document (1)
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public Swingtable4() {
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);
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();
setDefaultCloseOperation(JFrame.EXIT_ON_CL
OSE);
setVisible(true);
}
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();
}
}
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.");
}
}
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();
}
}