栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

03-01:数据准备

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

03-01:数据准备

在学习Java程序访问数据库之前,我们先以MySQL为例,做好准备工作,如创建数据库,创建数据表和初始化数据等,以确保后续学习任务能够顺利有序进行。

1、创建数据库

1.运行cmd,键入mysql客户端命令登录MySQL。

Microsoft Windows [版本 10.0.19042.1237]
(c) Microsoft Corporation。保留所有权利。

C:WINDOWSsystem32>mysql  -u root  -p
Enter password: ******
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 1091
Server version: 8.0.22 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

mysql>

2.在MySQL中创建一个名称为springbootdata的数据库。

# 创建数据库
CREATE DATAbase springbootdata character SET utf8;
2、创建数据表

1.选择当前数据库为springbootdata。

# 选择使用数据库
USE springbootdata;

2.在该数据库中创建两个表t_article和t_comment。

# 创建文章表t_article
DROp TABLE IF EXISTS t_article;

CREATE TABLE t_article (
  id int(11) NOT NULL AUTO_INCREMENT COMMENT '文章id',
  title varchar(50) NOT NULL COMMENT '文章标题',
  content longtext COMMENT '文章具体内容',
  PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

# 创建评论表t_comment
DROP TABLE IF EXISTS t_comment;

CREATE TABLE t_comment (
  id int(11) NOT NULL AUTO_INCREMENT COMMENT '评论id',
  article_id int(11) NOT NULL COMMENT '关联的文章id',
  content longtext COMMENT '评论内容',
  author varchar(200) DEFAULT  NULL COMMENT '评论用户用户名',
  PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

3、初始化数据

1.分别向向t_article和t_comment表插入数据。

#向 t_article 表插入数据
INSERT INTO t_article VALUES ('1', 'SSM框架基础教程', '从入门到精通...');
INSERT INTO t_article VALUES ('2', 'SPringBoot框架基础教程', '从入门到精通...');

#向 t_comment 表插入数据
INSERT INTO t_comment VALUES ('1', '1', '内容很详细', '明明');
INSERT INTO t_comment VALUES ('2', '1', '已三连', '红红');
INSERT INTO t_comment VALUES ('3', '1', '很不错', '小王');
INSERT INTO t_comment VALUES ('4', '1', '赞一个', '东东');
INSERT INTO t_comment VALUES ('5', '2', '内容全面', '方方');

2.查询表中数据。

mysql> select  *  from  t_article  ;
+----+------------------------+-----------------+
| id | title                  | content         |
+----+------------------------+-----------------+
|  1 | SSM框架基础教程        | 从入门到精通... |
|  2 | SPringBoot框架基础教程 | 从入门到精通... |
+----+------------------------+-----------------+
2 rows in set (0.01 sec)

mysql> select  *  from  t_comment ;
+----+------------+------------+--------+
| id | article_id | content    | author |
+----+------------+------------+--------+
|  1 |          1 | 内容很详细 | 明明   |
|  2 |          1 | 已三连     | 红红   |
|  3 |          1 | 很不错     | 小王   |
|  4 |          1 | 赞一个     | 东东   |
|  5 |          2 | 内容全面   | 方方   |
+----+------------+------------+--------+
5 rows in set (0.00 sec)
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/318033.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号