java - 在 JDBC 中使用 class.forName

标签 java class reflection jdbc

以下是连接MySQL数据库的代码

// This will load the MySQL driver, each DB has its own driver
  Class.forName("com.mysql.jdbc.Driver");
  // Setup the connection with the DB
  connect = DriverManager
      .getConnection("jdbc:mysql://localhost/feedback?"
          + "user=sqluser&password=sqluserpw");

我正在学习java中的反射,可以使用以下方法获得该类 Class.forName()

 Class c = Class.forName("java.awt.Button");

是否使用了语法。

我的问题是为什么 MySQL ForName 不需要被捕获在变量中?

最佳答案

您的意思是为什么不将 Class 对象保存到变量中。 因为不需要。只需要加载该类,实际的 Class 对象是多余的。

而且现在您甚至不再需要执行 Class.forName() 了。如果您使用 JDBC 类型 4(纯 java)驱动程序,则会自动从类路径中找到驱动程序。

关于java - 在 JDBC 中使用 class.forName,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20455114/

相关文章:

java - 是否可以解耦 Eclipse 的代码索引功能?

java - 扩展枚举并在 java 中的重写方法中使用它

php - 来自文件的反射类 PHP?

java - 如何动态创建一个使用反射扩展另一个动态类的类?

Java:通过泛型类型的反射调用特定方法

java - Spring Boot 中未找到 Elasticsearch 方法

java - 不同默认字符集中的 String.getBytes()

c++ - 类原型(prototype)创建 C++

java - 面向对象的Java : External method calling with variable reference

java - 显示 ArrayList 中对象的值(来自子类)(基于父类(super class))