SpringのJdbcTemplateでOracleにUPDATE発行してみた
SpringのJdbcTemplateを使用してUPDATEを実行してみます。
この記事で使うバージョンは以下になります。
- Windows 10 Home
- Java11
- Spring Tool Suite 4.6.2
- Oracle 11gXE
JdbcTemplateUpdateSampleApplication.java
Spring Starter Projectでプロジェクト作成した時に自動生成されるものです。
1 2 3 4 5 6 7 8 9 | package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class JdbcTemplateUpdateSampleApplication { public static void main(String[] args) { SpringApplication.run(JdbcTemplateUpdateSampleApplication. class , args); } } |
使用するテーブル
以下のテーブルとデータをOracleに作っておきましょう。
1 2 | CREATE TABLE animal (animal_id CHAR (3),animal_name VARCHAR2(20)); insert into animal values ( '001' , 'うさぎ' ); |
SQL> select animal_id,animal_name from animal; ANIMAL_ID ANIMAL_NAME ---------- ------------------ 001 うさぎ
JdbcTemplateUpdate.java
14~15行目でJdbcTemplateを使用するため依存性の注入をしています。
28行目でupdateメソッドを使用してUPDATEを実行します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | package com.example.demo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PostMapping; import com.example.demo.model.Animal; @Controller public class JdbcTemplateUpdate { @Autowired JdbcTemplate jdbcTemplate; public String init() { return "index" ; } @PostMapping ( "/update" ) public String updateAnimal(Animal animal,Model model) { String sqlText = "UPDATE ANIMAL " + "SET ANIMAL_NAME = ? " + "WHERE ANIMAL_ID = ?" ; jdbcTemplate.update(sqlText,animal.getAnimalName(),animal.getAnimalId()); model.addAttribute( "resultMessage" , "UPDATE SUCCESS!!!" ); return "index" ; } } |
Animal.java
テーブル定義と同じカラムのフィールドを定義してゲッターセッターを用意します。画面側のindex.htmlで「th:object=”${animal}”」で定義して入力した値を取り出せるようにします。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | package com.example.demo.model; public class Animal { private String animalId; private String animalName; public String getAnimalId() { return animalId; } public void setAnimalId(String animalId) { this .animalId = animalId; } public String getAnimalName() { return animalName; } public void setAnimalName(String animalName) { this .animalName = animalName; } } |
index.html
http://localhost:8888/にアクセスして変更したID(今回は「001」)と変更後の動物名を入力してください(今回は「いるか」)。UPDATEに成功したら画面に「UPDATE Success!!!」が表示されます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <!DOCTYPE html> <head> <meta charset= "UTF-8" > <link rel= "stylesheet" href= "/css/style.css" > <title>JdbcTemplate UPDATE Sample</title> </head> <body> <h1>UPDATE sample</h1> <p th:text= "${resultMessage}" ></p> <form method= "POST" action= "/update" th:object= "${animal}" > 更新したいIDを入力してください。<input type= "text" name= "animalId" ><br> 変更後の動物名を入力してください。<input type= "text" name= "animalName" ><br> <input type= "submit" value= "送信" > </form> </body> </html> |
UPDATEが完了しているかSQL*Plusを使用して確認してみましょう。
ANIMAL_ID ANIMAL_NAME ---------- ---------------------------------------- 001 いるか
動物名が「うさぎ」から「いるか」にUPDATEされていますね\(^o^)/
[/java]
application.properties
ユーザネームとパスワードはご自身のを設定してください。ポートは空いてるものなら何でもいいです。自分の場合、デフォルトの8080を別アプリに割り当ててたので8888にしてます。
1 2 3 4 5 | server.port= 8888 spring.datasource.url=jdbc:oracle:thin: @localhost : 1521 :XE spring.datasource.username={username} spring.datasource.password={password} spring.datasource.driverClassName=oracle.jdbc.OracleDriver |
pom.xml
今回のプロジェクトのpomは、以下になります。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | <?xml version= "1.0" encoding= "UTF-8" ?> <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion> 4.0 . 0 </modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version> 2.4 . 3 </version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>SpringTest</artifactId> <version> 0.0 . 1 -SNAPSHOT</version> <name>JdbcTemplateDDLSample</name> <description>Demo project for Spring Boot</description> <properties> <java.version> 11 </java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional> true </optional> </dependency> <dependency> <groupId>com.oracle.database.jdbc</groupId> <artifactId>ojdbc8</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> |
コメント