问题描述
我想知道为什么Class.forName("com.mysql.jdbc.Driver");
和Class.forName("com.mysql.jdbc.Driver").newInstance();
在我使用它们连接到数据库时都起作用.没错,因为没有创建新实例,所以前者不是不应该工作的.但是,它仍然有效.我正在使用netbeans 6.9.1.感谢您的输入!
Was wondering why both Class.forName("com.mysql.jdbc.Driver");
and Class.forName("com.mysql.jdbc.Driver").newInstance();
work when i use them to connect to a database. By right, isn't the former not supposed to work, since no new instance was created. And yet, it still works. Im using netbeans 6.9.1. Thanks for your input!
推荐答案
Class.forName("xxx")
不会创建与数据库的连接,它只是加载JDBC驱动程序并对其进行注册,以便随后的DriverManager.getConnection(...)
调用将起作用.不需要自己实例化驱动程序.
Class.forName("xxx")
doesn't create a connection to the database, it just loads the JDBC driver and registers it so that a subsequent DriverManager.getConnection(...)
call will work. Instantiating the driver yourself is not necessary.
这篇关于JDBC连接-Class.forName与Class.forName().newInstance吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!