The detailed process of using Java to connect to cloud MySQL to build a database and build a table on Idea

Tip: After the article is written, the table of contents can be automatically generated. How to generate it can refer to the help document on the right

Article Directory

Table of contents

Article Directory

foreword

1. Database classification and configuration

        1.1 Choose a manufacturer's cloud service, including Alibaba Cloud, Tencent Cloud, Baidu Cloud, etc. Here I choose Tencent Cloud. First, search for Tencent Cloud service on the browser.

         1.2 After entering Tencent Cloud, hover the mouse over the product on the left to bring up the menu bar, then move the mouse to the database below, and finally slide to the right to click on the TDSQL-C MySQL version, or you can choose the cloud database MySQL, the former is the cluster version, the function richer

         1.3 After selecting the version, slide down to the bottom of the page, and you can see the package price. If you are a novice, the benefits are very great. No matter which MySQL it is, there is a 1.88 package. Because I have a small amount of data and do not require high database specifications and space, I choose the cheapest one.

         1.4 When purchasing a package, the system will prompt you to log in and authenticate with your real name, just follow the requirements and place an order. After logging in and placing an order, the avatar will appear on the home page. Click the console next to the avatar to enter a dark-style page. Enter TD in the search box above to find the entrance of the purchased package. If you bought the cloud service MySQL, you can Search for MySQL.

         1.5 After entering, you can see TDSQL-C, MySQL and other columns in the left menu bar, click the inverted triangle on the right of TDSQL-C to expand all functions, select the cluster list to see the page on the right, and then select the region (the region), select MySQL, and the cluster name can be customized.

         1.6 After setting the cluster name, click Account Management. The newly created cluster must first reset the password. The password must contain English capital letters and symbols. After setting the password, you can click the cluster details and slide to the bottom to view the MySQL host number and port Waiting for information.

2. Connect to the database on the idea

        2.1 Create a new project and select the type and dependencies

        2.2 Configure data source

        2.3 idea to connect to the database

        2.4 Create SQL file

        2.5 Run the SQL file



foreword

        Many friends who have just learned Java do not know how to connect to the database and other operations, so today I will share a very detailed connection tutorial


1. Database classification and configuration

        There are two types of databases: non-relational databases and relational databases. I won’t go into details about the differences and similarities. Interested friends can go to related blogs.

        The database I demonstrated today is MySQL , and it is cloud MySQL, that is, no matter what language you use, where the platform is, and whether it is native or not, you can operate the database. Without further ado, let's start the tutorial.

        1.1 Choose a manufacturer's cloud service, including Alibaba Cloud, Tencent Cloud, Baidu Cloud, etc. Here I choose Tencent Cloud. First, search for Tencent Cloud service on the browser.

         1.2 After entering Tencent Cloud, hover the mouse over  the product  on the left to bring up the menu bar, then move the mouse to  the database  below , and finally slide to the right to click on  the TDSQL-C MySQL version, or you can choose  the cloud database MySQL  , the former is the cluster version, the function richer

         1.3 After selecting the version, slide down to the bottom of the page, and you can see the package price. If you are a novice, the benefits are very great. No matter which MySQL it is, there is a 1.88 package. Because I have a small amount of data and do not require high database specifications and space, I choose the cheapest one.

 

         1.4 When purchasing a package, the system will prompt you to log in and authenticate with your real name, just follow the requirements and place an order. After logging in and placing an order, the avatar will appear on the home page. Click  the console  next  to the avatar  to enter a dark-style page.   Enter  TD  in the search box above to find the entrance of the purchased package. If you bought the cloud service MySQL, you can Search for MySQL.

 

1.5 After entering, you can see TDSQL-C , MySQL and other columns           in the left menu bar , click the inverted triangle  on the right side of TDSQL-C to expand all functions, select  the cluster list  to see the page on the right, and then select the region (the region), select  MySQL , and the cluster name can be customized.

         1.6 After setting the cluster name, click  Account Management . The newly created cluster must first reset the password . The password must contain English capital letters and symbols. After setting the password, you can click the cluster details and  slide to the bottom to view the host number and port of this MySQL. Waiting for information.

         Since then, our cloud MySQL has been fully configured, and then you can click on the  database management  on the web page to build databases and tables, add, delete, modify and query the database, and you can also use other platforms to remotely connect and other operations.

 

 

2. Connect to the database on the idea

        2.1 Create a new project and select the type and dependencies

Note: If the maven framework is not installed, you need to query the information and install it first

 Search for the three dependencies on the right in turn in the search bar, select them one by one, and create

         2.2 Configure data source

        After the project is successfully created, wait for the dependency download to complete, open the green leaf file under resources , and write the following configuration information. Including driver, data source address, data source access user name and password, among which // and: in the string is the host number, if it is local MySQL, you can write localhost or local IP, but now we write cloud MySQL host Number

        : The latter is the connection port, which can be seen in the cloud MySQL cluster details

 

The configuration code is as follows (example):

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://----主机号---:**端口号**/blog?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false
spring.datasource.username=root
spring.datasource.password=111111

        2.3 idea to connect to the database

        You can see three functions on the right side of the idea. We click on  the database  . If the Chinese plug-in is not installed, it is Database. 

Then follow the steps to add a data source, select MySQL, and then continue to fill in the host number, port and other information. After downloading the driver, click the connection test. After the test is successful, you can click Apply

 

 

 

 

         2.4 Create SQL file

        Create a sql folder and two sql files under the resources folder, as shown in the figure

 

 

The Schema file code is as follows (example):

DROP DATABASE IF EXISTS `Blog`;
create  database Blog;

Use Blog;

DROP TABLE IF EXISTS `article`;
CREATE TABLE `article`
(
    `id`          int(11)                   NOT NULL AUTO_INCREMENT COMMENT '主键',
    `user_id`     int(11)                   NOT NULL COMMENT '作者 ID',
    `title`       varchar(100)              NOT NULL COMMENT '文章标题',
    `summary`     varchar(200) DEFAULT NULL COMMENT '文章概要',
    `read_count`  int(11) unsigned zerofill NOT NULL COMMENT '阅读读数',
    `create_time` datetime                  NOT NULL COMMENT '创建时间',
    `update_time` datetime                  NOT NULL COMMENT '最后修改时间',
    PRIMARY KEY (`id`)
) ENGINE = InnoDB
  AUTO_INCREMENT = 1
  DEFAULT CHARSET = utf8mb4;

The data file code is as follows (example):

INSERT INTO `article` VALUES ('1','2101','SpringBoot 核心注解',
                              '核心注解的主要作用','00000008976','2023-01-16 12:11:12','2023-01-16 12:11:19');

INSERT INTO `article` VALUES ('2','356752','JVM 调优',
                              'HotSpot 虚拟机详解','00000000026','2023-01-16 12:15:27','2023-01-16 12:15:30');

        2.5 Run the SQL file

        When creating the SQL file, the system will prompt you to change the dialect to MySQL as required

        After changing the dialect, click to run

        After the two files are successfully run, you can see that MySQL has an extra blog database and article table, and the data has been inserted into the table. Since then, Java has connected to the database and inserted the data.          


To sum up
        , the above is the content shared today. This article only introduces the simple operation after idea connects to the cloud MySQL. It does not involve the use of Java statements to add, delete, check and modify database data. It will be concluded in depth later. If this blog is helpful to friends If you can help, welcome to appreciate, leave a message and forward it!

Guess you like

Origin blog.csdn.net/qq_51294997/article/details/131018067