-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeleteDonor.java
136 lines (111 loc) · 4.89 KB
/
DeleteDonor.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
package bootathonfinal;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.sql.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class DeleteDonor{
static String Data;
static JFrame f;
DeleteDonor()
{
f=ObjectFile.getFrame("Delete Organ");
DeleteDonor.deleteorgan();
JOptionPane.showMessageDialog(f,"Click the Donor ID to Delete");
}
static void deleteorgan()
{
f.setLayout(new BorderLayout());
String[] columnNames = {"Hospital_id","Hospital_name","Donor_id","Hospital_phonenumber", "Organ","Blood_group"};
DefaultTableModel model = new DefaultTableModel();
JTable table = new JTable(model);
model.setColumnIdentifiers(columnNames);
table.setModel(model);
JScrollPane scroll = new JScrollPane(table);
try {
Connection con =ObjectFile.getjdbc();
Statement st = con.createStatement();
String sql="SELECT * FROM Donor natural join Hospital WHERE hospital_id='"+FirstPage.hid+"'" ;
ResultSet rs=st.executeQuery(sql);
int i=0;
while(rs.next())
{
String Hospital_id = rs.getString("Hospital_id");
String name = rs.getString("Hospital_name");
String Hospitalno=rs.getString("Hospital_phonenumber");
int did=rs.getInt("Donor_id");
String organs = rs.getString("Organ");
String blood=rs.getString("Blood_group");
System.out.println(name+" "+organs+" "+blood);
model.addRow(new Object[]{Hospital_id,name,did,Hospitalno, organs,blood});
i++;
}
if (i < 1) {
JOptionPane.showMessageDialog(f, "No Record Found", "Error", JOptionPane.ERROR_MESSAGE);
}
else {
System.out.println(i + " Records Found");
}
con.close();
}catch (Exception ex) {
JOptionPane.showMessageDialog(f, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
table.setCellSelectionEnabled(true);
ListSelectionModel select= table.getSelectionModel();
select.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
select.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
int[] row = table.getSelectedRows();
int[] columns = table.getSelectedColumns();
for (int i = 0; i < row.length; i++) {
for (int j = 0; j < columns.length; j++) {
Data =String.valueOf(table.getValueAt(row[i], columns[j]));
} }
try{
Connection con1=ObjectFile.getjdbc();
Statement st = con1.createStatement();
String name=Data;
String query="delete from Donor where Donor_id='"+name+"'";
int result = st.executeUpdate(query);
con1.close();
JOptionPane.showMessageDialog(f,"Successfully deleted");
f.dispose();
System.out.println("Success!");
}
catch(Exception a)
{
System.out.println(" Exception -->" + a);
}
System.out.println("Table element selected is: " + Data);
}
});
table.setRowHeight(30);
JTableHeader header = table.getTableHeader();
header.setBackground(Color.decode("#5424DA"));
header.setForeground(Color.white);
table.setPreferredScrollableViewportSize(new Dimension(450,63));
table.setFillsViewportHeight(true);
table.setFont(new Font("Merriweather",Font.PLAIN,20));
table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer()
{
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
final Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
c.setBackground(row % 2 != 0 ? Color.decode("#5424DA") : Color.WHITE);
c.setForeground(row % 2 == 0 ? Color.BLACK : Color.WHITE);
return c;
}
});
header.setFont(new Font("Merriweather",Font.PLAIN,20));
f.add(scroll);
}
public static void main(String a[])
{
}
}