質問です。
現在jspの入力フォームに入力した値を使用してMysqlのDBにレコードを追加するコードを作成したのですが、ここにレコードを更新する機能を追加したいと考えております。
現在の動作の流れとしては
まずDao、Dtoで用意した各テーブルのカラムの変数を一覧画面に表示、以下表示のjspです。↓
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content= "width=device-width, initial-scale=1"> <script type="text/javascript"> </script> <title>課題</title> </head> <body> <a href="./Login">ログアウト</a> <h1>一覧</h1> <a href="./Detail">新規登録</a> <body> <TABLE border="1"> <TR> <TH>No</TH> <TH>氏名</TH> <TH>氏名(ひらがな)</TH> <TH>年齢</TH> <TH>詳細</TH> </TR> <TR> <c:forEach var="tmp" items="${list}"> <TD><c:out value="${tmp.Id}" /></TD> <TD><c:out value="${tmp.name}" /></TD> <TD><c:out value="${tmp.nameHiragana}" /></TD> <TD><c:out value="${tmp.age}" /></TD> <TD><a href="./Datail">詳細</a></TD> <TR/> </c:forEach> </table> </body> </html>
次に入力フォームのjspです。
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta http-equiv="Content-Type" content= "width=device-width, initial-scale=1"> <script type="text/javascript"> </script> <title>詳細</title> </head> <body> <h1>詳細</h1> <form action="./Detail" method="post"> 氏名<input type="text" name="name" size="20" maxlength="20"><br> 氏名(ひらがな)<input type="text" name="nameHiragana" size="20" maxlength="20"><br> 生年月日<input type="text" name="birthday" size="10" maxlength="10"><br> <input type="submit" value="登録" /> </form> </body> </html>
以上で入力した内容をDto内のInsert用のメソッドへ渡し、Insert文を実行、という流れになります。
一覧画面では各レコードを横一列に表示し、レコードごとに入力画面へ移行するボタンを用意していますので、この列の情報をそのまま表示できたらと思います。
ここからどのように書き加えたらよいか、詳しい方いらっしゃればご教授いただければと思います。
どうぞ宜しくお願い致します。
追記です。
実装に何を使っているというのがよくわからないのですが、各コードを記載すればよろしいでしょうか。
Dao:Serect用メソッド
public class listDao { private static final String url = "jdbc:mysql://localhost:3306/test"; private static final String user = "root"; private static final String password = ""; private static Connection conn = null; // Connectionクラス private static PreparedStatement pstmt = null; // Statementクラス private static ResultSet rs = null; // ResultSetクラス public ArrayList<Listinfo> findAll(String[] args) { ArrayList<Listinfo> listInfos = new ArrayList<Listinfo>(); String sql = "SELECT * FROM e_einfo"; try { Class.forName("com.mysql.jdbc.Driver"); // ドライバクラスのロード // Connection,Statement,ResultSetのクラスを作成 conn = DriverManager.getConnection(url, user, password); // Connectionクラス生成 pstmt = conn.prepareStatement(sql); // Statementクラス生成 rs = pstmt.executeQuery(); // ResultSetクラス生成 while (rs.next()) { // 結果表からデータを取得 Listinfo list = new Listinfo(); // list.setAge(rs.getInt("age")); list.setEmployeeId(rs.getString("employee_id")); list.setName(rs.getString("name")); list.setNameHiragana(rs.getString("name_hiragana")); String birthday = rs.getString("birthday"); int age = convertAge(birthday); list.setAge(age); listInfos.add(list); } } catch (SQLException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) { } rs = null; } if (pstmt != null) { try { pstmt.close(); } catch (SQLException e) { } pstmt = null; } if (conn != null) { try { conn.close(); } catch (SQLException e) { } conn = null; } } return listInfos; }
Dao:Insertメソッド
public void InsertInformation(String employeeId, String name, String name_hiragana, String birthday) throws ClassNotFoundException, SQLException { // SQL文 String sql = "INSERT INTO employee_info (name, name_hiragana, birthday) values(?, ?, cast(? as date))"; try { Class.forName("com.mysql.jdbc.Driver"); // ドライバクラスのロード conn = DriverManager.getConnection(url, user, password); // Connectionクラス生成 pstmt = conn.prepareStatement(sql); // PreparedStatementクラス生成 pstmt.setString(1, name); pstmt.setString(2, name_hiragana); pstmt.setString(3, birthday); pstmt.executeUpdate(); // INSERT文を実行 conn.close(); } catch (SQLException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { } finally { // 終了処理 // PreparedStatement終了 if (pstmt != null) { try { pstmt.close(); } catch (SQLException e) { } pstmt = null; } // Connection終了 if (conn != null) { try { conn.close(); } catch (SQLException e) { } conn = null; } } }
最後に入力画面のServetです。
/** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // HttpSession session = request.getSession(); request.setCharacterEncoding("UTF-8"); String Id = request.getParameter("Id"); String name = request.getParameter("name"); String name_hiragana = request.getParameter("nameHiragana"); String birthday = request.getParameter("birthday"); listDao dao = new listDao(); //Daoクラス生成 try { dao.InsertInformation(employeeId, name, name_hiragana, birthday); } catch (ClassNotFoundException e) { // TODO 自動生成された catch ブロック e.printStackTrace(); } catch (SQLException e) { // TODO 自動生成された catch ブロック e.printStackTrace(); } //挿入処理} // session.setAttribute("Infomation", dao); response.sendRedirect("./List"); } }
以上になります。