Java 无法使用 Intellij 测试与 mysql 的连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23771129/
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
Can't test connection to mysql using Intellij
提问by Lucas Lowry
I'm on OSX Mavericks, running Intellij 12, trying to add a datasource already defined in XML, using the persistence facet.
我在 OSX Mavericks 上运行 Intellij 12,尝试使用持久性方面添加已在 XML 中定义的数据源。
I can find and select the source fine, but when I try to test the connection I get:
我可以很好地找到并选择源,但是当我尝试测试连接时,我得到:
Connection to XXXXX failed: Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driver at java.net.URLClassLoader$1.run(URLClassLoader.java:366) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at java.lang.ClassLoader.loadClass(ClassLoader.java:425) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:358) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:190) at com.intellij.persistence.database.console.RemoteJdbcServer.main(RemoteJdbcServer.java:15)
与 XXXXX 的连接失败:线程“main”中的异常 java.lang.ClassNotFoundException: com.mysql.jdbc.Driver at java.net.URLClassLoader$1.run(URLClassLoader.java:366) at java.net.URLClassLoader$1.run( URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at java.lang.ClassLoader.loadClass(ClassLoader.java:425) at sun .misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:358) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName (Class.java:190) 在 com.intellij.persistence.database.console.RemoteJdbcServer.main(RemoteJdbcServer.java:15)
Looks like it's just a classpath problem, but I'm having a surprisingly rough time figuring out what I have to do to get the mysql jar on the classpath.
看起来这只是一个类路径问题,但我在弄清楚我必须做什么才能在类路径上获取 mysql jar 时遇到了令人惊讶的艰难时期。
Every question I've found deals with adding the jar to the classpath of your compiled or testing application, that's NOT what I'm looking for here. My application connects to mysql fine, it's the IntellijIDE that's having problems. Where do I need to add the path to this file?
我发现的每个问题都涉及将 jar 添加到已编译或测试应用程序的类路径中,这不是我在这里要找的。我的应用程序可以很好地连接到 mysql,是 IntellijIDE 出现问题。我需要在哪里添加此文件的路径?
回答by BevynQ
Either of the following options should work
以下任一选项都应该有效
add jar to the global libraries of the project.
将 jar 添加到项目的全局库中。
put jar in the lib directory of intellij.
将jar放在intellij的lib目录下。
put jar in the ext lib of the jre that intellij is running under.
将 jar 放在运行 intellij 的 jre 的 ext lib 中。
回答by frevib
For Intellij 15 (maybe lower versions) this works:
对于 Intellij 15(可能是较低版本),这有效:
Add the JDBC library using Intellij:
使用 Intellij 添加 JDBC 库:
- Goto View -> Tool Windows -> Database.
- In the Database window, select a database and click the icon for data source properties (cmd+i).
- In the next window goto MySQL in the Drivers section. In the window left under driver files click download. That's it.
- 转到视图 -> 工具窗口 -> 数据库。
- 在“数据库”窗口中,选择一个数据库并单击数据源属性图标 (cmd+i)。
- 在下一个窗口中,转到 Drivers 部分中的 MySQL。在驱动程序文件下左侧的窗口中,单击下载。就是这样。
回答by Ali
In IntelliJ goto View -> Tool Windows -> Database
在 IntelliJ 中转到视图 -> 工具窗口 -> 数据库
Inside Click + button -> DataSource -> MySql
在里面单击 + 按钮 -> 数据源 -> MySql
as shown below
如下所示
In below screen enter your mysql connection details and click Test Connection...
在下面的屏幕中输入您的 mysql 连接详细信息,然后单击测试连接...
make sure you download mysql driver for connection
确保您下载了用于连接的 mysql 驱动程序
回答by xuehui
Go to Itellij IDEA menue: File-->project structure-->Modules-->choose your module-->Dependencies-->Click + to add Library-->Choose mysql-connector-java-->Apply & OK
进入Itellij IDEA菜单:文件-->项目结构-->模块-->选择你的模块-->依赖-->点击+添加库-->选择mysql-connector-java-->应用&确定
回答by Alz
My IntelliJ 2018.2.5 is pure standalone, without Maven and is not showing a Database menu under View/Tool Windows. The only thing that worked for me was documented here: Adding Jar files to IntellijIdea classpathand the solution was to add the JAR to Project Structure. I do not understand why IntelliJ does not pick up CLASSPATH. Adding the JAR to IntelliJ's lib tree also did NOT do it.
我的 IntelliJ 2018.2.5 是纯独立的,没有 Maven,并且没有在视图/工具窗口下显示数据库菜单。唯一对我有用的事情记录在这里:将 Jar 文件添加到 IntellijIdea 类路径,解决方案是将 JAR 添加到项目结构。我不明白为什么 IntelliJ 不选择 CLASSPATH。将 JAR 添加到 IntelliJ 的 lib 树也没有这样做。
回答by Jesus Z Conte
I had this same issue, I did all suggested until I noticed there is a "Problems" index in the Project Structure window of IntelliJ. It said that there was a problem because my directory "lib" was not included in the Artifact. I just clicked "Fix" and after that, everything worked. Maybe this helps someone else.
我遇到了同样的问题,我做了所有建议,直到我注意到 IntelliJ 的“项目结构”窗口中有一个“问题”索引。它说存在问题,因为我的目录“lib”未包含在 Artifact 中。我只是单击“修复”,然后一切正常。也许这对其他人有帮助。