While writing an application that interacts with a database the only way I can get it to work is if I write Class.forName("com.mysql.jdbc.Driver") in every method that interacts with the database.
Is this the only way to do it or is there a simpler way?
This line can’t possibly work.
jdbc:mysql://localhost/phone_bookis not a valid class name. You’ll aways get an exception when executing this method.If you mean
Class.forname("com.mysql.jdbc.Driver"), all it does is make sure the classloader loads the class. When the class is loaded, its static block is executed, and this static block registers the MySQL driver to the JDBC API. Doing it once is sufficient. Once a class is loaded, it’s loaded. Loading it a second time won’t change anything.