Java 将 jdbc 驱动程序添加到类路径

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

Adding jdbc driver to classpath

javahibernatejdbcclasspath

提问by Bohn

Ok I am on Win Vista and correctly set my MAVEN_HOME, JAVA_HOME stuff. but I do not have a class path yet. I have also installed MySQL. now I have opened a Hibernate book and at the very first pages it says "make sure the jdbc driver is in your classpath" . I have also downloaded some Zip file that is ConnectorJ or some name like that which is basically the mySql driver for java... but my problem for now is this sentence that I have no clue how to do it: "make sure the jdbc driver is in your classpath" would you please help me about this classpath thing?

好的,我在 Win Vista 上并正确设置了我的 MAVEN_HOME、JAVA_HOME 内容。但我还没有课程路径。我也安装了 MySQL。现在我打开了一本 Hibernate 书,在第一页上写着“确保 jdbc 驱动程序在您的类路径中”。我还下载了一些 Zip 文件,它是 ConnectorJ 或类似的名称,它基本上是 java 的 mySql 驱动程序......但我现在的问题是这句话,我不知道如何去做:“确保 jdbc 驱动程序在你的类路径中”你能帮我解决这个类路径的问题吗?

thanks

谢谢

采纳答案by Adeel Ansari

Here is a good tutorial regarding, setting the class path. Further you might like to read Managing the Java classpath (Windows).

这是一个关于设置类路径的好教程。此外,您可能还想阅读管理 Java 类路径 (Windows)

Having said that, you should not set the classpath for your driver in Windows environment variable. Instead, you should include that driver jarinside your IDE under project properties. But I noticed that you are actually using Maven. In this case you should look for the driver under Maven in order to fulfil that dependency. Maven will download the driver jar, if doesn't exist, and make it local.

话虽如此,您不应在 Windows 环境变量中为驱动程序设置类路径。相反,您应该jar在项目属性下的 IDE 中包含该驱动程序。但我注意到您实际上正在使用 Maven。在这种情况下,您应该在 Maven 下查找驱动程序以实现该依赖项。Maven 将下载驱动程序 jar,如果不存在,并将其设为本地。

In case, you are not using any IDE, then you can create a libdirectory and tell the compiler that all required jars are in there, at the time of compilation/execution. You can find HOW, in the former link given above.

如果您没有使用任何 IDE,那么您可以在编译/执行时创建一个lib目录并告诉编译器所有必需的jars 都在那里。您可以在上面给出的前一个链接中找到 HOW。

回答by Chris Nava

Since you are using Maven, you just need to put the JDBC driver as a dependency in your pom.xml file. Maven will add it to the classpath whenever it compiles/runs your application.

由于您使用的是 Maven,您只需要将 JDBC 驱动程序作为依赖项放在 pom.xml 文件中。Maven 会在编译/运行您的应用程序时将其添加到类路径中。

What you do when deploying your application is dependent on the technologies in use.

您在部署应用程序时所做的工作取决于所使用的技术。

  • If it's a command line application create a %CLASSPATH%variable or add the path to the jdbc.jar file using the java -cp {path\to\jdbc.jar}option.

  • If it's a web application, you'll need to package the driver jar in your .war/.ear/.sar (the maven assembly plugin can do this) or include it in the ./lib folder of the application container and declare it as scope=provided in maven.

  • 如果是命令行应用程序%CLASSPATH%,请使用该java -cp {path\to\jdbc.jar}选项创建一个变量或将路径添加到 jdbc.jar 文件。

  • 如果是web应用,你需要将驱动jar打包在你的.war/.ear/.sar中(maven程序集插件可以做到这一点)或者将它包含在应用容器的./lib文件夹中并声明它作为范围=在maven中提供。

回答by Mohit daga

you can directly connect database by following steps: 1) download mysql-connector-java 5.0.8 and extract the files. 2) then place the folder in program files. 3) then simply add this library on your project by right clicking on it. 4) and here you go. Run your app with db connectivity.

您可以通过以下步骤直接连接数据库: 1)下载mysql-connector-java 5.0.8并解压文件。2)然后将文件夹放在程序文件中。3)然后只需通过右键单击该库将其添加到您的项目中。4) 给你。使用数据库连接运行您的应用程序。

回答by user3371185

Steps to setup JDBC for Eclipse projects

为 Eclipse 项目设置 JDBC 的步骤

  1. Download JDBC zip archive from https://dev.mysql.com/downloads/connector/j/5.0.html
  2. Extract the file and copy the executable jar file to program files->Java->jdk->bin
  3. Right click on the project and select Buildpath->Add external archives->(Jar file)
  4. Compile the program
  1. https://dev.mysql.com/downloads/connector/j/5.0.html下载 JDBC zip 存档
  2. 解压文件并将可执行jar文件复制到 program files->Java->jdk->bin
  3. 右键单击项目并选择 Buildpath->Add external archives->(Jar file)
  4. 编译程序