実現したいこと
データベースに登録している日時をWeb上で表示する際にyyyy年mm月dd日HH時MM分というように表示したいです。
データベースでデータを取得するところまではできているのですが、うまく変換できずに困っています。
該当のソースコード
java
1//Beansを作成 2public class BuyDataBeans implements Serializable { 3 private int id; 4 private int userId; 5 private int totalPrice; 6 private int delivertMethodId; 7 private Date buyDate; 8 9 private String deliveryMethodName; 10 private int deliveryMethodPrice; 11 12 13 14 public int getId() { 15 return id; 16 } 17 public void setId(int id) { 18 this.id = id; 19 } 20 public int getUserId() { 21 return userId; 22 } 23 public void setUserId(int userId) { 24 this.userId = userId; 25 } 26 public int getTotalPrice() { 27 return totalPrice; 28 } 29 public void setTotalPrice(int totalPrice) { 30 this.totalPrice = totalPrice; 31 } 32 33 34 public int getDelivertMethodId() { 35 return delivertMethodId; 36 } 37 public void setDelivertMethodId(int delivertMethodId) { 38 this.delivertMethodId = delivertMethodId; 39 } 40 public Date getBuyDate() { 41 return buyDate; 42 } 43 public void setBuyDate(Date buyDate) { 44 this.buyDate = buyDate; 45 } 46 public String getDeliveryMethodName() { 47 return deliveryMethodName; 48 } 49 public void setDeliveryMethodName(String deliveryMethodName) { 50 this.deliveryMethodName = deliveryMethodName; 51 } 52 53 public String getFormatDate() { 54 SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH時mm分"); 55 return sdf.format(buyDate); 56 } 57 public void setFormatDate(Date buyDate) { 58 this.buyDate=buyDate; 59 } 60 public String getFormatTotalPrice() { 61 return String.format("%,d", this.totalPrice+this.deliveryMethodPrice); 62 } 63 64 public int getDeliveryMethodPrice() { 65 return deliveryMethodPrice; 66 } 67 public void setDeliveryMethodPrice(int deliveryMethodPrice) { 68 this.deliveryMethodPrice = deliveryMethodPrice; 69 } 70 71 72} 73
Java
1//DAOでSQL文を実行 2public static List<BuyDataBeans> getBuyDataBeansByuserId(int userId) throws SQLException { 3 Connection con = null; 4 PreparedStatement st = null; 5 List<BuyDataBeans> buydataList=new ArrayList<BuyDataBeans>(); 6 7 try { 8 con = DBManager.getConnection(); 9 10 st = con.prepareStatement( 11 "SELECT * FROM t_buy" 12 + " JOIN m_delivery_method" 13 + " ON t_buy.delivery_method_id = m_delivery_method.id" 14 + " WHERE t_buy.user_id = ?"); 15 st.setInt(1, userId); 16 17 ResultSet rs = st.executeQuery() 18 19 while(rs.next()) { 20 BuyDataBeans bdb = new BuyDataBeans(); 21 22 bdb.setId(rs.getInt("id")); 23 bdb.setTotalPrice(rs.getInt("total_price")+rs.getInt("price")); 24 bdb.setBuyDate(rs.getTimestamp("create_date")); 25 bdb.setFormatDate(rs.getTimestamp("create_date")); 26 bdb.setDelivertMethodId(rs.getInt("delivery_method_id")); 27 bdb.setUserId(rs.getInt("user_id")); 28 bdb.setDeliveryMethodPrice(rs.getInt("price")); 29 bdb.setDeliveryMethodName(rs.getString("name")); 30 31 buydataList.add(bdb); 32 33 } 34 } catch (SQLException e) { 35 System.out.println(e.getMessage()); 36 throw new SQLException(e); 37 } finally { 38 if (con != null) { 39 con.close(); 40 } 41 } 42 return buydataList; 43 }
Java
1//サーブレット 2 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 3 4 5 HttpSession session = request.getSession(); 6 try { 7 // ログイン時に取得したユーザーIDをセッションから取得 8 int userId = (int) session.getAttribute("userId"); 9 10 UserDataBeans udb = session.getAttribute("returnUDB") == null ? UserDAO.getUserDataBeansByUserId(userId) : (UserDataBeans) EcHelper.cutSessionAttribute(session, "returnUDB"); 11 12 13 14 // 入力された内容に誤りがあったとき等に表示するエラーメッセージを格納する 15 String validationMessage = (String) EcHelper.cutSessionAttribute(session, "validationMessage"); 16 17 18 request.setAttribute("validationMessage", validationMessage); 19 request.setAttribute("udb", udb); 20 //DAOで実行したSQL文から取得したデータを代入 21 List<BuyDataBeans> bdb= BuyDAO.getBuyDataBeansByuserId(userId); 22 //リクエストスコープに保存 23 request.setAttribute("bdb", bdb); 24 25 26 27 request.getRequestDispatcher(EcHelper.USER_DATA_PAGE).forward(request, response); 28 29 30 31 32 } catch (Exception e) { 33 e.printStackTrace(); 34 session.setAttribute("errorMessage", e.toString()); 35 response.sendRedirect("Error"); 36 } 37 38 39 }
DAOでSQL文を実行し、while文内で生成したインスタンスに値をセットする際の
bdb.setBuyDate(rs.getTimestamp("create_date"));
bdb.setFormatDate(rs.getTimestamp("create_date"));
この部分が原因だろうとは察しがつくのですが、どのように書き換えるべきか悩んでいます。
回答1件
あなたの回答
tips
プレビュー