-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlood.java
More file actions
104 lines (102 loc) · 3.85 KB
/
Blood.java
File metadata and controls
104 lines (102 loc) · 3.85 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
package bb_management;
import java.sql.*;
import java.util.*;
public class Blood 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 BLOOD VALUES (?, ?, ?)");
System.out.println("Enter bloodtype: ");
String bloodtype = obj.next();
System.out.println("Enter code: ");
String code = obj.next();
System.out.println("Enter cost: ");
int bcost = obj.nextInt();
preparedStatement.setString(1, bloodtype);
preparedStatement.setString(2, code);
preparedStatement.setInt(3, bcost);
preparedStatement.executeUpdate();
} catch (Exception e) {
throw e;
} finally {
close();
}
}
public void delete(String bloodtype) throws Exception {
try {
Class.forName("org.postgresql.Driver");
connect = DriverManager.getConnection("jdbc:postgresql://localhost:5432/BLOODBANKMANAGEMENTSYSTEM", "postgres", "amma1234");
preparedStatement = connect.prepareStatement("DELETE FROM BLOOD WHERE BLOODTYPE = ?");
preparedStatement.setString(1, bloodtype);
preparedStatement.executeUpdate();
} catch (Exception e) {
throw e;
} finally {
close();
}
}
public void edit(String bloodtype) throws Exception {
try {
Class.forName("org.postgresql.Driver");
connect = DriverManager.getConnection("jdbc:postgresql://localhost:5432/BLOODBANKMANAGEMENTSYSTEM", "postgres", "amma1234");
preparedStatement = connect.prepareStatement("UPDATE BLOOD SET CODE = ?, BCOST = ? "
+ "WHERE BLOODTYPE = ?");
System.out.println("Enter code: ");
String code = obj.next();
System.out.println("Enter cost: ");
int bcost = obj.nextInt();
preparedStatement.setString(3, bloodtype);
preparedStatement.setString(1, code);
preparedStatement.setInt(2, bcost);
preparedStatement.executeUpdate();
} catch (Exception e) {
throw e;
} finally {
close();
}
}
public void search(String bloodtype) throws Exception {
try {
Class.forName("org.postgresql.Driver");
connect = DriverManager.getConnection("jdbc:postgresql://localhost:5432/BLOODBANKMANAGEMENTSYSTEM", "postgres", "amma1234");
preparedStatement = connect.prepareStatement("SELECT * FROM BLOOD WHERE BLOODTYPE = ?");
preparedStatement.setString(1, bloodtype);
resultSet = preparedStatement.executeQuery();
writeResultSet(resultSet);
} catch (Exception e) {
throw e;
} finally {
close();
}
}
private void writeResultSet(ResultSet resultSet) throws SQLException {
while (resultSet.next()) {
String bloodtype = resultSet.getString("BLOODTYPE");
String code = resultSet.getString("CODE");
int cost = resultSet.getInt("BCOST");
System.out.println("BLOODTYPE: " + bloodtype);
System.out.println("CODE: " + code);
System.out.println("COST: " + cost);
}
}
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();
}
}
}