SlideShare a Scribd company logo
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");
}
}
}

More Related Content

PDF
MaintainStaffTable
DOC
code for quiz in my sql
TXT
Data20161007
PPTX
Drupal7 dbtng
PDF
Линзы - комбинаторная манипуляция данными
DOCX
VISUALIZAR REGISTROS EN UN JTABLE
PDF
Solid in practice
PDF
Kotlin : Advanced Tricks - Ubiratan Soares
MaintainStaffTable
code for quiz in my sql
Data20161007
Drupal7 dbtng
Линзы - комбинаторная манипуляция данными
VISUALIZAR REGISTROS EN UN JTABLE
Solid in practice
Kotlin : Advanced Tricks - Ubiratan Soares

What's hot (19)

PPTX
Writing Good Tests
PDF
Swift에서 꼬리재귀 사용기 (Tail Recursion)
PPTX
Ensure code quality with vs2012
DOCX
exportDisabledUsersRemoveMailbox
PDF
Lucene
PDF
PDF
Android Testing
ODP
PHPUnit elevato alla Symfony2
PDF
Developer Experience i TypeScript. Najbardziej ikoniczne duo
KEY
SOLID Principles
PDF
Dealing with Legacy PHP Applications
PDF
Separation of concerns - DPC12
KEY
Jython: Python para la plataforma Java (EL2009)
PDF
PureScript & Pux
PDF
Alexey Tsoy Meta Programming in C++ 16.11.17
PDF
Let the type system be your friend
DOCX
INSERCION DE REGISTROS DESDE VISUAL.NET A UNA BD DE SQL SERVER
PDF
Fp java8
PPTX
Devoxx 2012 hibernate envers
Writing Good Tests
Swift에서 꼬리재귀 사용기 (Tail Recursion)
Ensure code quality with vs2012
exportDisabledUsersRemoveMailbox
Lucene
Android Testing
PHPUnit elevato alla Symfony2
Developer Experience i TypeScript. Najbardziej ikoniczne duo
SOLID Principles
Dealing with Legacy PHP Applications
Separation of concerns - DPC12
Jython: Python para la plataforma Java (EL2009)
PureScript & Pux
Alexey Tsoy Meta Programming in C++ 16.11.17
Let the type system be your friend
INSERCION DE REGISTROS DESDE VISUAL.NET A UNA BD DE SQL SERVER
Fp java8
Devoxx 2012 hibernate envers
Ad

Similar to Java day9n (20)

DOCX
Week 12 code
PPTX
evoting ppt.pptx
DOCX
Update&delete
PDF
Modul Praktek Java OOP
DOCX
บทที่6 update&delete
DOCX
This is a basic JAVA pgm that contains all of the major compoents of DB2
PDF
Simple Jdbc With Spring 2.5
PDF
GAE/J 開発環境でJDO入門
PPT
Java Database Connectivity Java Database
PDF
This is the official tutorial from Oracle.httpdocs.oracle.comj.pdf
PDF
Spring and dependency injection
DOCX
Java beans
DOC
CRUD(IN_UP_DEL) IN JAVA
DOCX
4.3 Hibernate example.docx
DOCX
Html To JSP
PPT
Introduction to hibernate
KEY
JDBC Basics (In 20 Minutes Flat)
PDF
SummaryHW6 Account ManagementIn HW4, you kept track of multiple.pdf
PDF
Software architecture2008 ejbql-quickref
PPT
Spring data access
Week 12 code
evoting ppt.pptx
Update&delete
Modul Praktek Java OOP
บทที่6 update&delete
This is a basic JAVA pgm that contains all of the major compoents of DB2
Simple Jdbc With Spring 2.5
GAE/J 開発環境でJDO入門
Java Database Connectivity Java Database
This is the official tutorial from Oracle.httpdocs.oracle.comj.pdf
Spring and dependency injection
Java beans
CRUD(IN_UP_DEL) IN JAVA
4.3 Hibernate example.docx
Html To JSP
Introduction to hibernate
JDBC Basics (In 20 Minutes Flat)
SummaryHW6 Account ManagementIn HW4, you kept track of multiple.pdf
Software architecture2008 ejbql-quickref
Spring data access
Ad

More from vacbalolenvadi90 (7)

DOCX
Quiz test JDBC
TXT
Ass2 1 (2)
TXT
PPTX
PDF
TXT
PDF
SQL DdaHKH Huế
Quiz test JDBC
Ass2 1 (2)
SQL DdaHKH Huế

Recently uploaded (20)

PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
Cloud computing and distributed systems.
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
KodekX | Application Modernization Development
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
cuic standard and advanced reporting.pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Big Data Technologies - Introduction.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
Cloud computing and distributed systems.
NewMind AI Weekly Chronicles - August'25 Week I
Reach Out and Touch Someone: Haptics and Empathic Computing
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
KodekX | Application Modernization Development
Digital-Transformation-Roadmap-for-Companies.pptx
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Per capita expenditure prediction using model stacking based on satellite ima...
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
CIFDAQ's Market Insight: SEC Turns Pro Crypto
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
cuic standard and advanced reporting.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Spectral efficient network and resource selection model in 5G networks
Advanced methodologies resolving dimensionality complications for autism neur...
NewMind AI Monthly Chronicles - July 2025
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Network Security Unit 5.pdf for BCA BBA.
Big Data Technologies - Introduction.pptx

Java day9n

  • 1. 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");
  • 2. 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"
  • 3. 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");
  • 4. 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();
  • 5. } 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 .....");
  • 6. 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;
  • 7. 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);
  • 8. } /** * @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;
  • 9. 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);
  • 10. request.setAttribute("accList", accList); RequestDispatcher rd = request.getRequestDispatcher("ShowTranees.jsp"); rd.forward(request, response); } else { response.sendRedirect("Login.jsp"); } } }