オブジェクト指向、というものに取り組んでおります。
使いまわすものは、パーツとして切り分けるとよいよ、ということを聞いたので、データベースにつなぐプログラムだけ分けて書こうとしています。
が、エラーが出てうまくUserActionがMysqlConnectionを使えておりません。
型がいまだに理解できていないので、そこまわりかとも思うのですが、お知恵を拝借願いたいです。
UserAction.java
java
1package action; 2 3import java.sql.Connection; 4import java.sql.PreparedStatement; 5import java.sql.ResultSet; 6import java.sql.SQLException; 7import java.util.ArrayList; 8 9import javax.naming.NamingException; 10import javax.servlet.http.HttpServletRequest; 11import javax.servlet.http.HttpServletResponse; 12 13import org.apache.commons.beanutils.BeanUtils; 14import org.apache.struts.action.Action; 15import org.apache.struts.action.ActionForm; 16import org.apache.struts.action.ActionForward; 17import org.apache.struts.action.ActionMapping; 18 19import form.UserForm; 20import model.MysqlConnection; 21 22public class UserAction extends Action { 23 public 24 ActionForward execute( 25 ActionMapping mapping, 26 ActionForm form, 27 HttpServletRequest request, 28 HttpServletResponse response) 29 30 throws 31 SQLException, 32 NamingException, 33 IllegalAccessException, 34 java.lang.reflect.InvocationTargetException, 35 Exception{ 36 37 request.setCharacterEncoding("UTF-8"); 38 String department =request.getParameter("department"); 39 40 MysqlConnection mysqlconnection = new MysqlConnection(); 41 Connection conn = (Connection) mysqlconnection; 42 43 44 String sql 45 = "SELECT user_id, user FROM pcm.users " + 46 "where department=?"; 47 48 PreparedStatement pStmt = conn.prepareStatement(sql); 49 pStmt.setString(1, department); 50 ResultSet rs = pStmt.executeQuery(); 51 52 ArrayList list = new ArrayList<>(); 53 54 while(rs.next()) { 55 UserForm uform = new UserForm(); 56 BeanUtils.setProperty(uform, "userID", rs.getString("user_id")); 57 BeanUtils.setProperty(uform, "user", rs.getString("user")); 58 list.add(uform); 59 } 60 61 request.setAttribute("BBB",list); 62 63 64 return (mapping.findForward("success")); 65 } 66} 67
MysqlConnection.java
java
1package model; 2 3import java.sql.Connection; 4import java.sql.DriverManager; 5import java.sql.SQLException; 6 7import javax.naming.NamingException; 8 9public class MysqlConnection { 10 11 public Connection MysqlConnection() 12 throws SQLException, 13 NamingException{ 14 15 final String JDBC_URL = "jdbc:mysql://localhost:3306/pcm?serverTimezone=JST"; 16 final String DB_USER = "root"; 17 final String DB_PASS = "password"; 18 19 Connection conn = DriverManager.getConnection(JDBC_URL, DB_USER, DB_PASS); 20 return conn; 21 } 22} 23
エラーメッセージ
java
1HTTPステータス 500 – Internal Server Error 2タイプ 例外報告 3 4メッセージ java.lang.ClassCastException: class model.MysqlConnection cannot be cast to class java.sql.Connection (model.MysqlConnection is in unnamed module of loader org.apache.catalina.loader.ParallelWebappClassLoader @63cf9de0; java.sql.Connection is in module java.sql of loader 'platform') 5 6説明 サーバーは予期しない条件に遭遇しました。それはリクエストの実行を妨げます。 7 8例外 9 10javax.servlet.ServletException: java.lang.ClassCastException: class model.MysqlConnection cannot be cast to class java.sql.Connection (model.MysqlConnection is in unnamed module of loader org.apache.catalina.loader.ParallelWebappClassLoader @63cf9de0; java.sql.Connection is in module java.sql of loader 'platform') 11 org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:535) 12 org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:433) 13 org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236) 14 org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196) 15 org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432) 16 javax.servlet.http.HttpServlet.service(HttpServlet.java:660) 17 javax.servlet.http.HttpServlet.service(HttpServlet.java:741) 18 org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) 19 filter.EncodingFileter.doFilter(EncodingFileter.java:30) 20根本原因 21 22java.lang.ClassCastException: class model.MysqlConnection cannot be cast to class java.sql.Connection (model.MysqlConnection is in unnamed module of loader org.apache.catalina.loader.ParallelWebappClassLoader @63cf9de0; java.sql.Connection is in module java.sql of loader 'platform') 23 action.UserAction.execute(UserAction.java:41) 24 org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431) 25 org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236) 26 org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196) 27 org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432) 28 javax.servlet.http.HttpServlet.service(HttpServlet.java:660) 29 javax.servlet.http.HttpServlet.service(HttpServlet.java:741) 30 org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) 31 filter.EncodingFileter.doFilter(EncodingFileter.java:30)
castできていないようですが、なにが足りないのでしょうか....
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。