-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSupplierSQL.java
More file actions
172 lines (157 loc) · 5.98 KB
/
Copy pathSupplierSQL.java
File metadata and controls
172 lines (157 loc) · 5.98 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
import java.sql.*;
import java.sql.SQLException;
import java.text.ParseException;
public class SupplierSQL{
//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;
/**
* Method is called in Supplier file where user wants to see all the Suppliers in the database. This method
* will perform the sql statement and generate a result set of all the Suppliers in the database.
* @return ResultSet
* @throws ClassNotFoundException
* @throws SQLException
* @throws ParseException
*/
public static ResultSet viewAllSuppliers() throws ClassNotFoundException, SQLException, ParseException{
//A table of data representing a database result set, which is usually generated by executing a
//statement that queries the database.
ResultSet returnSet = null;
//Object that represents a precompiled SQL statement
PreparedStatement ps = null;
try {
ps = connection.prepareStatement("SELECT * FROM Supplier;");
returnSet = ps.executeQuery();
ps.close();
} catch (SQLException e) {
System.out.println("SQL Exception");
e.getStackTrace();
return null;
}
return returnSet;
}
/**
* Method is called in the Supplier file and is used to find a specific supplier.
* @param supplierID
* @return ResultSet
* @throws ClassNotFoundException
* @throws SQLException
* @throws ParseException
*/
public static ResultSet viewSupplier(int supplierID) throws ClassNotFoundException, SQLException, ParseException{
//A table of data representing a database result set, which is usually generated by executing a
//statement that queries the database.
ResultSet returnSet = null;
//Object that represents a precompiled SQL statement
PreparedStatement ps = null;
try {
ps = connection.prepareStatement("SELECT * FROM Supplier WHERE supplierID = ?;");
ps.setInt(1, supplierID);
returnSet = ps.executeQuery();
ps.close();
} catch (SQLException e) {
System.out.println("SQL Exception");
e.getStackTrace();
return null;
}
return returnSet;
}
/**
* Method is called from Supplier file and is called after the user has finished filling all the attributes for a Supplier
* @param supplierID
* @param name
* @param phone
* @param email
* @param location
* @param amountOwed
* @throws ParseException
*/
static void addSupplier(int supplierID, String name, String phone, String email, String location, double amountOwed) throws SQLException, ParseException{
//Object that represents a precompiled SQL statement
PreparedStatement ps = null;
int id = 0;
try{
ps = connection.prepareStatement("INSERT INTO Supplier (supplierID, name, phone, email, location, amountOwed) VALUES (?,?,?,?,?,?);");
ps.setInt(1,supplierID);
ps.setString(2,name);
ps.setString(3,phone);
ps.setString(4,email);
ps.setString(5,location);
ps.setDouble(6,amountOwed);
id = ps.executeUpdate();
connection.commit();
ps.close();
System.out.println(id);
if(id > 0){
System.out.println("Supplier added successfully");
} else{
System.out.println("Supplier not added");
}
}
catch (SQLException e) {
System.out.println("SQL Exception");
connection.rollback();
e.getStackTrace();
}
}
/**
* Method is called from Supplier file and is called after the user has finished filling all the attributes that they want to edit
* for a Supplier
* @param supplierID
* @param name
* @param phone
* @param email
* @param location
* @param amountOwed
* @throws ParseException
*/
static void editSupplier( int supplierID, String name, String phone, String email, String location, double amountOwed) throws SQLException, ParseException{
//Object that represents a precompiled SQL statement
PreparedStatement ps = null;
int id = 0;
try{
ps = connection.prepareStatement("UPDATE Supplier SET name = ?, phone = ?, email = ?, location = ?, amountOwed = ? WHERE supplierID = ?;");
ps.setString(1,name);
ps.setString(2,phone);
ps.setString(3,email);
ps.setString(4,location);
ps.setDouble(5, amountOwed);
ps.setInt(6,supplierID);
id = ps.executeUpdate();
connection.commit();
ps.close();
System.out.println(id);
if(id > 0){
System.out.println("Supplier updated successfully");
} else{
System.out.println("Supplier not updated");
}
}
catch (SQLException e) {
System.out.println("SQL Exception");
connection.rollback();
e.getStackTrace();
}
}
/**
* Method is called from Supplier file and is called to delete a supplier from the database
* @param supplierID
*/
static void deleteSupplier(int supplierID){
try {
PreparedStatement ps = connection.prepareStatement("DELETE FROM Supplier WHERE supplierID = ?;");
ps.setInt(1, supplierID);
int id = ps.executeUpdate();
System.out.println(id);
if (id > 0) {
System.out.println("Supplier deleted");
} else {
System.out.println("Supplier not deleted");
}
} catch (SQLException e) {
System.out.println("SQL Exception");
e.getStackTrace();
}
}
}