JDBC CLASSPATH 不工作

发布于 2024-10-12 11:57:35 字数 418 浏览 5 评论 0原文

我正在设置一个简单的 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 变量。

有人对如何设置 JDBC 有建议或修复吗?

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 $CLASSPATH to see if it exists, everything seems fine. But then when I open a new terminal and type echo $CLASSPATH it'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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

梦初启 2024-10-19 11:57:36

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

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

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

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

Forget the CLASSPATH environment variable. It's a big joke. It's ignored by pretty much everything else than a low-level java com.example.Foo command. It's ignored when you add the -jar argument. It's ignored when you add the -cp or -classpath argument. 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.

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.

Since you're talking about a server and considering the fact that the CLASSPATH environment 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/lib folder (if you use the container managed DataSource approach for a fast connection pool), or in webapp's WEB-INF/lib folder (if you use the poor man's Class#forName() approach to load the driver).

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.MF file of the JAR in question. But if it's also not that and it is a loose .class file, then you've got to specify the classpath in -cp or -classpath argument of java command. To save yourself from typing it again and again when executing it, just create a .sh file with the command.

〃安静 2024-10-19 11:57:36

尝试这样做:

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

在获得连接之前

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

看一下 JDBC 基础 它会给你一些提示。

这个教程也可能对您有帮助。

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.

Take a look at JDBC Basics it will give you some tips.

This tutorial might help you as well.

我很坚强 2024-10-19 11:57:36

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

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

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

#!/bin/bash

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

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

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

You seem to be working on a linux box. Make sure that your terminal launches a login shell. For example the gnome-terminal application (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.

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.

姜生凉生 2024-10-19 11:57:36

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

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

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

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

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

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

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

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

希望这会有所帮助

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

I use the JDBC driver 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 :

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

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

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

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

Hope this helps

雪花飘飘的天空 2024-10-19 11:57:36

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

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

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

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.40</version>
</dependency>
薯片软お妹 2024-10-19 11:57:36

在Ubuntu 20.04中:设置jdbc驱动程序的永久类路径

安装Mariadb驱动程序

sudo apt install libmariadb-java

将类路径放入.bashrc文件中

export CLASSPATH=/usr/share/java/mariadb-java-client-2.5.3.jar:$CLASSPATH

保存文件

In Ubuntu 20.04: Set permanent class path of jdbc driver

Install Mariadb driver

sudo apt install libmariadb-java

Put the class path in .bashrc file

export CLASSPATH=/usr/share/java/mariadb-java-client-2.5.3.jar:$CLASSPATH

Save the file

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文