以下是连接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/