java JDBC 类路径不起作用

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

JDBC CLASSPATH Not Working

javajdbcclasspath

提问by Brian

I'm setting up a simple JDBC connection to my working MySQL database on my server. I'm using the Connector-J provided by MySQL. According to their documentation, I'm suppose to create the CLASSPATH variable to point to the directory where the mysql-connector-java-5.0.8-bin.jar is located. I used export set CLASSPATH=/path/mysql-connector-java-5.0.8-bin.jar:$CLASSPATH. When I type echo $CLASSPATHto see if it exists, everything seems fine. But then when I open a new terminal and type echo $CLASSPATHit's no longer there. I think this is the main reason why my Java server won't connect to the JDBC, because it isn't saving the CLASSPATH variable I set.

我正在我的服务器上建立一个简单的 JDBC 连接到我的工作 MySQL 数据库。我正在使用 MySQL 提供的 Connector-J。根据他们的文档,我假设创建 CLASSPATH 变量以指向 mysql-connector-java-5.0.8-bin.jar 所在的目录。我用过export set CLASSPATH=/path/mysql-connector-java-5.0.8-bin.jar:$CLASSPATH。当我输入echo $CLASSPATH以查看它是否存在时,一切似乎都很好。但是当我打开一个新终端并输入echo $CLASSPATH它时,它就不再存在了。我认为这是我的 Java 服务器无法连接到 JDBC 的主要原因,因为它没有保存我设置的 CLASSPATH 变量。

Anyone got suggestions or fixes on how to set up JDBC in the first place?

任何人都有关于如何首先设置 JDBC 的建议或修复?

采纳答案by BalusC

Forget the CLASSPATHenvironment variable. It's a big joke. It's ignored by pretty much everything else than a low-level java com.example.Foocommand. It's ignored when you add the -jarargument. It's ignored when you add the -cpor -classpathargument. It's ignored by IDE's and web/application servers. That environment variable was intented as a convenience for starters. But beyond that, it's useless.

忘记CLASSPATH环境变量。这是一个大笑话。除了低级java com.example.Foo命令之外,几乎所有其他东西都会忽略它。添加-jar参数时,它会被忽略。添加-cpor-classpath参数时,它会被忽略。它会被 IDE 和 Web/应用程序服务器忽略。该环境变量旨在为初学者提供便利。但除此之外,它是无用的。

It boils down to that the JAR file has got to be placed in any of the existing/default paths of the runtime classpath of the Java application in question, or that at least the path to the JAR file is to be added to the runtime classpath of the Java application in question.

归结为 JAR 文件必须放置在相关 Java 应用程序的运行时类路径的任何现有/默认路径中,或者至少将 JAR 文件的路径添加到运行时类路径中有问题的 Java 应用程序。

Since you're talking about a server and considering the fact that the CLASSPATHenvironment variable doesn't work, I'll assume that it's actually a webserver/appserver such as Apache Tomcat. If that's indeed true, you've got to either drop the JAR file in Tomcat/libfolder (if you use the container managed DataSourceapproach for a fast connection pool), or in webapp's WEB-INF/libfolder (if you use the poor man's Class#forName()approach to load the driver).

由于您在谈论服务器并考虑到CLASSPATH环境变量不起作用的事实,因此我假设它实际上是一个网络服务器/应用程序服务器,例如 Apache Tomcat。如果确实如此,您必须将 JAR 文件放到Tomcat/lib文件夹中(如果您使用容器管理的DataSource方法来实现快速连接池),或者放在 webapp 的WEB-INF/lib文件夹中(如果您使用穷人的Class#forName()方法来加载驱动程序)。

If it's not and it's actually a Java application which is to be executed as a JAR, then you've got to specify the classpath in MANIFEST.MFfile of the JAR in question. But if it's also not that and it is a loose .classfile, then you've got to specify the classpath in -cpor -classpathargument of javacommand. To save yourself from typing it again and again when executing it, just create a .shfile with the command.

如果不是,而且它实际上是一个要作为 JAR 执行的 Java 应用程序,那么您必须在MANIFEST.MF相关 JAR 的文件中指定类路径。但是如果它也不是那样并且它是一个松散的.class文件,那么你必须在命令的-cp-classpath参数中指定类路径java。为了避免在执行时一次又一次地输入它,只需.sh使用该命令创建一个文件即可。

回答by Tony

Try to do :

试着做 :

Class.forName("com.mysql.jdbc.Driver"); 

before getting your connection

在获得连接之前

You might also want to get a more recent version of the mysql jdbc driver. 5.0.8 is pretty old.

您可能还想获得更新版本的 mysql jdbc 驱动程序。5.0.8 已经很老了。

Take a look at JDBC Basicsit will give you some tips.

看看JDBC 基础知识,它会给你一些提示。

This tutorialmight help you as well.

教程也可能对您有所帮助。

回答by Mihai Toader

You seem to be working on a linux box. Make sure that your terminal launches a loginshell. For example the gnome-terminalapplication (probably what you are using if you are using a ubuntu or fedora box for development) has a checkbox under the profile preferences > Title and command menu called "run command as a login shell". Check that and make sure you also put this code in the ~/.bash_profile file at the end.

你似乎在一个 linux 机器上工作。确保您的终端启动了一个loginshell。例如,gnome-terminal应用程序(如果您使用 ubuntu 或 fedora box 进行开发,则可能是您正在使用的应用程序)在配置文件首选项 > 标题和命令菜单下有一个复选框,称为“作为登录外壳运行命令”。检查并确保您也将此代码放在 ~/.bash_profile 文件的最后。

export CLASSPATH=/path/mysql-connector-java-5.0.8-bin.jar:$CLASSPATH

But i would argue against doing this and doing a simple launch script for your application. Something similar to this:

但我会反对这样做并为您的应用程序做一个简单的启动脚本。类似的东西:

#!/bin/bash

Set CLASSPATH=CLASSPATH=/path/mysql-connector-java-5.0.8-bin.jar:$CLASSPATH

java -cp "$CLASSPATH" <your.main.application.Class>

make executable and use that to launch your application.

制作可执行文件并使用它来启动您的应用程序。

回答by user1318796

I know this is an old thread but it might help someone.

我知道这是一个旧线程,但它可能对某人有所帮助。

I have succeeded connecting to SQLite3 on Windows 7 with the proper CLASSPATH

我已使用正确的 CLASSPATH 成功连接到 Windows 7 上的 SQLite3

I use the JDBC driver sqlite-jdbc-3.7.2.

我使用 JDBC 驱动程序 sqlite-jdbc-3.7.2。

First I tried to copy the driver under the directory c:\Program Files\java\jre7\lib and then set the CLASSPATH variable, but that failed to recognize the line :

首先,我尝试将驱动程序复制到目录 c:\Program Files\java\jre7\lib 下,然后设置 CLASSPATH 变量,但无法识别该行:

Class.forName("org.sqlite.JDBC");

I think that's due to the Windows bug to have spaces in a folder name like "Program Files".

我认为这是由于 Windows 错误导致文件夹名称中有空格,例如“程序文件”。

So I copied the JAR file under C:\jbmorla and set the CLASSPATH to:

所以我复制了 C:\jbmorla 下的 JAR 文件并将 CLASSPATH 设置为:

.;c:\jbmorla\sqlite-jdbc-3.7.2.jar

Hope this helps

希望这可以帮助

回答by ManJan

For MAVENS projectsolution. You can directly modify the pom.xmlfile. Add the mysql-connector dependency to the pom.xml project file:

对于MAVES 项目解决方案。可以直接修改pom.xml文件。在 pom.xml 项目文件中添加 mysql-connector 依赖:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.40</version>
</dependency>