[実現したいこと]
フォームに値を入力し、DBの値を取得して表示させる
[開発環境]
Java 11
STS 2.1.13
mysql 5.7
[エラー内容]
java.lang.ClassCastException: class java.lang.String cannot be cast to class java.lang.Integer (java.lang.String and java.lang.Integer are in module java.base of loader 'bootstrap') at com.example.demo.trySpring.HelloService.findOne(HelloService.java:18) ~[classes/:na] at com.example.demo.trySpring.HelloController.postDbRequest(HelloController.java:35) ~[classes/:na] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na] at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na] at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na] at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.1.14.RELEASE.jar:5.1.14.RELEASE] at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.1.14.RELEASE.jar:5.1.14.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105) ~[spring-webmvc-5.1.14.RELEASE.jar:5.1.14.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:893) ~[spring-webmvc-5.1.14.RELEASE.jar:5.1.14.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:798) ~[spring-webmvc-5.1.14.RELEASE.jar:5.1.14.RELEASE] at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.1.14.RELEASE.jar:5.1.14.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) ~[spring-webmvc-5.1.14.RELEASE.jar:5.1.14.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.1.14.RELEASE.jar:5.1.14.RELEASE] at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.1.14.RELEASE.jar:5.1.14.RELEASE] at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) ~[spring-webmvc-5.1.14.RELEASE.jar:5.1.14.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:660) ~[tomcat-embed-core-9.0.31.jar:9.0.31] at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.1.14.RELEASE.jar:5.1.14.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.31.jar:9.0.31] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.31.jar:9.0.31] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.31.jar:9.0.31] at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.31.jar:9.0.31] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.31.jar:9.0.31] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.31.jar:9.0.31] at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.1.14.RELEASE.jar:5.1.14.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.1.14.RELEASE.jar:5.1.14.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.31.jar:9.0.31] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.31.jar:9.0.31] at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.1.14.RELEASE.jar:5.1.14.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.1.14.RELEASE.jar:5.1.14.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.31.jar:9.0.31] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.31.jar:9.0.31] at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:94) ~[spring-web-5.1.14.RELEASE.jar:5.1.14.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.1.14.RELEASE.jar:5.1.14.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.31.jar:9.0.31] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.31.jar:9.0.31] at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.1.14.RELEASE.jar:5.1.14.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.1.14.RELEASE.jar:5.1.14.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.31.jar:9.0.31] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.31.jar:9.0.31] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.31.jar:9.0.31] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) ~[tomcat-embed-core-9.0.31.jar:9.0.31] at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) ~[tomcat-embed-core-9.0.31.jar:9.0.31] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) ~[tomcat-embed-core-9.0.31.jar:9.0.31] <後略>
[エラー該当箇所]
Java
1[一部抜粋] HelloService.java 2 3 public User findOne(int id) { 4// 1件検索 5 Map<String, Object> map = helloRepository.findOne(id); 6// Mapから値を取得 Mapのgetメソッドにテーブルのフィールド名を指定すれば値を取得できる。 7 int userId = (Integer)map.get("user_id"); 8 String userName = (String)map.get("user_name"); 9 int age = (Integer)map.get("age"); 10// Mapから取得した値をUserクラスのインスタンスにセット 11 User user = new User(); 12 user.setUserId(userId); 13 user.setUserName(userName); 14 user.setAge(age); 15 16 return user; 17 }
java
1[一部抜粋] HelloController.java 2 3 @PostMapping("/hello/db") 4 public String postDbRequest(@RequestParam("text2") String str, Model model) { 5 6 int id = Integer.parseInt(str); 7 8 User user = helloService.findOne(id); 9 10 model.addAttribute("id", user.getUserId()); 11 model.addAttribute("name", user.getUserName()); 12 model.addAttribute("age", user.getAge()); 13 14 return "helloResponseDB"; 15 }
java
1Hello.Repository 2 3package com.example.demo.trySpring; 4 5import java.util.Map; 6import org.springframework.beans.factory.annotation.Autowired; 7import org.springframework.jdbc.core.JdbcTemplate; 8import org.springframework.stereotype.Repository; 9 10 11@Repository 12public class HelloRepository { 13 @Autowired 14 15 private JdbcTemplate jdbcTemplate; 16 17 public Map<String, Object> findOne(int id) { 18 String query = "SELECT " 19 + " user_id, " 20 + " user_name, " 21 + " age " 22 + " FROM m_user " 23 + " WHERE user_id = ? "; 24 Map<String, Object> m_user = jdbcTemplate.queryForMap(query, id); 25 26 return m_user; 27 } 28} 29
mysql
1mysql> use user 2Database changed 3mysql> show tables; 4+----------------+ 5| Tables_in_user | 6+----------------+ 7| employee | 8| m_user | 9| user | 10+----------------+ 113 rows in set (0.00 sec) 12 13mysql> use m_user; 14ERROR 1049 (42000): Unknown database 'm_user' 15mysql> SELECT * FROM m_user; 16+---------+--------------+------+ 17| user_id | user_name | age | 18+---------+--------------+------+ 19| 1 | 野田雄大 | 27 | 20+---------+--------------+------+ 211 row in set (0.01 sec) 22
[試したこと]
・findOneメソッド内のIntegerをintに変更。
・postDbRequestメソッド内のStringをIntegerに変更して、キャストを行わないようにした。
→それでも同様のエラーが同様のエラー内容で表示されました。
なので、データベースの値の受け渡しが上手くできてないのかなと考えています。
[質問内容]
・データベースの値の受け渡し以外の問題の可能性はどこにあるのか?
・データベースの値の受け渡しが上手くいって無い場合、どのような現象が考えうるのか?
以上の内容に関して、ご指摘、アドバイスなど頂戴できればと思います。
よろしくお願いいたします。
回答2件
あなたの回答
tips
プレビュー