
Write a java swing program to delete a selected record from a table.

import java.awt.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
class DeleteStudent {
public static void main(String[] args) {
JFrame f = new JFrame();
JLabel label = new JLabel("Enter is to delete the record ");
final JTextField text = new JTextField(20);
JButton b = new JButton("Search");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int id = Integer.parseInt(text.getText());
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/roseindia", "root","root");
Statement st = con.createStatement();
int i=st.executeUpdate("Delete from student where id="+id+"");
JOptionPane.showMessageDialog(null, "Record is deleted successfully!");
} catch (Exception ex) {
}
}
});
Panel p = new Panel(new GridLayout(2, 2));
p.add(label);
p.add(text);
p.add(b);
f.add(p);
f.setVisible(true);
f.pack();
}
}

import java.awt.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
class DeleteStudent {
public static void main(String[] args) {
JFrame f = new JFrame();
JLabel label = new JLabel("Enter id to delete the record ");
final JTextField text = new JTextField(20);
JButton b = new JButton("Delete");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int id = Integer.parseInt(text.getText());
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/roseindia", "root","root");
Statement st = con.createStatement();
int i=st.executeUpdate("Delete from student where id="+id+"");
JOptionPane.showMessageDialog(null, "Record is deleted successfully!");
} catch (Exception ex) {
}
}
});
Panel p = new Panel(new GridLayout(2, 2));
p.add(label);
p.add(text);
p.add(b);
f.add(p);
f.setVisible(true);
f.pack();
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.