-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolution.java
More file actions
181 lines (150 loc) · 5.18 KB
/
Solution.java
File metadata and controls
181 lines (150 loc) · 5.18 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
173
174
175
176
177
178
179
180
181
//Greet Philologus and Julia, Nereus and his sister, and Olympas, and all the saints who are with them (Romans 16:15)
package com.javarush.task.task21.task2111;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;
/*
Освобождаем ресурсы
*/
public class Solution {
private Connection connection;
public Solution(Connection connection) {
this.connection = connection;
}
public List<User> getUsers() {
String query = "select ID, DISPLAYED_NAME, LEVEL, LESSON from USER";
List<User> result = new LinkedList();
try {
try(Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(query);) {
while (rs.next()) {
int id = rs.getInt("ID");
String name = rs.getString("DISPLAYED_NAME");
int level = rs.getInt("LEVEL");
int lesson = rs.getInt("LESSON");
result.add(new User(id, name, level, lesson));
}
}
} catch (SQLException e) {
e.printStackTrace();
result = null;
}
return result;
}
@Override
protected void finalize() throws Throwable {
super.finalize();
if (connection != null) {connection.close();}
}
public static class User {
private int id;
private String name;
private int level;
private int lesson;
public User(int id, String name, int level, int lesson) {
this.id = id;
this.name = name;
this.level = level;
this.lesson = lesson;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", level=" + level +
", lesson=" + lesson +
'}';
}
}
public static void main(String[] args) {
}
}
/*
Освобождаем ресурсы
Реализуй метод finalize, предварительно обдумав, что именно в нем должно быть.
Проведи рефакторинг метода getUsers в соответствии с java7 try-with-resources.
Требования:
1. Метод finalize в классе Solution должен содержать вызов super.finalize().
2. Метод finalize в классе Solution должен корректно завершаться в случае, если значение поля connection равно null.
3. Метод finalize в классе Solution должен закрывать текущее соединение, если значение поля connection не равно null.
4. Метод getUsers должен корректно использовать try-with-resources.
package com.javarush.task.task21.task2111;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;
*
Освобождаем ресурсы
*
public class Solution {
private Connection connection;
public Solution(Connection connection) {
this.connection = connection;
}
public List<User> getUsers() {
String query = "select ID, DISPLAYED_NAME, LEVEL, LESSON from USER";
List<User> result = new LinkedList();
Statement stmt = null;
ResultSet rs = null;
try {
stmt = connection.createStatement();
rs = stmt.executeQuery(query);
while (rs.next()) {
int id = rs.getInt("ID");
String name = rs.getString("DISPLAYED_NAME");
int level = rs.getInt("LEVEL");
int lesson = rs.getInt("LESSON");
result.add(new User(id, name, level, lesson));
}
} catch (SQLException e) {
e.printStackTrace();
result = null;
} finally {
if(stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return result;
}
public static class User {
private int id;
private String name;
private int level;
private int lesson;
public User(int id, String name, int level, int lesson) {
this.id = id;
this.name = name;
this.level = level;
this.lesson = lesson;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", level=" + level +
", lesson=" + lesson +
'}';
}
}
public static void main(String[] args) {
}
}
*/