I have tried it on my own. I have read the article posted in another help query and done what I think that article directed (see code attached). And yet, I am getting the following error:
com/codegym/task/task21/task2111/Solution.java:29: error: <identifier> expected
try (stmt = connection.createStatement()) {
I do not understand what the issue is. This follows the example found in the before mentioned article and yet, does not work. Can someone please shine some light on this?
Thank you.
package com.codegym.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;
/*
Release resources
*/
public class Solution {
private Connection connection;
public Solution(Connection connection) {
this.connection = connection;
}
public List<User> getUsers(Connection connection) {
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;
}
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 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) {
}
}