In Java connecting to mysql, what is the meaning of Class.forName?

What is the purpose of this line?
It does not return a value or sets the state of an existing class / object (or is it?)

Class.forName ("com.mysql.jdbc.Driver").newInstance ();
+5
source share
9 answers

It uses reflection to look at the class path for the class named "com.mysql.jdbc.Driver" and creates a new instance.

In code when you write

Integer foo = new Integer()

Instead you can write

Integer foo = Class.forName("java.lang.Integer").newInstance()

? , . , , , . , ( Class.forName, )

+8

, com.mysql.jdbc.Driver , :

static {java.sql.DriverManager.registerDriver(new com.mysql.jdbc.Driver())};

forName. , , MySQL.

newInstance, , . .

+7

JDBC MySQL.

, , , , . , java.sql.Connection, MySQL . .

, JDBC .

+3

- , , .

, JVM .class pathpath, newInstance() .

, . ( 100% , , "" , , )

+1

Calling Class.forName .

+1

.forName( "X" ) X ( ).

+1

java- , ( ) . JVM ( ) Java-.

Class.forName( "className" ) , " " / .

, :

ClassName classInstance = new ClassName();
+1

Class.forName Class . , com.mysql.jdbc.Driver, , . , Class.newInstance() , com.mysql.jdbc.Driver - .

+1

Class.forName( )

, [...],

, ( ) com.mysql.jdbc.Driver

Class.newInstace()

, [...]

, , mysql.

:

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

:

import java.sql.Driver;
.... 
Driver driver = new com.mysql.jdbc.Driver()

, DriverManager doc, :

JDBC, Class.forName()

, DriverManager .

, :

.. DataSource, API JDBC 2.0, . DataSource .

-1
source

All Articles