简体   繁体   English

如何使用Java与MySQL交互?

[英]How do I use interact with MySQL using Java?

I'm assuming I need to use Connector\\J and JDBC to achieve this, but I can't seem to 'install' Connector\\J using the CLASSPATH thing. 我假设我需要使用Connector \\ J和JDBC来实现此目的,但是我似乎无法使用CLASSPATH来“安装” Connector \\ J。 How do I do that ? 我怎么做 ? I use the IntelliJ IDE if thats relevant. 如果可以的话,我使用IntelliJ IDE。

I'm looking for a way to talk to a mysql database and execute and print out a few basic queries but I'm not getting anywhere because I can't even talk to the database. 我正在寻找一种与mysql数据库对话并执行并打印一些基本查询的方法,但是我什么也没走,因为我什至无法与数据库对话。

Any help is appreciated. 任何帮助表示赞赏。

You might follow an example such as this one: 您可能会遵循以下示例:

http://www.kitebird.com/articles/jdbc.html http://www.kitebird.com/articles/jdbc.html

But, you might want to consider using Hibernate unless you're doing only a couple basic queries. 但是,除非只做几个基本查询,否则您可能要考虑使用Hibernate

请参阅使用MySQL和Java -从第一个链接谷歌

I like this tutorial because it's very detailed and thorough. 我喜欢本教程,因为它非常详细和透彻。 Pretty much every step from downloading and installing MySQL and Connector/J to writing and running queries is covered. 涵盖了从下载和安装MySQL和Connector / J到编写和运行查询的几乎所有步骤。

Which version of IntelliJ? 哪个版本的IntelliJ?

You need to add the MySQL Connector-J JAR to your project dependencies. 您需要将MySQL Connector-J JAR添加到项目依赖项中。

Open your IntelliJ settings (the "wrench" in the top menu bar), go to "Libraries", click on "Attach Classes", and add the MySQL Connector-J JAR that you downloaded above. 打开IntelliJ设置(顶部菜单栏中的“扳手”),转到“库”,单击“附加类”,然后添加您在上面下载的MySQL Connector-J JAR。 That puts it in your CLASSPATH. 那把它放在你的CLASSPATH中。

If you still have problems, it means you don't have the driver class name right. 如果仍然有问题,则意味着您没有正确的驱动程序类名称。 It should be com.mysql.jdbc.Driver. 它应该是com.mysql.jdbc.Driver。

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

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