forked from nickrgarner/WolfWR-DB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiscountSQL.java
159 lines (143 loc) · 4.79 KB
/
DiscountSQL.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
156
157
158
159
import java.sql.*;
import java.sql.Date;
import java.text.ParseException;
import java.sql.SQLException;
public class DiscountSQL {
/** 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 Discount relation for all tuples and attributes
* @return ResultSet containing all tuples
* @throws ClassNotFoundException
* @throws SQLException
* @throws ParseException
*/
public static ResultSet viewAllDiscounts() throws ClassNotFoundException, SQLException, ParseException
{
ResultSet returnSet = null;
PreparedStatement ps = null;
try {
ps = connection.prepareStatement("SELECT * FROM Discount;");
returnSet = ps.executeQuery();
ps.close();
} catch (SQLException e) {
System.out.println("SQL Exception: " + e.getStackTrace());
return null;
}
return returnSet;
}
/**
* Queries Discount relation for tuple matching the given discountID
* @param discountID ID of tuple to query for
* @return ResultSet containing tuple matching given discountnID or null
* @throws ClassNotFoundException
* @throws SQLException
* @throws ParseException
*/
public static ResultSet viewDiscount(int discountID) throws ClassNotFoundException, SQLException, ParseException
{
ResultSet returnSet = null;
PreparedStatement ps = null;
try {
ps = connection.prepareStatement("SELECT * FROM Discount WHERE discountID=?;");
ps.setInt(1, discountID);
returnSet = ps.executeQuery();
ps.close();
} catch (SQLException e) {
System.out.println("SQL Exception: " + e.getStackTrace());
return null;
}
return returnSet;
}
/**
* Adds tuple to Discount relation with the given attribute values
* @param discountID value to store
* @param productID value to store
* @param priceReduction value to store
* @param startDate value to store
* @param endDate value to store
* @throws ParseException
* @throws SQLException
*/
public static void addDiscount(int discountID, int productID, double priceReduction, Date startDate, Date endDate) throws ParseException, SQLException
{
PreparedStatement ps = null;
int id = 0;
try {
ps = connection.prepareStatement("INSERT INTO Discount VALUES (?,?,?,?,?);");
ps.setInt(1, discountID);
ps.setInt(2, productID);
ps.setDouble(3, priceReduction);
ps.setDate(4, startDate);
ps.setDate(5, endDate);
id = ps.executeUpdate();
ps.close();
System.out.println(id);
if (id > 0) {
System.out.println("Discount added successfully.");
} else {
System.out.println("Discount not added.");
}
} catch (SQLException e) {
System.out.println("SQL Exception: " + e.getStackTrace());
connection.rollback();
}
}
/**
* Changes attribute values to given values of discount tuple that matches discountID
* @param discountID ID of discount to edit
* @param productID value to store
* @param priceReduction value to store
* @param startDate value to store
* @param endDate value to store
* @throws ParseException
* @throws SQLException
*/
public static void editDiscount(int discountID, int productID, double priceReduction, Date startDate, Date endDate) throws ParseException, SQLException
{
PreparedStatement ps = null;
int id = 0;
try {
ps = connection.prepareStatement("UPDATE Discount SET productID=?, priceReduction=?, startDate=?, endDate=? WHERE discountID=?;");
ps.setInt(1, productID);
ps.setDouble(2, priceReduction);
ps.setDate(3, startDate);
ps.setDate(4, endDate);
ps.setInt(5, discountID);
id = ps.executeUpdate();
ps.close();
System.out.println(id);
if (id > 0) {
System.out.println("Discount added successfully.");
} else {
System.out.println("Discount not added.");
}
} catch (SQLException e) {
System.out.println("SQL Exception: " + e.getStackTrace());
connection.rollback();
}
}
/**
* Deletes discount from database that matches given discountID
* @param discountID ID of discount to delete
* @throws SQLException
*/
public static void deleteDiscount(int discountID) throws SQLException
{
try {
PreparedStatement ps = connection.prepareStatement("DELETE FROM Discount WHERE discountID=?;");
ps.setInt(1, discountID);
int id = ps.executeUpdate();
System.out.println(id);
if (id > 0) {
System.out.println("Discount deleted.");
} else {
System.out.println("Discount not deleted.");
}
} catch (SQLException e) {
System.out.println("SQL Exception: " + e.getStackTrace());
}
}
}