###前提・実現したいこと
あるボタンを押したらDB内にあるカラムのint型データが、
1の場合、0に切り替わる。
0の場合、1に切り替わる。
という処理を実現したいのですが、
自分で記述したSQLでは反応しませんでした。
###Servle
package servlet; import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import javax.naming.InitialContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.sql.DataSource; @WebServlet("/user_delete") public class user_delete extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Connection con = null; PreparedStatement ps = null; String sql = null; try { // 確立 InitialContext initContext = new InitialContext(); DataSource ds = (DataSource) initContext.lookup("java:comp/env/jdbc/Library"); con = ds.getConnection(); // sql文の発行 sql = "UPDATE user SET delete_flg = 1 WHERE user_id=? and delete_flg = 0"; ps = con.prepareStatement(sql); ps.setString(1, request.getParameter("user_no")); ps.executeUpdate(); } catch (Exception e) { throw new ServletException(e); } finally { try { if (con != null) { con.close(); } if (ps != null) { ps.close(); } } catch (Exception e) { } } response.sendRedirect("user_kanri.jsp"); } }
###JSP
<%@ page contentType="text/html; charset=UTF-8" import="java.sql.*,javax.naming.*,javax.sql.*,java.text.*"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%> <sql:setDataSource var="db" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/Library" user="root" password="" /> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>ユーザ管理画面</title> </head> <body> <h2>ユーザー管理画面</h2> <form method="POST" action="user_tsuika"> <h3> 新規ユーザ追加 </h3> 名前(姓) <input type="text" name="first_name" size="30"> 名前(名) <input type="text" name="last_name" size="30"> <br> カナ(姓) <input type="text" name="first_name_kana" size="30"> カナ(名) <input type="text" name="last_name_kana" size="30"> <br> ID <input type="text" name="user_id" size="30"> パスワード <input type="password" name="password" size="30"> <br><br> 管理者 or 利用者 <input name="admin" type="radio" value="1">管理者</input> <input name="admin" type="radio" value="2">ユーザ</input> <input style="color:white; background-color:D57200;" type="submit" value="登録"/> </form> <h3>ユーザリスト</h3> <table border="1" width=70%> <th>ユーザNo</th> <th>名前(姓)</th> <th>名前(名)</th> <th>カナ(姓)</th> <th>カナ(名)</th> <th>ID</th> <th>パスワード</th> <th>削除</th> <th>編集</th> <% Connection con = null; PreparedStatement ps = null; String sql = null; ResultSet rs = null; try{ InitialContext initContext = new InitialContext(); DataSource ds = (DataSource) initContext.lookup("java:comp/env/jdbc/Library"); con = ds.getConnection(); sql = "SELECT user_no,first_name,last_name,first_name_kana,last_name_kana,user_id,password" + " FROM user where delete_flg = 0"; // SELECT命令の準備 ps = con.prepareStatement(sql); // UPDATE命令を実行 rs = ps.executeQuery(); // 結果セットの内容を順に出力 while(rs.next()){ %> <tr> <td height=70><%=rs.getString("user_no") %></td> <td height=70><%=rs.getString("first_name") %></td> <td height=70><%=rs.getString("last_name") %></td> <td height=70><%=rs.getString("first_name_kana") %></td> <td height=70><%=rs.getString("last_name_kana") %></td> <td height=70><%=rs.getString("user_id") %></td> <td height=70><%=rs.getString("password") %></td> <td height=70><form method="POST" action ="user_delete"> <input name="user_no" type="hidden" value=<%=rs.getString("user_no")%>> <input class="button" type="submit" value="削除"> </form></td> <td height=70><form method="POST" action ="user_edit.jsp"> <input name="user_edit" type="hidden" value=<%=rs.getString("user_id")%>> <input class="button" type="submit" value="編集"> </form></td> <% } }catch(Exception e){ throw new ServletException(e); }finally{ try{ if(rs != null){rs.close();} if(rs != null){ps.close();} if(rs != null){con.close();} }catch(Exception e){} } %> </table> <form action="kanri_mypage.jsp"> <input style="color:white; background-color:D57200;" type="submit" name="reset" value="TOPページ"> </form> </body> </html>
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/06/17 01:53
2016/06/17 02:03
2016/06/17 02:25