-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStores.java
More file actions
96 lines (93 loc) · 3.48 KB
/
Stores.java
File metadata and controls
96 lines (93 loc) · 3.48 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
package bb_management;
import java.sql.*;
import java.util.*;
public class Stores 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 STORES VALUES (?, ?)");
System.out.println("Enter blood bank no: ");
String bno = obj.next();
System.out.println("Enter bloodtype: ");
String bloodtype = obj.next();
preparedStatement.setString(1, bno);
preparedStatement.setString(2, bloodtype);
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 STORES 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 STORES SET BLOODTYPE = ? WHERE BNO = ?");
preparedStatement.setString(2, bno);
System.out.println("Enter bloodtype: ");
String bloodtype = obj.next();
preparedStatement.setString(1, bloodtype);
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 STORES 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");
System.out.println("BLOODBANK NUMBER: " + bno);
System.out.println("BLOODTYPE: " + bloodtype);
}
}
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();
}
}
}