Java day9n
Upcoming SlideShare
Loading in...5
×
 

Java day9n

on

  • 313 views

 

Statistics

Views

Total Views
313
Views on SlideShare
313
Embed Views
0

Actions

Likes
0
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Java day9n Java day9n Document Transcript

    • package model.bo; import import import import java.sql.SQLException; java.util.ArrayList; model.bean.Account; model.dao.ShowUserDAO; public class CheckLoginBO { private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-+]+(.[_A-Zaz0-9-]+)*@" + "[A-Za-z0-9-]+(.[A-Za-z0-9]+)*(.[A-Za-z]{2,})$"; public boolean isValidAccount(String account, String firstName, String lastName, String email) { //check vaild input boolean vaildMail =email.matches(EMAIL_PATTERN); if ((account == null || "".equals(account)) || (firstName == null || "".equals(firstName)) || (lastName == null || "".equals(lastName)) || (email == null|| "".equals(email)) || !vaildMail) { return false; } else { return true; } } public ArrayList<Account> getAccountDetail() throws SQLException { ArrayList<Account> listAccount = new ArrayList<Account>(); ShowUserDAO showUserDAO = new ShowUserDAO(); listAccount = showUserDAO.getAllUser(); return listAccount; } /** * CheckDulication user * @param account * @return */ public boolean checkDulication(String account) { // TODO Auto-generated method stub return false; } } ===================================================== <%@page import="model.bean.Account"%> <%@page import="java.util.ArrayList"%> <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!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=ISO-8859-1"> <title>Insert title here</title> </head> <body> <% ArrayList<Account> accountList = new ArrayList<Account>(); Account accounts = new Account(); String account = request.getParameter("account");
    • String fistName = request.getParameter("fistName"); String lastName = request.getParameter("lastName"); String email = request.getParameter("email"); session.setAttribute("account",account); session.setAttribute("fistName",fistName); session.setAttribute("lastName",lastName); session.setAttribute("email",email); accountList = (ArrayList<Account>) request.getAttribute("accList"); %> <table> <tr> <th>account</th> <th>firstName</th> <th>lastName</th> <th>email</th> </tr> <tr> <td><%=account%></td> <td><%=fistName%></td> <td><%=lastName%></td> <td><%=email%></td> </tr> </table> <br> <table> <tr> </tr> <% <th>account</th> <th>firstName</th> <th>lastName</th> <th>email</th> for (Account accc : accountList) { %> <tr> <td><%=accc.getAccount()%></td> <td><%=accc.getFistName()%></td> <td><%=accc.getLastName()%></td> <td><%=accc.getEmail()%></td> </tr> <% } %> </table> <br> <% String returnPage = "Login.jsp"; %> <input type="button" value="Change" onClick="javascript:window.location='<%=returnPage%>';"> <% String updatePage = "UpdateServlet"; %> <input type="button" value="Save" onClick="javascript:window.location='<%=updatePage%>';"> </body> </html> ============================================================================= <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    • pageEncoding="ISO-8859-1"%> <!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=ISO-8859-1"> <title>Insert title here</title> </head> <body> <form action="CheckAccountLoginServlet" method="post"> <H2 >Input your information</H2> <table> <tr> <td>Account</td> <td><Input type="text" id="account" name="account" /></td> </tr> <tr> <td>First name</td> <td><Input type="text" id="firstName" name="fistName" /></td> </tr> <tr> <td>Last name</td> <td><Input type="text" id="lastName" name="lastName" /></td> </tr> <tr> <td>Email</td> <td><Input type="text" id="email" name="email" /></td> </tr> <tr> <td style="text-align:center" colspan="2"> <Input type="submit" value="Submit" /> <Input type="reset" value="Reset" /> </td> </tr> </table> </form> </body> </html> ================================================================================ ======== package model.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; public class UpdateDAO { public void update(String account, String fistName, String lastName, String email) { Connection con = null; PreparedStatement ps = null; GetConnectionDAO connectionDAO = new GetConnectionDAO(); String query = "update account set fist_name=?,last_name=?,email=? where useraccount =?"; try { con = connectionDAO.getConnection(); ps = con.prepareStatement(query); ps.setString(1, "fist_name"); ps.setString(2, "last_name");
    • ps.setString(3, "email"); ps.setString(4, "useraccount"); ps.executeUpdate(); ps.close(); con.close(); } catch (SQLException e) { e.printStackTrace(); } catch (Exception e) { System.out.println("Not Update! "); } } } ================================================================================ ======================== package model.dao; import import import import import java.sql.Connection; java.sql.PreparedStatement; java.sql.ResultSet; java.sql.SQLException; java.util.ArrayList; import com.sun.org.apache.regexp.internal.recompile; import com.sun.xml.internal.ws.api.pipe.NextAction; import model.bean.Account; public class ShowUserDAO { static GetConnectionDAO getConnectionDAO = new GetConnectionDAO(); /** * * @return List Account * @throws SQLException */ public ArrayList<Account> getAllUser() throws SQLException { ArrayList<Account> accounts = new ArrayList<Account>(); String query = "SELECT useraccount,fist_name,last_name,email FROM account"; Connection con = null; PreparedStatement ps = null; ResultSet set = null; try { con = getConnectionDAO.getConnection(); System.out.println(con); ps = con.prepareStatement(query); set = ps.executeQuery(); while (set.next()) { Account account = new Account(); account.setAccount(set.getString("useraccount")); account.setFistName(set.getString("fist_name")); account.setLastName(set.getString("last_name")); account.setEmail(set.getString("email")); accounts.add(account); //TODO Xem Lai--> } ps.close(); set.close(); con.close();
    • } catch (SQLException e) { e.printStackTrace(); } finally { } return accounts; } public boolean duplicateCheck(String account){ Connection con = null; PreparedStatement ps = null; ResultSet set = null; String query ="SELECT useraccount FROM account "; boolean check = false; try { con = getConnectionDAO.getConnection(); ps = con.prepareStatement(query); set = ps.executeQuery(); while (set.next()) { if(set.getString("useraccount").equals(account)){ check = true; break; }else{ check = false; } } set.close(); ps.close(); con.close(); } catch (Exception e) { e.printStackTrace(); } return check; } } ================================================================================ ============= package model.dao; import java.sql.Connection; import java.sql.PreparedStatement; public class InsertDAO { public void insert(String account, String fistName, String lastName, String email) { Connection con = null; PreparedStatement ps = null; String query = "insert into account(useraccount, fist_name,last_name,email) values (?,?,?,?)"; try { GetConnectionDAO connectionDAO = new GetConnectionDAO(); con = connectionDAO.getConnection(); ps = con.prepareStatement(query); ps.setString(1, account); ps.setString(2, fistName); ps.setString(3, lastName); ps.setString(4, email); ps.executeUpdate(); System.out.println("Insert .....");
    • ps.close(); con.close(); } catch (Exception e) { // TODO: handle exception } } } ======================================================================= package model.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class GetConnectionDAO { public Connection getConnection(){ Connection con = null; try{ Class.forName("com.mysql.jdbc.Driver"); if(con == null || con.isClosed()){ String ulr ="jdbc:mysql://localhost:3306/TRANEES"; con = DriverManager.getConnection(ulr,"root","1234"); } }catch(SQLException e){ e.printStackTrace(); } catch(Exception e){ System.out.println("ket noi ko duoc"); e.printStackTrace(); } return con; } } =============================================================================== package model.bean; public class Account { private String account; private String fistName; private String lastName; private String email; public Account() { // TODO Auto-generated constructor stub } public Account(String account,String fistName,String lastName,String email) { this.account = account; this.fistName =fistName; this.lastName =lastName; this.email = email; } } public String getAccount() { return account;
    • public void setAccount(String account) { this.account = account; } } public String getFistName() { return fistName; public void setFistName(String fistName) { this.fistName = fistName; } } public String getLastName() { return lastName; public void setLastName(String lastName) { this.lastName = lastName; } } public String getEmail() { return email; public void setEmail(String email) { this.email = email; } } ================================================================= package controller; import java.io.IOException; import java.sql.SQLException; import java.util.ArrayList; import import import import import javax.servlet.RequestDispatcher; javax.servlet.ServletException; javax.servlet.http.HttpServlet; javax.servlet.http.HttpServletRequest; javax.servlet.http.HttpServletResponse; import import import import model.bean.Account; model.bo.CheckLoginBO; model.dao.InsertDAO; model.dao.UpdateDAO; /** * Servlet implementation class UpdateServlet */ public class UpdateServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public UpdateServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response);
    • } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String String String String account = request.getParameter("account"); fistName = request.getParameter("fistName"); lastName = request.getParameter("lastName"); email = request.getParameter("email"); request.setAttribute("account", account); request.setAttribute("fistName", fistName); request.setAttribute("lastName", lastName); request.setAttribute("email", email); System.out.println(account +fistName+lastName+email); UpdateDAO updateDAO = new UpdateDAO(); InsertDAO insertDAO = new InsertDAO(); CheckLoginBO checkLoginBO = new CheckLoginBO(); RequestDispatcher rd = request.getRequestDispatcher("ShowTranees.jsp"); ArrayList<Account> accList = new ArrayList<Account>(); if (checkLoginBO.checkDulication(account)) { //update updateDAO.update(account, fistName, lastName, email); try { accList = checkLoginBO.getAccountDetail(); } catch (SQLException e) { System.out.println("Get Db Fail!"); e.printStackTrace(); } request.setAttribute("accList", accList); rd.forward(request, response); System.out.println("Updated !!!!!!"); } else { //insert insertDAO.insert(account, fistName, lastName, email); try { accList = checkLoginBO.getAccountDetail(); } catch (SQLException e) { System.out.println("Get Db Fail!"); e.printStackTrace(); } request.setAttribute("accList", accList); rd.forward(request, response); System.out.println("Inserted !!!!1"); } } } ============================================================================= package controller;
    • import java.io.IOException; import java.sql.SQLException; import java.util.ArrayList; import import import import import javax.servlet.RequestDispatcher; javax.servlet.ServletException; javax.servlet.http.HttpServlet; javax.servlet.http.HttpServletRequest; javax.servlet.http.HttpServletResponse; import model.bean.Account; import model.bo.CheckLoginBO; /** * Servlet implementation class CheckLoginServlet */ public class CheckAccountLoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public CheckAccountLoginServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String account = request.getParameter("account"); String fistName = request.getParameter("fistName"); String lastName = request.getParameter("lastName"); String email = request.getParameter("email"); ArrayList<Account> accList = new ArrayList<Account>(); CheckLoginBO checkLoginBO = new CheckLoginBO(); { if (checkLoginBO.isValidAccount(account, fistName, lastName, email)) try { checkLoginBO.getAccountDetail(); accList = } catch (SQLException e) { System.out.println("Get Db Fail!"); e.printStackTrace(); } request.setAttribute("account", account); request.setAttribute("fistName", fistName); request.setAttribute("lastName", lastName); request.setAttribute("email", email);
    • request.setAttribute("accList", accList); RequestDispatcher rd = request.getRequestDispatcher("ShowTranees.jsp"); rd.forward(request, response); } else { response.sendRedirect("Login.jsp"); } } }