简体   繁体   English

JDBC 类路径不起作用

[英]JDBC CLASSPATH Not Working

I'm setting up a simple JDBC connection to my working MySQL database on my server.我正在我的服务器上建立一个简单的 JDBC 连接到我的工作 MySQL 数据库。 I'm using the Connector-J provided by MySQL.我正在使用 MySQL 提供的 Connector-J。 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.根据他们的文档,我假设创建 CLASSPATH 变量以指向 mysql-connector-java-5.0.8-bin.jar 所在的目录。 I used export set CLASSPATH=/path/mysql-connector-java-5.0.8-bin.jar:$CLASSPATH .我使用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.当我输入echo $CLASSPATH以查看它是否存在时,一切似乎都很好。 But then when I open a new terminal and type echo $CLASSPATH it's no longer there.但是当我打开一个新终端并输入echo $CLASSPATH它不再存在。 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.我认为这是我的 Java 服务器无法连接到 JDBC 的主要原因,因为它没有保存我设置的 CLASSPATH 变量。

Anyone got suggestions or fixes on how to set up JDBC in the first place?任何人都有关于如何首先设置 JDBC 的建议或修复?

Forget the CLASSPATH environment variable. 忘记CLASSPATH环境变量。 It's a big joke. 这是个大笑话。 It's ignored by pretty much everything else than a low-level java com.example.Foo command. 除了低级别的java com.example.Foo命令之外,它几乎被其他所有东西忽略了。 It's ignored when you add the -jar argument. 添加-jar参数时会忽略它。 It's ignored when you add the -cp or -classpath argument. 添加-cp-classpath参数时会忽略它。 It's ignored by IDE's and web/application servers. IDE和Web /应用程序服务器忽略了它。 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. 归结为JAR文件必须放在有问题的Java应用程序的运行时类路径的任何现有/缺省路径中,或者至少要将JAR文件的路径添加到运行时类路径中有问题的Java应用程序。

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. 由于您正在讨论服务器并考虑到CLASSPATH环境变量不起作用的事实,我将假设它实际上是一个Web服务器/应用程序服务器,例如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). 如果确实如此,您必须删除Tomcat/lib文件夹中的JAR文件(如果您使用容器管理的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.MF file of the JAR in question. 如果不是,并且它实际上是要作为JAR执行的Java应用程序,那么您必须在相关JAR的MANIFEST.MF文件中指定类路径。 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. 但是如果它也不是那个并且它是一个松散的.class文件,那么你必须在java命令的-cp-classpath参数中指定classpath。 To save yourself from typing it again and again when executing it, just create a .sh file with the command. 为了避免在执行时反复输入,只需使用该命令创建一个.sh文件。

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. 您可能还想获得更新版本的mysql jdbc驱动程序。 5.0.8 is pretty old. 5.0.8很老了。

Take a look at JDBC Basics it will give you some tips. 看看JDBC Basics它会给你一些提示。

This tutorial might help you as well. 教程也可以为您提供帮助。

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错误在文件夹名称中有空格,如“Program Files”。

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 希望这可以帮助

For MAVENS project solution. 对于MAVENS项目解决方案。 You can directly modify the pom.xml file. 您可以直接修改pom.xml文件。 Add the mysql-connector dependency to the pom.xml project file: 将mysql-connector依赖项添加到pom.xml项目文件:

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

You seem to be working on a linux box. 你好像在使用linux机箱。 Make sure that your terminal launches a login shell. 确保终端启动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". 例如, gnome-terminal应用程序(可能是您使用ubuntu或fedora盒进行开发时使用的)在配置文件首选项>标题和命令菜单下有一个复选框,名为“run command as login shell”。 Check that and make sure you also put this code in the ~/.bash_profile file at the end. 检查一下,确保最后将此代码放在〜/ .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. make可执行文件并使用它来启动您的应用程序。

In Ubuntu 20.04: Set permanent class path of jdbc driver在 Ubuntu 20.04 中:设置 jdbc 驱动程序的永久类路径

Install Mariadb driver安装 Mariadb 驱动

sudo apt install libmariadb-java

Put the class path in .bashrc file将类路径放在.bashrc文件中

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

Save the file保存文件

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM