JDBC CLASSPATH不起作用 [英] JDBC CLASSPATH Not Working

查看:196
本文介绍了JDBC CLASSPATH不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我服务器上运行的MySQL数据库建立一个简单的JDBC连接。我正在使用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变量。

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.

任何人都有建议或修复了如何设置JDBC的问题?

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

推荐答案

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

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.

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

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.

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

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).

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

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.

这篇关于JDBC CLASSPATH不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