0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

SpringのDB、h2 console

0
Posted at

最近勉強しているSpring Projectでh2というDBをつなげてみたので記録。

1. 接続

1.png
1.Projectのresources→application.propertiesに spring.h2.console.enabled = trueの入力で準備OK


3.png
2.Application.javaをShift+f10の入力または赤い四角内のボタンで起動させる


2.png
3. 下でh2を検索、赤い四角のようにjdbcで始まるjdbc urlを探してコピーしておく。
 このIDはSpringが起動するたび変わるものなので注意!


44.png
4.http://localhost:8080/h2-consoleに接続、3でコピーしたjdbc urlをPasteしてConnect


5.png
5.接続に成功した!





2. CRUD(Insert, Select, Update, Delete)

Springで作成したEntityファイルにはLong id, String title, String content項目があることを前提に
6.png
1. InsertとSelect:まずはCreate(Insert)とRead(Select)文の作成とその意味
(1)Insert:INSERT INTO article(id, title, content) VALUES (1, 'title1', 'content1');
      →DBテーブルのarticleの項目id, title, contentにそれぞれ1、title1、content1を付与
(2)Select:SELECT * FROM article;
      →articleに入れた値をすべて出力
 Insertを5回、Selectを入れ、Runすると、ちゃんとデータがテーブルに入ってることが確認できた


77.png
2. Update:Update文の作成と意味
 (1)Update:UPDATE article SET id=10 WHERE id=5;
        →DBテーブルのID5の値を10に変更。結果、5だったID値が10になった


8.png
3. Delete:Delete文の作成と意味
 (1)Delete:DELETE FROM article WHERE id=10;
        →DBテーブルのID10の値を削除。結果、ID10の行がなくなった


DBでの直接なCRUDは、いちいちコードを書かないといけないのが面倒なところもあるが、自分の入力したコードの結果がすぐに見えて意外と楽しい

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?