-
Notifications
You must be signed in to change notification settings - Fork 0
/
HomeProperty.java
126 lines (105 loc) · 4.66 KB
/
HomeProperty.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
import java.sql.*;
import java.util.Scanner;
public class HomeProperty implements Entity {
public void listAll() {
System.out.println("================================= Properties =======================================");
printHomeProperties();
System.out.println("====================================================================================");
}
public void displayDetails(int id) {
System.out.println("========================================== Property Details ======================================");
printPropertyDetails(id);
System.out.println("===================================================================================================");
}
public void addNew() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter Street Address:");
String street = scanner.nextLine();
System.out.print("Enter City:");
String city = scanner.nextLine();
System.out.print("Enter State acronym:");
String state = scanner.nextLine();
System.out.print("Enter Zip Code:");
String zipcode = scanner.nextLine();
try {
HomeProperty.insert(street, city, state, zipcode);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void update(int id) {
}
public void delete(int id) {
String sql = "delete from property where propertyId = ?";
try (Connection conn = Connect.getConnection()) {
PreparedStatement statement = conn.prepareStatement(sql);
statement.setInt(1, id);
int rowsAffected = statement.executeUpdate();
if (rowsAffected == 1) {
System.out.println("Successfully deleted Property!");
}
} catch (SQLException e) {
System.out.println("Error Occurred!");
System.out.println(e.getMessage());
}
}
private static void printHomeProperties() {
String sql = "select propertyId, street, city, state, zipCode from property";
try (Connection conn = Connect.getConnection()) {
if (conn == null) return;
Statement statement = conn.createStatement();
ResultSet result = statement.executeQuery(sql);
String displayFormat = "%-3s%-25s%-20s%-20s%-20s";
System.out.println(String.format(displayFormat, "Id", "Street", "City", "State", "Zip Code"));
while (result.next()) {
int id = result.getInt("propertyId");
String street = result.getString("street");
String city = result.getString("city");
String state = result.getString("state");
String zip = result.getString("zipCode");
System.out.println(String.format(displayFormat, id, street, city, state, zip));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
private static void printPropertyDetails(int id) {
String sql = "select street, city, state, zipCode from property where propertyId = ?";
try (Connection conn = Connect.getConnection()) {
if (conn == null) return;
PreparedStatement statement = conn.prepareStatement(sql);
statement.setInt(1, id);
ResultSet result = statement.executeQuery();
if (result.next()) {
String street = result.getString("street");
System.out.printf("Street:%s", street);
String city = result.getString("city");
System.out.printf("\tCity:%s", city);
String state = result.getString("state");
System.out.printf("\tState:%s", state);
String zip = result.getString("zipCode");
System.out.printf("\tZip Code:%s%n", zip);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
private static void insert
(String street, String city, String state, String zipcode)
throws SQLException {
String sql = "insert Property (street, city, state, zipcode) "
+ "values (?, ?, ?, ?)";
Connection conn = Connect.getConnection();
PreparedStatement statement = conn.prepareStatement(sql);
statement.setString(1, street);
statement.setString(2, city);
statement.setString(3, state);
statement.setString(4, zipcode);
int rowsAffected = statement.executeUpdate();
if (rowsAffected == 1) {
System.out.println("Successfully added new owner!");
} else {
System.out.println("Error Occurred!");
}
}
}