お世話になっております。
現在あるエラーで悩んでおります。
ご教示いただけると幸いです。
【概要】
・「Spring JDBC」を利用して「MySQL」のデータにアクセスしようと試みております。
【問題点】
・MySQLにアクセスできません。
【開発環境】
・Spring Tool Suite ver3.91
・JDK8
【コード】
「application.properties」 # --- Database access settings --- spring.datasource.url=jdbc:mysql://IPアドレス:3306/test_db?characterEncoding=UTF-8 spring.datasource.username=adminuser spring.datasource.password=password spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.initialize=false spring.datasource.sqlScriptEncoding=UTF-8
public class Controller { @Autowired private JdbcTemplate jdbcTemplate; 「コントローラー」 // test.html画面呼び出し用 @RequestMapping(value="/test") public ModelAndView test(ModelAndView mav) { List<Map<String, Object>> list = jdbcTemplate.queryForList("select * from user_tbl"); System.out.println(list.get(0).get("user_id")); mav.addObject("thymeleafTest",list.get(0).get("user_id")); // 対象テンプレートの呼び出し mav.setViewName("/test"); return mav; } }
【エラー内容】
※上記test.html呼び出し時にuser_idの1番目を取得したいが
There was an unexpected error (type=Internal Server Error, status=500).
Could not get JDBC Connection; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
のエラーとなってしまいます。(タイムアウトしているみたいです)
【確認したこと】
1.MySQLに外部からアクセスできるか以下コードより「Linux」上で確認。
# mysql -u adminuser -h IPアドレス -p
2.pom.xmlに必要な依存を追加し、jarの追加もされている事を確認。
「pom.xml」 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> <version>1.5.7.RELEASE</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
以上となりますが、足りない情報などありましたらご指摘いただきたく存じます。
もしよろしければ、お力添えいただけると幸いです。
よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。