javaを勉強し始めて2カ月ほどです。
ログイン機能の勉強をしているのですが、新規登録のところで少し気になるところがあるので質問させていただきたいと思います。
下記のコード2種類書いたのですが一方は新規登録できますが、もう一方はできません。
ログイン機能の方はパラメータの値をDAOまで引数でもっていってからBEANに代入しているので
新規登録の方もDAOまで引数もっていってからしようとしましたができませんでした。エラーは出ていませんがJSPに遷移せずデータベースも反映されませんでした。原因は〇〇=execute〇〇のイメージがつかめていないからなのかと思っているのですが...
個人的にスッキリしないのはBEANに値を入れるタイミングが新規登録とログインで同じでない所なのですが、下記の「新規登録不可DAO」のコードの方を直すとすればどのように直せばよいのでしょうか?
見ずらいコードですみませんがご回答いただけると助かります。
新規登録可能サーブレット package chat; import java.io.IOException; 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; import bean.Customer; import dao.CustomerDAO; /** * Servlet implementation class Regist */ @WebServlet("/Regist") public class Regist extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //response.getWriter().append("Served at: ").append(request.getContextPath()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //doGet(request, response); request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); String login = request.getParameter("login"); String password = request.getParameter("password"); Customer customer = new Customer(); customer.setLogin(login); customer.setPassword(password); CustomerDAO dao = new CustomerDAO(); int line = 0; try { line = dao.insert(customer); if(line > 0){ RequestDispatcher dispatcher = request.getRequestDispatcher("/jsp/regist-ok.jsp"); dispatcher.forward(request, response); }else{ RequestDispatcher dispatcher = request.getRequestDispatcher("/jsp/regist-error.jsp"); dispatcher.forward(request, response); } } catch (Exception e) { // TODO 自動生成された catch ブロック e.printStackTrace(); } } }
新規登録可能DAO package dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import bean.Customer; public class CustomerDAO extends DAO { public Customer search(String login,String password)throws Exception{//サーチメソッド bean.Customer customer = null; Connection con = getConnection(); PreparedStatement st; st=con.prepareStatement("select * from customer where login = ? and password = ?"); st.setString(1, login); st.setString(2, password); ResultSet rs = st.executeQuery(); while(rs.next()){ customer = new Customer(); customer.setId(rs.getInt("id")); customer.setLogin(rs.getString("login")); customer.setPassword(rs.getString("password")); } st.close(); con.close(); return customer; } public int insert(Customer customer)throws Exception{//インサートメソッド Connection con = getConnection(); con.setAutoCommit(false); PreparedStatement st= con.prepareStatement("insert into customer values(null,?,?)"); st.setString(1, customer.getLogin()); st.setString(2, customer.getPassword()); int line = st.executeUpdate(); if(line > 0){ con.commit(); }else{ con.rollback(); } st.close(); con.close(); return line; } }
新規登録不可サーブレット package chat; import java.io.IOException; 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; import dao.CustomerDAO_Error; /** * Servlet implementation class Regist */ @WebServlet("/Regist_Error") public class Regist_Error extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //response.getWriter().append("Served at: ").append(request.getContextPath()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //doGet(request, response); request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); String login = request.getParameter("login"); String password = request.getParameter("password"); CustomerDAO_Error dao = new CustomerDAO_Error(); int line = 0; try { line = dao.insert(login,password); if(line > 0){ RequestDispatcher dispatcher = request.getRequestDispatcher("/jsp/regist-ok.jsp"); dispatcher.forward(request, response); }else{ RequestDispatcher dispatcher = request.getRequestDispatcher("/jsp/regist-error.jsp"); dispatcher.forward(request, response); } } catch (Exception e) { // TODO 自動生成された catch ブロック e.printStackTrace(); } } }
新規登録不可DAO package dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import bean.Customer; public class CustomerDAO_Error extends DAO { public Customer search(String login,String password)throws Exception{//サーチメソッド bean.Customer customer = null; Connection con = getConnection(); PreparedStatement st; st=con.prepareStatement("select * from customer where login = ? and password = ?"); st.setString(1, login); st.setString(2, password); ResultSet rs = st.executeQuery(); while(rs.next()){ customer = new Customer(); customer.setId(rs.getInt("id")); customer.setLogin(rs.getString("login")); customer.setPassword(rs.getString("password")); } st.close(); con.close(); return customer; } public int insert(String login,String password)throws Exception{//インサートメソッド bean.Customer customer = null; Connection con = getConnection(); con.setAutoCommit(false); PreparedStatement st= con.prepareStatement("insert into customer values(null,?,?)"); st.setString(1, login); st.setString(2, password); ResultSet rs = st.executeQuery(); int line = st.executeUpdate(); while(rs.next()){ customer = new Customer(); customer.setId(rs.getInt("id")); customer.setLogin(rs.getString("login")); customer.setPassword(rs.getString("password")); } if(line > 0){ con.commit(); }else{ con.rollback(); } st.close(); con.close(); return line; } }
開発環境
OS windows10
エディション enterprise
言語 java8.0.1310.11
MariaDB10.1
HeidiSQL
eclipse NEON
新規登録不可DAO while文部分コメントアウトに修正 PreparedStatement st= con.prepareStatement("insert into customer values(null,?,?)"); st.setString(1, login); st.setString(2, password); ResultSet rs = st.executeQuery(); int line = st.executeUpdate(); //while(rs.next()){ customer = new Customer(); customer.setId(rs.getInt("id")); customer.setLogin(rs.getString("login")); customer.setPassword(rs.getString("password")); //} if(line > 0){ con.commit(); }else{ con.rollback(); } st.close(); con.close(); return line; } }
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/03/06 13:02 編集
2018/03/07 00:30