Answer the question
In order to leave comments, you need to log in
Why do you need to import classes in every file?
Hello. I'm completely new to java and I don't understand one thing. I use the JDBC driver to work with the database, which, in turn, requires the connection of its libraries in the com.mysql package. I have two files, 1 - the base class App.java, the second class that implements work with the database - DB.java. The DB executes the query and returns an array of type ResultSet. This is a class from the com.mysql package. So, I understand correctly that the com.mysql.ResultSet class must be connected in App.java or a line in DB.java will be enough (the compiler swears and claims the opposite, but it’s not clear to me why to import again when this class is already connected in DB while DB is connected in App)?
Answer the question
In order to leave comments, you need to log in
Because when you write to App.java the
compiler won't know what ResultSet is meant here. He will need to look in the DB, it will already look in the used class from com.mysql. Etc. If you specify a class, then the compiler immediately understands what will be returned here, what exceptions can be thrown. And the programmer understands.
PS You can in DB.getUsers() (for example) convert ResultSet into an array, well, or into a collection. IMHO it would be even more correct, App, as it were, should not know where and how the data is taken from.
It seems to me that in the App class you are using some part of the MySQL library, which is why it swears. So if the DB returns a ResultSet to App, then App without importing the MySQL library does not know the type of ResultSet, because it's not in Java's base types.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question