๐–„๐•บ๐ŸŒŽ๐•ฟ๐•ฝ๐•บยฅ

๐–„๐•บ๐ŸŒŽ๐•ฟ๐•ฝ๐•บยฅ

๐•ด ๐–‰๐–” ๐–’๐–†๐–Œ๐–Ž๐–ˆ
github

Implementing Java Connection to MySQL Database and JDBC Interaction Example Based on IDEA from Scratch

Objective#

Implement Java and database interaction, including configuring SQL environment, Java libraries, and implementing a simple example.

Environment Configuration#

Host Configuration: IntelliJ IDEA, SDK18, Windows11

Download:
Connector/J
System Selection: Platform Independent
connectorJ

MySQL Community 8.0
Note: Choose mysql-installer-community, not mysql-installer-web-community
MySQLCommunity

After downloading MySQLCommunity, install only the server. Remember the information and set up the database account (in this example, <username>, <password>). The image below shows a successful installation, which may differ from the installation process.

mysqlInstaller

IDEA Configuration#

MySQL - Create New Data Source for Connection#

After creating a new project, go to the menu bar, select View -> Tool Windows -> Database. Create a new data source and download the MySQL driver. Take note of the class name (in the image, it is com.mysql.cj.jdbc.Driver).
mysqldriver

For the data source, you can use the database account used during MySQL installation. Take note of the URL address.
localhost
Test the connection at the bottom to ensure it is successful before proceeding to the next step.

Import Connector/J into Project Library#

Go to File -> Project Structure -> Libraries. Add the downloaded Connector/J jar file (after extraction) and click OK.
projectStructure

Implementation of Interaction#

Database#

Go to the console and enter MySQL commands. You can refer to the MySQL Tutorial: MySQL Database Learning Compendium (From Beginner to Master).

sqlkongzhitai

Enter the following code:

After running the code, you should see the creation completed.

Java Class#

Create a new Java class. The code is as follows:

Output:

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.