简体   繁体   中英

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. How do I do that ? I use the IntelliJ IDE if thats relevant.

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.

Any help is appreciated.

You might follow an example such as this one:

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

But, you might want to consider using Hibernate unless you're doing only a couple basic queries.

请参阅使用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.

Which version of IntelliJ?

You need to add the MySQL Connector-J JAR to your project dependencies.

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. That puts it in your CLASSPATH.

If you still have problems, it means you don't have the driver class name right. It should be com.mysql.jdbc.Driver.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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