简体   繁体   English

我必须在Java中的每个方法中编写Class.forname(“ com.mysql.jdbc.Driver”)吗?

[英]do i have to write Class.forname(“com.mysql.jdbc.Driver”) in every method in java?

While writing an application that interacts with a database the only way I can get it to work is if I write Class.forName("com.mysql.jdbc.Driver") in every method that interacts with the database. 在编写与数据库交互的应用程序时,使它正常工作的唯一方法是,如果我在与数据库交互的每种方法中都编写Class.forName("com.mysql.jdbc.Driver")

Is this the only way to do it or is there a simpler way? 这是唯一的方法,还是有更简单的方法?

This line can't possibly work. 这条线可能无法工作。 jdbc:mysql://localhost/phone_book is not a valid class name. jdbc:mysql://localhost/phone_book不是有效的类名。 You'll aways get an exception when executing this method. 执行此方法时,您将获得异常。

If you mean Class.forname("com.mysql.jdbc.Driver") , all it does is make sure the classloader loads the class. 如果您的意思是Class.forname("com.mysql.jdbc.Driver") ,则Class.forname("com.mysql.jdbc.Driver")确保类加载器加载该类即可。 When the class is loaded, its static block is executed, and this static block registers the MySQL driver to the JDBC API. 加载该类时,将执行其静态块,并且此静态块将MySQL驱动程序注册到JDBC API。 Doing it once is sufficient. 这样做一次就足够了。 Once a class is loaded, it's loaded. 加载类后,即会加载它。 Loading it a second time won't change anything. 第二次加载不会改变任何东西。

Invoking Class.forName(<jdbcDriverClass>) serves to load the JDBC driver (that implements the java.sql.Driver interface). 调用Class.forName(<jdbcDriverClass>)用于加载JDBC驱动程序(实现java.sql.Driver接口)。 Loading a JDBC driver class results in invocation of the static initializers within the Driver and most, if not all all drivers invoke DriverManager.registerDriver(jdbcDriverInstance) within the static initializer. 加载JDBC驱动程序类会导致调用Driver内的静态初始化Driver并且,如果不是所有的Driver ,大多数驱动程序会调用静态初始化程序内的DriverManager.registerDriver(jdbcDriverInstance)

The pertinent method invocation serves the purpose of registering the JDBC driver with the DriverManager , allowing for the DriverManager.getConnection methods to return a connection to a database that is supported by the JDBC driver. 相关方法调用的目的是向DriverManager注册JDBC驱动程序,从而允许DriverManager.getConnection方法将连接返回到JDBC驱动程序支持的数据库。 Each JDBC driver recognizes only one/some JDBC URL connection format(s), and when you invoke DriverManager.getConnection(...) , the DriverManager class cycles through all the drivers registered, and only the driver that recognizes the connection URL format will return a connection. 每个JDBC驱动程序仅识别一种/某些JDBC URL连接格式,并且当您调用DriverManager.getConnection(...)DriverManager类在所有已注册的驱动程序之间循环,并且只有识别连接URL格式的驱动程序才会返回连接。

Going by the above, Class.forname("jdbc:mysql://localhost/phone_book") will make no sense in this context, as jdbc:mysql://localhost/phone_book is not a JDBC driver class. 按照以上说明, Class.forname("jdbc:mysql://localhost/phone_book")在这种情况下是没有意义的,因为jdbc:mysql://localhost/phone_book不是JDBC驱动程序类。 Rather it is a connection URL format. 而是一种连接URL格式。 Since you are interested in accessing a MySQL database instance, you should use the driver class of the MySQL Connector/J driver: Class.forName("com.mysql.jdbc.Driver") . 由于您有兴趣访问MySQL数据库实例,因此应使用MySQL Connector / J驱动程序的驱动程序类: Class.forName("com.mysql.jdbc.Driver") When you need to access the database instance, you ought to use the JDBC URL as: DriverManager.getConnection("jdbc:mysql://localhost/phone_book"); 当您需要访问数据库实例时,应该使用JDBC URL: DriverManager.getConnection("jdbc:mysql://localhost/phone_book"); . You can pass in the user Id and password, using the three-parameter variant of the DriverManager.getConnection(...) method. 您可以使用DriverManager.getConnection(...)方法的三参数变体来传递用户ID和密码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 的Class.forName(“com.mysql.jdbc.Driver”)。的newInstance() - Class.forName(“com.mysql.jdbc.Driver”).newInstance() ClassNotFoundException Class.forName(“ com.mysql.jdbc.Driver”); - ClassNotFoundException Class.forName(“com.mysql.jdbc.Driver”); Class.forName(“ com.mysql.jdbc.Driver”)中的ClassNotFoundException; - ClassNotFoundException from Class.forName(“com.mysql.jdbc.Driver”); 如何保护Class.forName(“com.mysql.jdbc.Driver”)? - How to secure Class.forName("com.mysql.jdbc.Driver)? 与Java applet相关的Class.forName(“ com.mysql.jdbc.Driver”)。newInstance()中的ClassNotFoundException - ClassNotFoundException in Class.forName(“com.mysql.jdbc.Driver”).newInstance() related to java applet 这究竟做了什么Class.forName(“com.mysql.jdbc.Driver”)。newInstance(); - what exactly does this do Class.forName(“com.mysql.jdbc.Driver”).newInstance(); 无法通过class.forName(“ com.mysql.jdbc.Driver”)加载com.mysql.jdbc.Driver类 - Couldn't load class com.mysql.jdbc.Driver by class.forName(“com.mysql.jdbc.Driver”) 将“ java.lang.ClassNotFoundException:com.mysql.jdbc.Driver”添加到构建路径并使用Class.forName()注册后,如何修复? - How to fix 'java.lang.ClassNotFoundException: com.mysql.jdbc.Driver' after adding it to the build path and registered using Class.forName(); Class.forName(&quot;com.mysql.jdbc.Driver&quot;) 没有在该类的 jar 文件中查找 - Class.forName("com.mysql.jdbc.Driver") is not looking in jar file for the class Class.forName(“ com.mysql.jdbc.Driver”)在Raspberry Pi上不起作用 - Class.forName(“com.mysql.jdbc.Driver”) does not work on Raspberry Pi
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM