forked from rishikavk-08/Library_Management_System_DBMS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeleteMemberController.java
157 lines (131 loc) · 5.72 KB
/
DeleteMemberController.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package sample;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
import java.net.URL;
import java.sql.*;
import java.util.ResourceBundle;
public class DeleteMemberController implements Initializable {
@FXML
private Button Back;
//fxids
@FXML
private Button Delete;
@FXML
private TextField dmemberid;
@FXML
private TextField dfirstname;
@FXML
private TextField dlastname;
@FXML
private TableView<TableviewMember> table;
@FXML
private TableColumn<TableviewMember, Integer> id;
@FXML
private TableColumn<TableviewMember, String> first;
@FXML
private TableColumn<TableviewMember, String> last;
@FXML
private TableColumn<TableviewMember, String> address;
@FXML
private TableColumn<TableviewMember, Integer> phone;
@FXML
private TableColumn<TableviewMember, String> email;
@FXML
private TableColumn<TableviewMember, String> username;
@FXML
private TableColumn<TableviewMember, String> password;
ObservableList<TableviewMember> list = FXCollections.observableArrayList();
public void initialize(URL url, ResourceBundle resources) {
table.getItems().clear();
try {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/library", "root", "mypass");
ResultSet rs = con.createStatement().executeQuery("Select * from member");
while (rs.next()) {
list.add(new TableviewMember(rs.getInt("member_ID"),rs.getString("firstname"),rs.getString("lastname"),rs.getString("address"),rs.getString("phone_number"),rs.getString("emailID"),rs.getString("username"),rs.getString("password")));
}
try { //set properties to cells of each column of the table
id.setCellValueFactory(new PropertyValueFactory<TableviewMember,Integer>("id"));
first.setCellValueFactory(new PropertyValueFactory<TableviewMember,String>("first"));
last.setCellValueFactory(new PropertyValueFactory<TableviewMember,String>("last"));
address.setCellValueFactory(new PropertyValueFactory<TableviewMember,String>("address"));
phone.setCellValueFactory(new PropertyValueFactory<TableviewMember,Integer>("phone"));
email.setCellValueFactory(new PropertyValueFactory<TableviewMember,String>("email"));
username.setCellValueFactory(new PropertyValueFactory<TableviewMember,String>("username"));
password.setCellValueFactory(new PropertyValueFactory<TableviewMember,String>("password"));
table.setItems(list);
rs.close();
} catch (Exception e) { //Exception is handled
e.printStackTrace();
}
} catch (SQLException throwables) { //SQLException thrown by next()
throwables.printStackTrace();
}
}
public void Backclicked()
{
Stage stage = (Stage) Back.getScene().getWindow();
stage.close();
try
{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
Stage loginStage = new Stage();
loginStage.setTitle("Librarian Page");
loginStage.setScene(new Scene(root, 703, 547));
loginStage.show();
}
catch(Exception e)
{
e.printStackTrace();
e.getCause();
}
}
Connection myCon;
ResultSet reg1;
public void Deletememberclicked(ActionEvent actionEvent) {
if(!dmemberid.getText().isBlank() && !dfirstname.getText().isBlank() && !dlastname.getText().isBlank() ){
String id= dmemberid.getText();
String first=dfirstname.getText();
String last=dlastname.getText();
try {
myCon = DriverManager.getConnection("jdbc:mysql://localhost:3306/library", "root", "mypass");
String reg1 = "delete from member where member_ID="+"?" ;
PreparedStatement preparedStmt = myCon.prepareStatement(reg1);
preparedStmt.setString (1, id);
int res=preparedStmt.executeUpdate();
if(res==1){
infoBox(" Member deleted successfully",null,"Success!!");
//initialize();//refresh here!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
}
else{
infoBox("issue in deleting member ",null,"ERROR");
}
}
catch(Exception e) {
e.printStackTrace();
}
}
else{
infoBox("please enter all details",null,"ERROR!!");
}
}
public static void infoBox(String infoMessage, String headerText, String title) {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setContentText(infoMessage);
alert.setTitle(title);
alert.setHeaderText(headerText);
ButtonType buttonTypeOk = new ButtonType("Ok");//button Ok is created
ButtonType buttonTypeCancel = new ButtonType("Cancel", ButtonBar.ButtonData.CANCEL_CLOSE);//button Cancel is created
alert.getButtonTypes().setAll(buttonTypeOk, buttonTypeCancel);
alert.getDialogPane().lookupButton(buttonTypeCancel).setVisible(false);
alert.showAndWait();
}
}