質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
MySQL

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Spring

Spring Framework は、Javaプラットフォーム向けのオープンソースアプリケーションフレームワークです。 Java Platform上に、 Web ベースのアプリケーションを設計するための拡張機能が数多く用意されています。

MyBatis

MyBatisはJavaや.NET Frameworkでなどで使用できる、SQL文や、ストアドプロシージャをオブジェクトと紐付けるO/Rマッピングフレームワークです。

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

Q&A

解決済

2回答

7201閲覧

spring bootとMySQLをMyBatisで接続させたい

youth_k0228

総合スコア18

MySQL

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Spring

Spring Framework は、Javaプラットフォーム向けのオープンソースアプリケーションフレームワークです。 Java Platform上に、 Web ベースのアプリケーションを設計するための拡張機能が数多く用意されています。

MyBatis

MyBatisはJavaや.NET Frameworkでなどで使用できる、SQL文や、ストアドプロシージャをオブジェクトと紐付けるO/Rマッピングフレームワークです。

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

0グッド

0クリップ

投稿2021/07/06 01:15

こんにちは。
こちらのサイトを参考にspring bootとMySQLの接続を試みましたが、「Failed to obtain JDBC Connection」のエラーが発生してしまい、解消できなかった為、ご教示をお願いしたく質問させていただきます。
状況としては、「spring bootをMyBatisを用いてMySQLに接続させる」というもので、DBに格納されているIDを検索し、該当のデータを引っ張り出すというものです。
検索画面は作成できましたが、IDを入力して検索ボタンを押すと、「mysqlが接続できない」旨のエラーが発生してしまいました。
JDBCドライバを参照していても、こちらのエラーが発生してまっている状況です。

consoleerror

12021-07-06 09:55:13.679 ERROR 20836 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]  : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: 2### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure 3 4The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. 5### The error may exist in com/example/demo/repository/UserMapper.xml 6### The error may involve com.example.demo.repository.UserMapper.search 7### The error occurred while executing a query 8### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure 9 10The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.] with root cause

UserController

1package com.example.demo.controller; 2 3import java.util.List; 4 5import org.springframework.beans.factory.annotation.Autowired; 6import org.springframework.stereotype.Controller; 7import org.springframework.ui.Model; 8import org.springframework.web.bind.annotation.GetMapping; 9import org.springframework.web.bind.annotation.ModelAttribute; 10import org.springframework.web.bind.annotation.RequestMapping; 11import org.springframework.web.bind.annotation.RequestMethod; 12 13import com.example.demo.dto.UserSearchRequest; 14import com.example.demo.entity.User; 15import com.example.demo.service.UserService; 16 17//ユーザー情報コントローラ 18@Controller 19public class UserController{ 20 //ユーザー情報Service 21 @Autowired 22 UserService userService; 23 24 /** 25 * ユーザー情報一覧画面を表示 26 * @param model Model 27 * @return ユーザー情報一覧画面のHTML 28 */ 29 30 @RequestMapping(value = "/user/list", method = RequestMethod.GET) 31 public String displayList(Model model) { 32 List<User> userlist=userService.searchAll(); 33 model.addAttribute("userlist",userlist); 34 return "user/list"; 35 36 } 37 38 /** 39 * ユーザー情報情報検索画面を表示 40 * @param model Model 41 * @return ユーザー情報一覧画面 42 */ 43 @GetMapping(value = "/user/search") 44 public String displaySearch(Model model) { 45 return "user/search"; 46 } 47 48 /** 49 * ユーザー情報検索 50 * @param userSearchRequest リクエストデータ 51 * @param model Model 52 * @return ユーザー情報一覧画面 53 */ 54 @RequestMapping(value = "/user/id_search",method = RequestMethod.POST) 55 public String search(@ModelAttribute UserSearchRequest userSearchRequest, Model model) { 56 User user = userService.search(userSearchRequest); 57 System.out.println("テスト"); 58 model.addAttribute("userinfo",user); 59 System.out.println("試験"); 60 return "user/search"; 61 } 62}

