Java IntelliJ中的JDBC mysql驱动配置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41667087/
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-08-11 23:34:31  来源:igfitidea点击:

JDBC mysql driver configuration in IntelliJ

javamysqljdbcintellij-idea

提问by Mohru

I'm creating a web app using servlets and Tomcat in IntelliJ. I want to connect my application to a MySQL database with JDBC. I downloaded MySQL Workbench, connected to it, and created a database.

我正在 IntelliJ 中使用 servlet 和 Tomcat 创建一个 Web 应用程序。我想使用 JDBC 将我的应用程序连接到 MySQL 数据库。我下载了 MySQL Workbench,连接到它,并创建了一个数据库。

Now, in my IntelliJ project I followed thistutorial to add database connection, and my properties look like this: enter image description here

现在,在我的 IntelliJ 项目中,我按照教程添加了数据库连接,我的属性如下所示: 在此处输入图片说明

My mysql driver version is 5.1.35, so it should be compatible with JDBC 4.0. As found out (mainly here) to connect with my database I should use just:

我的mysql驱动版本是5.1.35,所以应该兼容JDBC 4.0。发现(主要在这里)连接我的数据库我应该只使用:

Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);

However,when I try to connect I get No suitable driverexception. Even when I add

但是,当我尝试连接时,出现No suitable driver异常。即使我添加

Class.forName(DB_DRIVER).newInstance();

I end up with the same. It only works when I add manually jar to this driver in my libraries and artifacts: enter image description hereenter image description here

我最终也一样。它仅在我在库和工件中手动向此驱动程序添加 jar 时才有效: 在此处输入图片说明在此处输入图片说明

Is it really the only way? If IntelliJ downloaded the jar automatically, shouldn't it also load it automatically when I try to connect? In DriverManagerdocumentation there is something like this: As part of its initialization, the DriverManager class will attempt to load the driver classes referenced in the "jdbc.drivers" system property.Do I have to configure this property somehow?

这真的是唯一的方法吗?如果 IntelliJ 自动下载了 jar,当我尝试连接时,它不应该也自动加载它吗?在DriverManager文档中有这样的东西:As part of its initialization, the DriverManager class will attempt to load the driver classes referenced in the "jdbc.drivers" system property.我必须以某种方式配置这个属性吗?

采纳答案by DeepakV

Short answer - Yes, project driver jars need to be mentioned explicitly.

简短回答 - 是的,需要明确提及项目驱动程序 jar。

When you configure DB for Intellij(using its database panel), Intellij can download respective driver based on database type. This is similar to configuring MySQL Workbench. Its for development purposes.

当您为 Intellij配置 DB 时(使用其数据库面板),Intellij 可以根据数据库类型下载相应的驱动程序。这类似于配置 MySQL Workbench。其用于开发目的。

When you use JDBC connection within your project, you need to tell Intellij somehow that you need corresponding driver. You can do it the following ways:

当您在项目中使用 JDBC 连接时,您需要以某种方式告诉 Intellij 您需要相应的驱动程序。您可以通过以下方式进行操作:

  1. Manually add driver jar as library to the project (like you did)
  2. In code, use the driver class. Use Intellisense to import the JAR (screenshot below). 'add to classpath'.
  3. Same as above but for maven based project (screenshot below). 'Add maven dependency'
  4. Use Spring-boot framework, let it automatically figure out the driver for you.
  1. 手动将驱动程序 jar 作为库添加到项目中(就像您所做的那样)
  2. 在代码中,使用驱动程序类。使用 Intellisense 导入 JAR(下面的屏幕截图)。'添加到类路径'。
  3. 与上面相同,但适用于基于 maven 的项目(下面的屏幕截图)。'添加Maven依赖'
  4. 使用Spring-boot框架,让它自动为你找出驱动程序。

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明