-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBloodBank.java
More file actions
112 lines (110 loc) · 4.24 KB
/
BloodBank.java
File metadata and controls
112 lines (110 loc) · 4.24 KB
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
package bb_management;
import java.sql.*;
import java.util.*;
public class BloodBank implements User {
private Connection connect = null;
private Statement statement = null;
private PreparedStatement preparedStatement = null;
private ResultSet resultSet = null;
Scanner obj = new Scanner(System.in);
public void add() throws Exception {
try {
Class.forName("org.postgresql.Driver");
connect = DriverManager.getConnection("jdbc:postgresql://localhost:5432/BLOODBANKMANAGEMENTSYSTEM", "postgres", "amma1234");
preparedStatement = connect.prepareStatement("INSERT INTO BLOODBANK VALUES (?, ?, ?, ?)");
System.out.println("Enter bloodbank number: ");
String bno = obj.next();
System.out.println("Enter bloodtype: ");
String bloodtype = obj.next();
System.out.println("Enter orders: ");
int orders = obj.nextInt();
System.out.println("Enter donor issues: ");
int issues = obj.nextInt();
preparedStatement.setString(1, bno);
preparedStatement.setString(2, bloodtype);
preparedStatement.setInt(3, orders);
preparedStatement.setInt(4, issues);
preparedStatement.executeUpdate();
} catch (Exception e) {
throw e;
} finally {
close();
}
}
public void delete(String bno) throws Exception {
try {
Class.forName("org.postgresql.Driver");
connect = DriverManager.getConnection("jdbc:postgresql://localhost:5432/BLOODBANKMANAGEMENTSYSTEM", "postgres", "amma1234");
preparedStatement = connect.prepareStatement("DELETE FROM BLOODBANK WHERE BNO = ?");
preparedStatement.setString(1, bno);
preparedStatement.executeUpdate();
} catch (Exception e) {
throw e;
} finally {
close();
}
}
public void edit(String bno) throws Exception {
try {
Class.forName("org.postgresql.Driver");
connect = DriverManager.getConnection("jdbc:postgresql://localhost:5432/BLOODBANKMANAGEMENTSYSTEM", "postgres", "amma1234");
preparedStatement = connect.prepareStatement("UPDATE DONOR SET BLOODTYPE = ?, ORDERS = ?, "
+ "ISSUES = ? WHERE BNO = ?");
preparedStatement.setString(4, bno);
System.out.println("Enter bloodtype: ");
String bloodtype = obj.next();
System.out.println("Enter orders: ");
int orders = obj.nextInt();
System.out.println("Enter donor issues: ");
int issues = obj.nextInt();
preparedStatement.setString(1, bloodtype);
preparedStatement.setInt(2, orders);
preparedStatement.setInt(3, issues);
preparedStatement.executeUpdate();
} catch (Exception e) {
throw e;
} finally {
close();
}
}
public void search(String bno) throws Exception {
try {
Class.forName("org.postgresql.Driver");
connect = DriverManager.getConnection("jdbc:postgresql://localhost:5432/BLOODBANKMANAGEMENTSYSTEM", "postgres", "amma1234");
preparedStatement = connect.prepareStatement("SELECT * FROM BLOODBANK WHERE BNO = ?");
preparedStatement.setString(1, bno);
resultSet = preparedStatement.executeQuery();
writeResultSet(resultSet);
} catch (Exception e) {
throw e;
} finally {
close();
}
}
private void writeResultSet(ResultSet resultSet) throws SQLException {
while (resultSet.next()) {
String bno = resultSet.getString("BNO");
String bloodtype = resultSet.getString("BLOODTYPE");
int orders = resultSet.getInt("ORDERS");
int issues = resultSet.getInt("ISSUES");
System.out.println("BLOODBANK NO: " + bno);
System.out.println("BLOODTYPE: " + bloodtype);
System.out.println("ORDERS: " + orders);
System.out.println("ISSUES: " + issues);
}
}
private void close() throws SQLException {
if (resultSet != null) {
resultSet.close();
}
if (preparedStatement != null) {
preparedStatement.close();
}
if (statement != null) {
statement.close();
}
if (connect != null) {
connect.close();
}
}
}