-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrders.java
More file actions
104 lines (102 loc) · 3.84 KB
/
Orders.java
File metadata and controls
104 lines (102 loc) · 3.84 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 Orders 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 ORDERS VALUES (?, ?, ?)");
System.out.println("Enter hospital id: ");
String hid = obj.next();
System.out.println("Enter blood bank number: ");
String bno = obj.next();
System.out.println("Enter hphone: ");
String hphone = obj.next();
preparedStatement.setString(1, hid);
preparedStatement.setString(2, bno);
preparedStatement.setString(3, hphone);
preparedStatement.executeUpdate();
} catch (Exception e) {
throw e;
} finally {
close();
}
}
public void delete(String hid) throws Exception {
try {
Class.forName("org.postgresql.Driver");
connect = DriverManager.getConnection("jdbc:postgresql://localhost:5432/BLOODBANKMANAGEMENTSYSTEM", "postgres", "amma1234");
preparedStatement = connect.prepareStatement("DELETE FROM ORDERS WHERE HID = ?");
preparedStatement.setString(1, hid);
preparedStatement.executeUpdate();
} catch (Exception e) {
throw e;
} finally {
close();
}
}
public void edit(String hid) throws Exception {
try {
Class.forName("org.postgresql.Driver");
connect = DriverManager.getConnection("jdbc:postgresql://localhost:5432/BLOODBANKMANAGEMENTSYSTEM", "postgres", "amma1234");
preparedStatement = connect.prepareStatement("UPDATE ORDERS SET BNO = ?, HPHONE = ? "
+ "WHERE HID = ?");
preparedStatement.setString(3, hid);
System.out.println("Enter blood bank number: ");
String bno = obj.next();
System.out.println("Enter hphone: ");
String hphone = obj.next();
preparedStatement.setString(1, bno);
preparedStatement.setString(2, hphone);
preparedStatement.executeUpdate();
} catch (Exception e) {
throw e;
} finally {
close();
}
}
public void search(String hid) throws Exception {
try {
Class.forName("org.postgresql.Driver");
connect = DriverManager.getConnection("jdbc:postgresql://localhost:5432/BLOODBANKMANAGEMENTSYSTEM", "postgres", "amma1234");
preparedStatement = connect.prepareStatement("SELECT * FROM ORDERS WHERE HID = ?");
preparedStatement.setString(1, hid);
resultSet = preparedStatement.executeQuery();
writeResultSet(resultSet);
} catch (Exception e) {
throw e;
} finally {
close();
}
}
private void writeResultSet(ResultSet resultSet) throws SQLException {
while (resultSet.next()) {
String hid = resultSet.getString("HID");
String bno = resultSet.getString("BNO");
String phone = resultSet.getString("HPHONE");
System.out.println("HOSPITAL ID: " + hid);
System.out.println("BLOODBANK NUMBER: " + bno);
System.out.println("HOSPITAL PHONE NO: " + phone);
}
}
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();
}
}
}