Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (6.1k points)

I have written down the steps I use to connect to MySQL.

Connection con = null;

Resultset rs = null;

Statement st = null;

Class.forName("com.mysql.jdbc.Driver").newInstance();

con = DriverManager.getConnection("jdbc:mysql://localhost:3306/database","root","passwp");

So I want to know what this Class.forName statement does?

1 Answer

0 votes
by (11.7k points)

You can find out the Class class in java.lang package, it is distributed with Java, and it gets imported by itself into each class.

The forName() method returns the Class object for the parameter that was loaded by the class loader. The newInstance() method returns a new instance of the class.

Therefore when you call Class.forName(...) it returns com.mysql.jdbc.Driver.class. Thereafter the newInstance() will be called on that class which returns an instance of the class, with no parameters, it is just calling new com.mysql.jdbc.Driver();.

If you want to get more insights into SQL, check out this SQL Course from Intellipaat.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...