-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVendorDAO.java
More file actions
107 lines (98 loc) · 3.65 KB
/
Copy pathVendorDAO.java
File metadata and controls
107 lines (98 loc) · 3.65 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
package DAO;
import Model.Vendor;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class VendorDAO {
Connection conn;
public VendorDAO(Connection conn){
this.conn = conn;
}
//add new vendor to table "Vendor"
public void insertVendor(Vendor v){
try{
PreparedStatement ps = conn.prepareStatement("insert into " +
"Vendor (vendor_id, vendor_name)" + "values (?, ?)");
ps.setInt(1, v.getVendorId());
ps.setString(2, v.getVendorName());
ps.executeUpdate();
}catch(SQLException e){
//add better messaging
e.printStackTrace();
}
}
//get vendor from table "Vendor" with a matching name
public List<Vendor> getVendorsByName(String vendorName) {
List<Vendor> vendorList = new ArrayList<>();
try{
PreparedStatement ps = conn.prepareStatement("select * from Vendor where vendor_name = ?");
ps.setString(1, vendorName);
ResultSet resultSet = ps.executeQuery();
while (resultSet.next()) {
int resultID = resultSet.getInt("vendor_id");
String resultName = resultSet.getString("vendor_name");
Vendor v = new Vendor(resultID, resultName);
vendorList.add(v);
}
}catch (SQLException e) {
//add better messaging
e.printStackTrace();
}
return vendorList;
}
//get all vendors from table "Vendor"
public List<Vendor> getVendors(){
List<Vendor> vendorList = new ArrayList<>();
try{
PreparedStatement ps = conn.prepareStatement("select * from Vendor");
ResultSet resultSet = ps.executeQuery();
while (resultSet.next()) {
int vendorID = resultSet.getInt("vendor_id");
String vendorName = resultSet.getString("vendor_name");
Vendor v = new Vendor(vendorID, vendorName);
vendorList.add(v);
}
}catch (SQLException e) {
//add better messaging
e.printStackTrace();
}
return vendorList;
}
//get vendor from table "Vendor" by vendorId
public Vendor getVendorById(int vendorId) {
try {
PreparedStatement ps = conn.prepareStatement("select * from Vendor where vendor_id = ?");
ps.setInt(1, vendorId);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
int vendorID = rs.getInt("vendor_id");
String vendorName = rs.getString("vendor_name");
Vendor v = new Vendor (vendorID, vendorName);
return v;
} else {
return null;
}
} catch (SQLException e){
//add better messaging
e.printStackTrace();
}
return null;
}
//update/put vendor in table "Vendor" via vendorID
public void updateVendor (Vendor v, int id) {
try {
PreparedStatement ps = conn.prepareStatement("update Vendor set vendor_name=? where vendor_id=?");
ps.setString(1, v.getVendorName());
ps.setInt(2, id);
ps.executeUpdate();
}catch (SQLException e){
//add better messaging
e.printStackTrace();
}
}
//delete vendor from table "Vendor" via vendorID (complexity: "Product" table dependency)
//can we include a variation in "Update/Put" where we are able to remove a vendor name and have it be null instead?
}