java 更好的理解 - Class.forName("com.mysql.jdbc.Driver").newInstance();

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12933113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 10:50:02  来源:igfitidea点击:

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

javamysql

提问by jason m

I came across this helpful link with codewhich works perfectly when updated to hit against my web server. I can do absolutely everything.

我遇到了这个有用的代码链接,当更新到我的网络服务器时,它可以完美地工作。我绝对可以做任何事情。

Now, the only thing I do not fully understand is the Class.forName().

现在,我唯一不完全理解的是Class.forName().

Why is this being used? Can this be done differently? Is this a work around for something else? Adding a reference? Creating a class as implementing/extending another one?

为什么要使用这个?这可以做不同的事情吗?这是解决其他问题的方法吗?添加参考?创建一个类作为实现/扩展另一个类?

I want to fully understand what is going on, but this is in my way.

我想完全了解发生了什么,但这妨碍了我。

Thank you

谢谢

回答by Bruno Reis

That code is forcing the class representing the MySQL driver to load and initialize. In Java, a class is not loaded unless it is necessarythat the class gets loaded. Since JDBC code usually never directly references the driver, it wouldn't get loaded without Class.forName(or some other equivalent alternatives).

该代码强制表示 MySQL 驱动程序的类加载和初始化。在 Java 中,除非有必要加载类,否则不会加载类。由于 JDBC 代码通常从不直接引用驱动程序,因此它不会在没有Class.forName(或其他一些等效替代方法)的情况下加载。

Note that it is necessary to both loadand initializethe class, which are 2 different things.

请注意,加载初始化类是必要的,这是两件不同的事情。

Also, note that it is not necessary to call .newInstance()-- the static initializer of the Driver already registers itself as a JDBC driver.

另外,请注意,没有必要调用.newInstance()——驱动程序的静态初始化程序已经将自己注册为 JDBC 驱动程序。

Finally, note that with the Service Loader APIit is usually not necessary to call Class.forName() to load the driver: it can be loaded automatically.

最后,请注意,使用Service Loader API通常不需要调用 Class.forName() 来加载驱动程序:它可以自动加载。

回答by Jesper

Class.forName(className)loads the class with the specified className.

Class.forName(className)加载具有指定的类className

JDBC drivers are loaded this way to avoid having to have a compile-time dependency on a specific JDBC driver. The idea is that you use Java's JDBC API (the classes and interfaces defined in the packages java.sqland javax.sql) without having to refer directly to a particular JDBC driver.

JDBC 驱动程序以这种方式加载,以避免编译时依赖于特定的 JDBC 驱动程序。这个想法是您使用 Java 的 JDBC API(在包java.sql和 中定义的类和接口javax.sql)而不必直接引用特定的 JDBC 驱动程序。

When you let Java load the driver class with the forNamecall, the driver will register itself so that it can be used.

当您让 Java 通过forName调用加载驱动程序类时,驱动程序将自行注册以便可以使用。

Note that for most JDBC drivers it's unnecessary to explicitly create a new instance of the driver class; you can leave off the .newInstance()call.

请注意,对于大多数 JDBC 驱动程序,没有必要显式创建驱动程序类的新实例;你可以.newInstance()挂断电话。

Note that since JDBC version 4.0, the Class.forName()call isn't necessary anymore - the process to discover drivers has been improved, JDBC can load them automatically.

请注意,从 JDBC 4.0 版开始,Class.forName()不再需要调用 - 发现驱动程序的过程已得到改进,JDBC 可以自动加载它们。

回答by Ta Duy Anh

All JDBC Drivers have a static block that registers itself with DriverManager and DriverManager has static an initializer only.

所有 JDBC 驱动程序都有一个静态块,它向 DriverManager 注册自己,而 DriverManager 仅具有静态初始化程序。

The MySQL JDBC Driver has a static initializer looks like this:

MySQL JDBC 驱动程序有一个静态初始化程序,如下所示:

static {
    try {
        java.sql.DriverManager.registerDriver(new Driver());
    } catch (SQLException E) {
        throw new RuntimeException("Can't register driver!");
    }
}

JVM executes the static block and the Driver registers itself with the DriverManager. You need a database connection to manipulate the database. In order to create the connection to the database, the DriverManager class has to know which database driver you want to use. It does that by iterating over the array (internally a Vector) of drivers that have registered with it and calls the acceptsURL(url) method on each driver in the array, effectively asking the driver to tell it whether or not it can handle the JDBC URL.

JVM 执行静态块,驱动程序向 DriverManager 注册自己。您需要一个数据库连接来操作数据库。为了创建到数据库的连接,DriverManager 类必须知道您要使用哪个数据库驱动程序。它通过迭代已注册的驱动程序数组(内部为 Vector)并调用数组中每个驱动程序的 acceptsURL(url) 方法,有效地要求驱动程序告诉它它是否可以处理 JDBC网址。

回答by m3th0dman

You can use for example:

例如,您可以使用:

import com.mysql.jdbc.Driver;

public class MyClass {
   //[...]
   public void myMethod() {
        Class<Driver> clz = Driver.class;
        Driver driver = clz.newInstance();
   }
}

But this is not so flexible; you can, for example, read the name "com.mysql.jdbc.Driver" from a config file. Maybe you want to user other driver (from Oracle) and you do not want to change the code; in the way presented above, this is not possible, so the proper approach is with Class.forName(name).

但这不是那么灵活;例如,您可以从配置文件中读取名称“com.mysql.jdbc.Driver”。也许您想使用其他驱动程序(来自 Oracle)并且您不想更改代码;在上面介绍的方式中,这是不可能的,因此正确的方法是使用Class.forName(name).