-
Notifications
You must be signed in to change notification settings - Fork 0
/
Delete.java
35 lines (29 loc) · 1.22 KB
/
Delete.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.JOptionPane;
public class Delete {
static void delete(String tableName){
String text = "Type your %s's number";
String numberString = JOptionPane.showInputDialog(null, String.format(text, tableName));
int number = Integer.parseInt(numberString);
try {
String sql = "DELETE FROM %s WHERE id = ?";
String formatedSql = String.format(sql, tableName);
//Create a confirm deletion with your account's password
PreparedStatement stt = Connect.connection.prepareStatement(formatedSql);
stt.setInt(1, number);
int response = stt.executeUpdate();
if(response > 0){
String dialog = "%s %d successfully deleted!";
JOptionPane.showMessageDialog(null, String.format(dialog, tableName, number));
}
} catch (SQLException e){
if(e.getErrorCode() == 1451){
JOptionPane.showMessageDialog(null, "You must delete this customer's accounts first");
} else {
e.printStackTrace();
}
}
Main.landingPage();
}
}