简体   繁体   English

当 mysql 连接器位于同一项目中时,如何从命令行运行连接数据库的 Java 程序?

[英]How to run a database connected Java program from the command line, when the mysql connector is located in the same project?

I have written Java code that connects to a database, all works great in IntelliJ, as the connector is configured via the IDE.我编写了连接到数据库的 Java 代码,在 IntelliJ 中都运行良好,因为连接器是通过 IDE 配置的。 However, when I try to run the same code from the commandline, I run into some problems.但是,当我尝试从命令行运行相同的代码时,我遇到了一些问题。

Firstly, I used the command:首先,我使用了命令:

javac -encoding UTF8 -cp mysql/mysql-connector-java-8.0.19.jar Main.java Logic.java javac -encoding UTF8 -cp mysql/mysql-connector-java-8.0.19.jar Main.java Logic.java

for compiling my Java files, (I've tried the same things with just javac -encoding UTF8 *.java as well) and then I have tried to run my Java program.用于编译我的 Java 文件,(我也尝试过同样的事情,只使用javac -encoding UTF8 *.java ),然后我尝试运行我的 Java 程序。

If I use "java Main.java" on the commandline, I get an error:如果我在命令行上使用“java Main.java”,我会收到一个错误:

java.sql.SQLException: No suitable driver found for jdbc:mysql:***. java.sql.SQLException: 找不到适合 jdbc:mysql:*** 的驱动程序。

However, when I try to run java -cp mysql/mysql-connector-java-8.0.19.jar Main.java , I get the error:但是,当我尝试运行java -cp mysql/mysql-connector-java-8.0.19.jar Main.java ,出现错误:

Main.java:10: error: package Logic does not exist Main.java:10: 错误:包逻辑不存在

I have tried to get it to work by moving the entire jar to the same folder as my files and also tried extracting the Driver.class file from the jar, yet I still can not get it to work.我试图通过将整个 jar 移动到与我的文件相同的文件夹来使其工作,并尝试从 jar 中提取 Driver.class 文件,但我仍然无法让它工作。

I realise this was not probably the most efficient way to connect a database anyways, but this is where I am at.我意识到这可能不是连接数据库的最有效方法,但这就是我所处的位置。 Sorry if it is a stupid question, but I have been at it for hours.对不起,如果这是一个愚蠢的问题,但我已经研究了几个小时。 Thanks!谢谢!

IDEs are very helpful to provide a simple "sandbox" where you can work visually on the code, build it, run it, and debug it fast. IDE 非常有助于提供一个简单的“沙箱”,您可以在其中直观地处理代码、构建、运行和快速调试。

However, when you want to publish or deploy your application so it can be run outside your sandbox, you probably need to perform a "standard build".但是,当您想要发布部署您的应用程序以便它可以在您的沙箱之外运行时,您可能需要执行“标准构建”。 This standard build will produce an application that can be run anywhere.此标准构建将生成可在任何地方运行的应用程序。

Solution?解决方案? Use any build tool, such as Maven, Ant, or Gradle.使用任何构建工具,例如 Maven、Ant 或 Gradle。 You'll need to learn the basics of the one you choose.你需要学习你选择的那个的基础知识。 There are plenty of examples online.网上有很多例子。 Also, pretty much all IDEs provide integration with those three I mention.此外,几乎所有 IDE 都提供与我提到的这三个集成。

You'll need to decide if you want to include the JDBC driver inside the application itself or as an external dependency, as well.您需要决定是要将 JDBC 驱动程序包含在应用程序本身中还是作为外部依赖项包含在内。 Options, options, options.选项,选项,选项。

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

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