Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in SQL by (5k 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 (10.2k 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.

Browse Categories

...