The try-with-resources construct works with resources that implement the AutoCloseable interface. This is stated in the documentation . The Class.forName method returns an object of type Class that does not implement this interface.
Accordingly, Class.forName cannot be used in try-with-resources .
Moreover, there is one more problem (although the first one is enough to create non-compiled code): you cannot just write, for example,
try (new FileReader("file.txt"))
Must be an assignment:
try (FileReader reader = new FileReader("file.txt"))
Class.forName code must be inside a try block:
try { Class.forName("org.postgresql.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); }
)- Bohdan Korinnyi