forked from nickrgarner/WolfWR-DB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStoreSQL.java
155 lines (138 loc) · 4.42 KB
/
StoreSQL.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import java.sql.*;
import java.text.ParseException;
import java.sql.SQLException;
public class StoreSQL {
/** This connects to the database by calling the login file */
static Connection connection = Login.connection;
static Statement statement = Login.statement;
static ResultSet result = Login.result;
/**
* Queries Store relation for all tuples and attributes
* @return ResultSet containing all tuples
* @throws ClassNotFoundException
* @throws SQLException
* @throws ParseException
*/
public static ResultSet viewAllStores() throws ClassNotFoundException, SQLException, ParseException
{
ResultSet returnSet = null;
PreparedStatement ps = null;
try {
ps = connection.prepareStatement("SELECT * FROM Store;");
returnSet = ps.executeQuery();
ps.close();
} catch (SQLException e) {
System.out.println("SQL Exception: " + e.getStackTrace());
return null;
}
return returnSet;
}
/**
* Queries Store relation for tuple matching the given storeID
* @param storeID ID of tuple to query for
* @return ResultSet containing tuple matching given storeID or null
* @throws ClassNotFoundException
* @throws SQLException
* @throws ParseException
*/
public static ResultSet viewStore(int storeID) throws ClassNotFoundException, SQLException, ParseException
{
ResultSet returnSet = null;
PreparedStatement ps = null;
try {
ps = connection.prepareStatement("SELECT * FROM Store WHERE storeID=?;");
ps.setInt(1, storeID);
returnSet = ps.executeQuery();
ps.close();
} catch (SQLException e) {
System.out.println("SQL Exception: " + e.getStackTrace());
return null;
}
return returnSet;
}
/**
* Adds store with the given attribute values
* @param storeID ID of store to edit
* @param managerID value to store
* @param address value to store
* @param phone value to store
* @throws ParseException
* @throws SQLException
*/
public static void addStore(int storeID, int managerID, String address, String phone) throws ParseException, SQLException
{
PreparedStatement ps = null;
int id = 0;
try {
ps = connection.prepareStatement("INSERT INTO Store VALUES (?,?,?,?);");
ps.setInt(1, storeID);
ps.setInt(2, managerID);
ps.setString(3, address);
ps.setString(4, phone);
id = ps.executeUpdate();
ps.close();
System.out.println(id);
if (id > 0) {
System.out.println("Store added successfully.");
} else {
System.out.println("Store not added.");
}
} catch (SQLException e) {
System.out.println("SQL Exception: " + e.getStackTrace());
connection.rollback();
}
}
/**
* Changes attribute values to given values of store that matches given storeID
* @param storeID ID of store to edit
* @param managerID value to store
* @param address value to store
* @param phone value to store
* @throws ParseException
* @throws SQLException
*/
public static void editStore(int storeID, int managerID, String address, String phone) throws ParseException, SQLException
{
PreparedStatement ps = null;
int id = 0;
try {
ps = connection.prepareStatement("UPDATE Store SET managerID=?, address=?, phone=? WHERE storeID=?;");
ps.setInt(1, managerID);
ps.setString(2, address);
ps.setString(3, phone);
ps.setInt(4, storeID);
id = ps.executeUpdate();
ps.close();
System.out.println(id);
if (id > 0) {
System.out.println("Store added successfully.");
} else {
System.out.println("Store not added.");
}
} catch (SQLException e) {
System.out.println("SQL Exception: " + e.getStackTrace());
connection.rollback();
}
}
/**
* Deletes store from database that matches supplied storeID
* @param storeID ID of store member to delete
* @throws SQLException
*/
public static void deleteStore(int storeID) throws SQLException
{
try {
PreparedStatement ps = connection.prepareStatement("DELETE FROM Store WHERE storeID = ?;");
ps.setInt(1, storeID);
int id = ps.executeUpdate();
System.out.println(id);
if (id > 0) {
System.out.println("Store deleted.");
} else {
System.out.println("Store not deleted.");
}
} catch (SQLException e) {
System.out.println("SQL Exception: " + e.getStackTrace());
}
}
}