UserService

1package com.example.demo.service; 2 3import java.util.List; 4 5import org.springframework.beans.factory.annotation.Autowired; 6import org.springframework.stereotype.Service; 7 8import com.example.demo.dto.UserSearchRequest; 9import com.example.demo.entity.User; 10import com.example.demo.repository.UserMapper; 11import com.example.demo.repository.UserRepository; 12 13 14//ユーザー情報Service 15 16@Service 17public class UserService { 18 //ユーザー情報Repository 19 20 UserRepository userRepository; 21 @Autowired 22 private UserMapper userMapper; 23 24 public List<User> searchAll(){ 25 //ユーザーテーブル内を全て検索する 26 return userRepository.findAll(); 27 } 28 /** 29 * ユーザー情報検索 30 * @param userSearchRequest リクエストデータ 31 * @return 検索結果 32 */ 33 public User search(UserSearchRequest userSearchRequest) { 34 System.out.println("test"); 35 return userMapper.search(userSearchRequest); 36 } 37} 38

applicationpropeties

1spring.datasource.url=jdbc:mysql://localhost:8080/sampledb?characterEncoding=UTF-8&socketTimeout=30000 2spring.datasource.username=root 3spring.datasource.password=chhagane 4spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver 5spring.jpa.open-in-view= true 6spring.datasource.sql-script-encoding=utf-8 7spring.jpa.database=MYSQL 8#設定されていない場合はrootで制御 9logging.level.root=INFO 10logging.level.org.springframework.web=DEBUG 11logging.level.org.hibernate=ERROR 12#ファイルパス or ファイル名のどちらかを設定 13logging.file.name=MyLog/myLog.log 14logging.file.path=MyLog 15# Web関連のロガーのログレベルをDEBUG or TRACEへ 16logging.level.web=trace 17 18# Spring MVCへのリクエスト/レスポンスログの詳細情報出力を有効化 19spring.mvc.log-request-details=true

以上、拙い質問で大変恐縮ですが、ご教示の程宜しくお願い致します。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

自己解決

以下の方法で解決致しました。

  1. application.propetiesからタイムゾーンの記述を削除
  2. mysqlのポート番号だけでなく、サーバーのポート番号も指定する。

投稿2021/07/08 01:39

youth_k0228

総合スコア18

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

MySQLにはまったく詳しくありませんが、JDBC接続に8080ポートはなかなか使わないような気がします。

spring.datasource.url=jdbc:mysql://localhost:8080/sampledb?...

see: https://spring.pleiades.io/spring-boot/docs/current/reference/html/application-properties.html#application-properties.server.server.port

server.port | サーバー HTTP ポート。| 8080


こちらでは、3306ポートなそうです。

投稿2021/07/06 08:33

編集2021/07/06 08:53
shiketa

総合スコア3971

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

youth_k0228

2021/07/07 04:58

ご回答ありがとうございます。 こちらはMySQLのポート番号を指定するのですね。 自分のMySQLのポート番号を調べたところ3306番でしたので試してみましたが、同様のエラーが発生してしいました...。
shiketa

2021/07/07 05:14 編集

> 同様の ということは、質問にあるエラーとは異なる、ということですよね。であればそれをすべて晒してもらう以外には、あなた以外には何が起きているかわからないとおもいますけど。 > こちらはMySQLのポート番号を指定するのですね。 この段階だとすると、application.propertiesの設定をすべて疑ったほうがいいですね。 Spring/MyBatisどうのこうのやるまえに、小さなプログラムを用意して、DriverManager#getConnection()で接続できるかどうかを確認してみてはどうでしょうか。 https://docs.oracle.com/javase/jp/8/docs/api/java/sql/DriverManager.html#getConnection-java.lang.String-java.lang.String-java.lang.String-
youth_k0228

2021/07/08 01:38

ありがとうございます。 別の方法で解決致しましたが、application.propetiesについて見直す機会になりました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問