yuki_si
@yuki_si

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

jdbcメモアプリ

index.jsp

<%@page import="java.util.Iterator"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.HashMap"%>
<%@page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Memo App</title>
</head>
<body>

 <h1>メモアプリ</h1>

 <form method="post">
  <input type="text" name="title" size="50" /> <br />
  <textarea rows="5" cols="80" name="memo"></textarea>
  <br /> <input type="submit" />
 </form>

 <%
     ArrayList<HashMap<String, String>> list = (ArrayList<HashMap<String, String>>) request
             .getAttribute("record_list");

if(list != null){ 
     Iterator<HashMap<String, String>> i = list.iterator();
     while (i.hasNext()) {
         HashMap map = i.next();
         out.println("<hr/>");
         out.println("<div>" + map.get("title") + "</div>");
         out.println("<div>" + map.get("modified_date") + "</div>");
         out.println("<div>" + ((String) map.get("memo")).replace("\n", "<br/>") + "</div>");
     }
}
 %>

</body>
</html>

MemoAppMain.java
```
package jp.example.org;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/")
public class MemoAppMain extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    String url = "jdbc:mysql://telecom-db2.cnybmp6pfsty.ap-northeast-1.rds.amazonaws.com:3306/memoapp_db";
    String user = "memoapp";
    String pass = "memoapp";
    String view = "/WEB-INF/jsp/index.jsp";
    Connection con = null;
    Statement smt = null;
    ResultSet result = null;


    RequestDispatcher dispatcher = req.getRequestDispatcher(view);
    dispatcher.forward(req, resp);
    try {
         Class.forName("com.mysql.jdbc.Driver");

        con =  DriverManager.getConnection(url, user, pass);

        smt = con.createStatement();

        String select_memo = "select title, memo, modified_date from memo_data;";

        result = smt.executeQuery(select_memo);

        while (result.next()) {
             System.out.println("title: " + result.getString("title"));
             System.out.println("memo: " + result.getString("memo"));
             System.out.println("modify: " + result.getString("modified_date"));
        }
    }catch(SQLException | ClassNotFoundException e) {
        e.printStackTrace();
    }finally {
        if(result != null) {
            try{result.close();}catch(SQLException ignore){}
        }
        if(smt != null) {
            try{smt.close();}catch(SQLException ignore){}
        }
        if(con != null) {
            try{con.close();}catch(SQLException ignore){}
        }
    }

    }



ArrayList<HashMap<String, String>> record_list = new ArrayList<>();


@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    req.setCharacterEncoding("UTF-8");
    System.out.println("title: " + req.getParameter("title"));
    System.out.println("memo: " + req.getParameter("memo"));

    String url = "jdbc:mysql://telecom-db2.cnybmp6pfsty.ap-northeast-1.rds.amazonaws.com:3306/memoapp_db";
    String user = "memoapp";
    String pass = "memoapp";
    String view = "/WEB-INF/jsp/index.jsp";
    Connection con = null;
    Statement smt = null;
    ResultSet result = null;



    // -- ここにDBへ保存処理 --
    // -- ここにDBへ保存処理 --

    try {
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection(url, user, pass);
        smt = con.createStatement();
        System.out.println("smt: " + smt);

        String create_table = "create table if not exists memo_data (" +
                "memo_id INT(11) auto_increment not null comment 'ID'," +
                "category INT(11) comment 'カテゴリ'," +
                "title VARCHAR(64) comment 'タイトル'," +
                "memo TEXT comment 'メモ'," +
                "create_date DATETIME comment '作成日'," +
                "modified_date DATETIME comment '更新日'," +
                "primary key (memo_id)" + ")";
            // create table
            smt.executeUpdate(create_table);

            String form_title = req.getParameter("title");
            String form_memo = req.getParameter("memo");

            System.out.println("title: " + form_title);
            System.out.println("text: " + form_memo);
            String insert_memo = "insert into memo_data (" +
                    "category, title, memo, create_date, modified_date" +
                ") values (" +
                    "0," +
                    "'" + form_title + "'," +
                    "'" + form_memo + "'," +
                    "cast(now() as datetime)," +
                    "cast(now() as datetime) " +
                ");";
            // レコード保存
            smt.executeUpdate(insert_memo);


            String select_memo = "select title, memo, modified_date from memo_data;";

            result = smt.executeQuery(select_memo);
            while (result.next()) {
                HashMap<String, String> record = new HashMap<>();
                record.put("title", result.getString("title"));
                record.put("memo", result.getString("memo"));
                record.put("modified_date", result.getString("modified_date"));
                record_list.add(record);

                req.setAttribute("record_list", record_list);

            }
    } catch (SQLException | ClassNotFoundException e) {
        e.printStackTrace();
    }
    finally {
        if(result != null) {
            try{result.close();}catch(SQLException ignore){}
        }
        if(smt != null) {
            try{smt.close();}catch(SQLException ignore){}
        }
        if(con != null) {
            try{con.close();}catch(SQLException ignore){}
        }
    }
    // -- ここまでDB処理 --

    resp.sendRedirect(".");
}

}
```

0

No Answers yet.

Your answer might help someone💌