Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the connection of Java Project to MySQL through IntelliJ IDEA Software

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "how to connect Java projects to MySQL through IntelliJ IDEA software", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this article "how to connect Java projects through IntelliJ IDEA software".

Steps to install the MySQL driver

Tools:

IntelliJ IDEA

MySQL8.0.18

MySQL connection driver: mysql-connector-java-8.0.23.jar

Download driver is required to connect to MySQL

Download MySQL connection driver

Select Platform Independent

Click Download

Click No thanks, just start my download.

Start downloading automatically

Extract ZIP and copy out the mysql-connector-java-8.0.23.jar file (choose the path you want to put in)

Open IDEA

(due to the installation of the Chinese package, the English version of the user can operate according to the icon.)

Select the menu bar file, and then select the project structure

Select Libraries

Click +

Select a mysql-connector-java-8.0.23.jar file

Added successfully

Click Modules

Click Dependencies, then click +

Select JARs or directories...

Select a mysql-connector-java-8.0.23.jar file

Tick

The external library has already displayed mysql-connector-java-8.0.23.jar

End of adding driver step!

Let's start connecting to the MySQL.

Guide package operation

Import java.sql.*

Define JDBC driver name, database URL, user name, password

MySQL version below 8.0-JDBC driver name and database URL

Static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost:3306/RUNOOB"

MySQL 8.0 +-JDBC driver name and database URL

Static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost:3306/RUNOOB?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC"

Here is an illustration of the database URL

When java connects to mysql database through JDBC or other tools, connecting URL with certain parameters can solve a lot of problems.

The URL format without parameters is as follows:

Jdbc:mysql://localhost:3306/ database name

Useful parameters are as follows:

1. To solve the problem of data garbled, add parameter: characterEncoding=utf-8

two。 Turn on the switch for mysql to store bulk data

If you do not turn on the switch to store bulk data, it will be very slow to store data. You need to add a parameter to URL: rewriteBatchedStatement = true

After adding these two parameters, the complete format of url is:

Jdbc:mysql://localhost:3306/ database name? characterEncoding=utf-8&rewriteBatchedStatement=true

The user name and password of the database need to be set according to your own settings

Static final String USER = "root"; static final String PASS = "root"

Connect to the database

Database:

The following example uses JDBC to connect to the MySQL database. Note that some data such as user name and password need to be configured according to your development environment:

Package MySQLTest;import java.sql.*;public class Test {/ / MySQL version 8.0 or later-JDBC driver name and database URL static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost:3306/lcz?characterEncoding=utf-8&rewriteBatchedStatement=true"; / / user name and password of the database, according to your own settings static final String USER = "root"; static final String PASS = "root" Public static void main (String [] args) {Connection conn = null; Statement stmt = null; try {/ / register the JDBC driver Class.forName (JDBC_DRIVER); / / Open the link System.out.println ("connect to the database..."); conn = DriverManager.getConnection (DB_URL,USER,PASS) / / execute query System.out.println ("instantiate Statement object..."); stmt = conn.createStatement (); String sql; sql = "SELECT * FROM lcztest"; ResultSet rs = stmt.executeQuery (sql); / / expand the result set database while (rs.next ()) {/ / retrieve int id = rs.getInt ("id") by field String name = rs.getString ("name"); String age = rs.getString ("age"); / / output data System.out.print ("ID:" + id); System.out.print (", name:" + name); System.out.print (", age:" + age); System.out.print (") } / / close rs.close (); stmt.close (); conn.close ();} catch (SQLException se) {/ / handle JDBC error se.printStackTrace ();} catch (Exception e) {/ / handle Class.forName error e.printStackTrace () } finally {/ / close the resource try {if (stmtasking null) stmt.close ();} catch (SQLException se2) {} / / do nothing try {if (connected database null) conn.close ();} catch (SQLException se) {se.printStackTrace ();}} System.out.println ("connection to the database is over!") }} the above is about the content of this article on "how to connect Java projects to MySQL through IntelliJ IDEA software". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about related knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report