-Tạo project File > New File > Other.. > Web > Dynamic Web Project
-Download template Html Download
-Ta bắt tay vào thực hiện việc connect tới Database với Sql Server
Download Driver SQL Server
Đọc thêm ở bài viết này
-Download template Html Download
-Ta bắt tay vào thực hiện việc connect tới Database với Sql Server
Download Driver SQL Server
Đọc thêm ở bài viết này
DButil.java
Java Jsp/Servet 2017
package com.connect; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class DButil { private static Connection connection = null; public static Connection getConnection() { if (connection != null) { return connection; } else { try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); connection = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databasename=MyDatabase", "sa", "12345678"); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return connection; } } // Test connection database public static void main(String[] args) { System.out.println(getConnection()); } }
Person.java
Java Jsp/Servet 2017
package com.fsoft.model;
public class Person {
private String firstName;
private String lastName;
private String gender;
private String phone;
private String email;
private String youarein;
private String hobbis;
private String description;
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getGender() {
return gender;
}
public String getPhone() {
return phone;
}
public String getEmail() {
return email;
}
public String getYouarein() {
return youarein;
}
public String getHobbis() {
return hobbis;
}
public String getDescription() {
return description;
}
public Person(String firstName, String lastName, String gender, String phone, String email, String youarein,
String hobbis, String description) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.gender = gender;
this.phone = phone;
this.email = email;
this.youarein = youarein;
this.hobbis = hobbis;
this.description = description;
}
public Person() {
super();
// TODO Auto-generated constructor stub
}
}
MyDao.java
Java Jsp/Servet 2017
package com.fsoft.controller;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import com.connect.DButil;
import com.fsoft.model.Person;
public class MyDao {
private Connection conn = null;
private PreparedStatement pstmt = null;
private ResultSet rs = null;
public MyDao() {
super();
// TODO Auto-generated constructor stub
conn = DButil.getConnection();
}
// INSERT
public boolean addPerson(Person p) {
boolean b = false;
try {
pstmt = conn.prepareStatement(
"INSERT INTO [dbo].[PERSONAL] ([FIRST_NAME] ,[LAST_NAME] ,[MOBILE],[EMAIL] ,[HOBBIES] ,[DESCRIPTION],[GENDER] ,[YOUAREIN]) VALUES (?,?,?,?,?,?,?,?)");
pstmt.setString(1, p.getFirstName());
pstmt.setString(2, p.getLastName());
pstmt.setString(3, p.getPhone());
pstmt.setString(4, p.getEmail());
pstmt.setString(5, p.getHobbis());
pstmt.setString(6, p.getDescription());
pstmt.setString(7, p.getGender());
pstmt.setString(8, p.getYouarein());
b = pstmt.executeUpdate() > 0;
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return b;
}
//LIST ALL
public ArrayList<Person> showPerson() {
ArrayList<Person> list = new ArrayList();
try {
pstmt = conn.prepareStatement("SELECT * FROM [dbo].[PERSONAL]");
rs = pstmt.executeQuery();
while (rs.next()) {
String firstName = rs.getString("FIRST_NAME");
String lastName = rs.getString("LAST_NAME");
String gender = rs.getString("GENDER");
String phone = rs.getString("MOBILE");
String email = rs.getString("EMAIL");
String youarein = rs.getString("YOUAREIN");
String hobbis = rs.getString("HOBBIES");
String description = rs.getString("DESCRIPTION");
Person person = new Person(firstName, lastName, gender, phone, email, youarein, hobbis, description);
list.add(person);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
//VIEW EDIT
public Person showByName(String first_Name) {
Person p = null;
try {
pstmt = conn.prepareStatement("SELECT * FROM dbo.PERSONAL WHERE FIRST_NAME = ?");
pstmt.setString(1, first_Name);
rs = pstmt.executeQuery();
while (rs.next()) {
String firstName = rs.getString("FIRST_NAME");
String lastName = rs.getString("LAST_NAME");
String gender = rs.getString("GENDER");
String phone = rs.getString("MOBILE");
String email = rs.getString("EMAIL");
String youarein = rs.getString("YOUAREIN");
String hobbis = rs.getString("HOBBIES");
String description = rs.getString("DESCRIPTION");
p = new Person(firstName, lastName, gender, phone, email, youarein, hobbis, description);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return p;
}
//UPDATE
public boolean updatePerson(String firstName, Person p) {
boolean b = false;
try {
pstmt = conn.prepareStatement(
"UPDATE [dbo].[PERSONAL] SET [LAST_NAME] = ?,[MOBILE] = ?,[EMAIL] = ?,[HOBBIES] = ?,[DESCRIPTION] = ?,[GENDER] = ?,[YOUAREIN] = ? WHERE FIRST_NAME =?");
pstmt.setString(1, p.getLastName());
pstmt.setString(2, p.getPhone());
pstmt.setString(3, p.getEmail());
pstmt.setString(4, p.getHobbis());
pstmt.setString(5, p.getDescription());
pstmt.setString(6, p.getGender());
pstmt.setString(7, p.getYouarein());
pstmt.setString(8, firstName);
b = pstmt.executeUpdate() > 0;
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return b;
}
//DELETE
public boolean deletePerson(String firstName) {
boolean b = false;
try {
pstmt = conn.prepareStatement("DELETE FROM dbo.PERSONAL WHERE FIRST_NAME = ?");
pstmt.setString(1, firstName);
b = pstmt.executeUpdate() > 0;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return b;
}
//LIST SEARCH
public ArrayList<Person> listByName(String first_Name) {
ArrayList<Person> list = new ArrayList();
try {
pstmt = conn.prepareStatement("SELECT * FROM [dbo].[PERSONAL] WHERE FIRST_NAME= ? ");
pstmt.setString(1, first_Name);
rs = pstmt.executeQuery();
while (rs.next()) {
String firstName = rs.getString("FIRST_NAME");
String lastName = rs.getString("LAST_NAME");
String gender = rs.getString("GENDER");
String phone = rs.getString("MOBILE");
String email = rs.getString("EMAIL");
String youarein = rs.getString("YOUAREIN");
String hobbis = rs.getString("HOBBIES");
String description = rs.getString("DESCRIPTION");
Person person = new Person(firstName, lastName, gender, phone, email, youarein, hobbis, description);
list.add(person);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
}
Myservlet.java
Java Jsp/Servet 2017
package com.fsoft.controller;
import java.io.IOException;
import java.util.ArrayList;
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 javax.servlet.http.HttpSession;
import com.fsoft.model.Person;
/**
* Servlet implementation class MyServlet
*/
@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public MyServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
String firstName = request.getParameter("firstName");
MyDao my = new MyDao();
ArrayList<Person> list = my.listByName(firstName);
if (list != null) {
request.setAttribute("item", list);
RequestDispatcher rd = request.getRequestDispatcher("Search.jsp");
rd.forward(request, response);
} else {
response.sendRedirect("Help.jsp");
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
MyDao my = new MyDao();
String action = request.getParameter("action");
if (action.equalsIgnoreCase("Register")) {
String firstName = request.getParameter("first_name");
String lastName = request.getParameter("last_name");
String gender = request.getParameter("gender"); // Combobox
String phone = request.getParameter("telephone");
String email = request.getParameter("email");
String youarein = request.getParameter("youarein"); // Radio
String YouAre = "";
try {
if (youarein.equals("Europe")) {
YouAre = "Europe";
} else if (youarein.equals("Africa")) {
YouAre = "Africa";
} else if (youarein.equals("Autralia")) {
YouAre = "Autralia";
} else if (youarein.equals("Asia")) {
YouAre = "Asia";
} else if (youarein.equals("Amarican")) {
YouAre = "Amarican";
}
} catch (Exception e) {
YouAre = "Null";
}
String hobbis = request.getParameter("hobbis"); // Check box
String description = request.getParameter("description");
Person p = new Person(firstName, lastName, gender, phone, email, youarein, hobbis, description);
try {
boolean b = my.addPerson(p); // boolean INSERT personal
if (b) {
ArrayList<Person> list = my.showPerson();
request.setAttribute("item", list);
RequestDispatcher rd = request.getRequestDispatcher("Search.jsp");
rd.forward(request, response);
} else {
response.sendRedirect("Help.jsp");
}
} catch (Exception e) {
e.printStackTrace();
}
// Update Perosnal
} else if (action.equals("Update")) {
String firstName = request.getParameter("first_name");
String lastName = request.getParameter("last_name");
String gender = request.getParameter("gender"); // Combobox
String phone = request.getParameter("telephone");
String email = request.getParameter("email");
String youarein = request.getParameter("youarein"); // Radio
String YouAre = "";
try {
if (youarein.equals("Europe")) {
YouAre = "Europe";
} else if (youarein.equals("Africa")) {
YouAre = "Africa";
} else if (youarein.equals("Autralia")) {
YouAre = "Autralia";
} else if (youarein.equals("Asia")) {
YouAre = "Asia";
} else if (youarein.equals("Amarican")) {
YouAre = "Amarican";
}
} catch (Exception e) {
YouAre = "Null";
}
String hobbis = request.getParameter("hobbis"); // Check box
String description = request.getParameter("description");
Person p = new Person(firstName, lastName, gender, phone, email, youarein, hobbis, description);
boolean b = my.updatePerson(firstName, p); // Boolean UPDATE
// personal
if (b) {
ArrayList<Person> list = my.showPerson();
request.setAttribute("item", list);
RequestDispatcher rd = request.getRequestDispatcher("Search.jsp");
rd.forward(request, response);
} else {
response.sendRedirect("Help.jsp");
}
// Delete Personal
} else if (action.equals("Delete")) {
String firstName = request.getParameter("first_name");
boolean b = my.deletePerson(firstName);
if (b) {
response.sendRedirect("Search.jsp");
} else {
response.sendRedirect("Help.jsp");
}
} else {
// Back Search.jsp
response.sendRedirect(request.getContextPath() + "/Search.jsp");
}
}
public void read() {
}
}
Person.jsp
Java Jsp/Servet 2017
<%@ 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>Visitor Information</title>
<link rel="stylesheet" type="text/css" href="css/java_tech_css.css">
<style>
.help-block {
color: red;
}
</style>
</head>
<body>
<div id="container">
<div id="header">
<img src="images/customLogo.jpg" style="width: 100%" />
</div>
<fieldset>
<h3>Visitor Information</h3>
</fieldset>
<div id="content">
<fieldset id="fiel_2">
<form id="" action="MyServlet" method="POST">
<table id="tablePerson">
<tr>
<td>Fist Name</td>
<td>
<div class="form-group">
<div class="input-group">
<input name="first_name" class="inputText form-control"
type="text" />
</div>
</div>
</td>
</tr>
<tr>
<td>Last Name</td>
<td>
<div class="form-group">
<div class="input-group">
<input name="last_name" class="inputText" type="text" />
</div>
</div>
</td>
</tr>
<tr>
<td>Gender</td>
<td><select name="gender">
<option value="Male">Male</option>
<option value="Female">Female</option>
<option value="Other">Other</option>
</select></td>
</tr>
<tr>
<td>Telephone</td>
<td>
<div class="form-group">
<div class="input-group">
<input name="telephone" class="inputText" type="text" />
</div>
</div>
</td>
</tr>
<tr>
<td>Email</td>
<td>
<div class="form-group">
<div class="input-group">
<input name="email" class="inputText" type="text" />
</div>
</div>
</td>
</tr>
<tr>
<td rowspan="2">You are in</td>
<td>
<div class="form-group">
<div class="input-group">
<input type="radio" name="youarein" value="Europe">Europe
<input type="radio" name="youarein" value="Africa">Africa
</div>
</div>
</td>
</tr>
<tr>
<td><input type="radio" name="youarein" value="Autralia">
Autralia <input type="radio" name="youarein" value="Asia">
Asia <input type="radio" name="youarein" value="Amarican">
Amarican</td>
</tr>
<tr>
<td rowspan="2">What are your hobbis</td>
<td><input type="checkbox" name="hobbis" value="Swimming">Swimming
<input type="checkbox" name="hobbis" value="Cooking">Cooking
<input type="checkbox" name="hobbis" value="Shopping">Shopping
</td>
</tr>
<tr>
<td><input type="checkbox" name="hobbis" value="Sport">
Sport <input type="checkbox" name="hobbis" value="Dance">Dance
<input type="checkbox" name="hobbis" value="Sing">Sing</td>
</tr>
<tr>
<td>Description</td>
<td>
<div class="form-group">
<div class="input-group">
<textarea name="description" rows="6" cols="30"></textarea>
</div>
</div>
</td>
</tr>
</table>
<center>
<input type="Submit" value="Register" id="buttonCenter"
name="action" />
</center>
</form>
</fieldset>
<div id="page"></div>
</div>
</div>
<script
src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script
src='http://cdnjs.cloudflare.com/ajax/libs/bootstrap-validator/0.4.5/js/bootstrapvalidator.min.js'></script>
<script src="js/validate.js"></script>
</body>
</html>
Search.jsp
Java Jsp/Servet 2017
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="com.fsoft.controller.*"%>
<%@page import="com.fsoft.model.Person"%>
<!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>
<title>Search Visitor</title>
<link rel="stylesheet" type="text/css" href="css/java_tech_css.css">
</head>
<body>
<div id="container">
<div id="header">
<img src="images/customLogo.jpg" style="width: 100%" />
</div>
<fieldset>
<h3>Search Visitor</h3>
<div id="searchNav">
<form action="MyServlet" method="get">
Firstname: <input class="inputText_search" type="text"
name="firstName" /> <input type="submit" value="Search.." />
(Type First name vistor and Click Search button)
</form>
</div>
</fieldset>
<div id="content">
<table id="tableSearch">
<tr>
<th id="ths">Firstname</th>
<th id="ths">Lastname</th>
<th id="ths">Gender</th>
<th id="ths">Telephone</th>
<th id="ths">You're in</th>
<th id="ths">Hobbies</th>
<th id="ths" class="des">Description</th>
</tr>
<%
List<Person> list;
if (request.getAttribute("item") != null) {
list = (ArrayList) request.getAttribute("item");
} else {
list = new MyDao().showPerson();
}
for (Person p : list) {
%>
<tr>
<td id="tds"><a
href="Edit.jsp?firstName=<%=p.getFirstName()%>"><%=p.getFirstName()%></a></td>
<td id="tds"><%=p.getLastName()%></td>
<td id="tds"><%=p.getGender()%></td>
<td id="tds"><%=p.getPhone()%></td>
<td id="tds"><%=p.getYouarein()%></td>
<td id="tds"><%=p.getHobbis()%></td>
<td id="tds"><%=p.getDescription()%></td>
</tr>
<%
}
%>
</table>
</div>
</div>
</body>
</html>
Edit.jsp
Java Jsp/Servet 2017
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="com.fsoft.model.*"%>
<%@ page import="com.fsoft.controller.*"%>
<!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>Visitor Information</title>
<link rel="stylesheet" type="text/css" href="css/java_tech_css.css">
<style>
.help-block {
color: red;
}
</style>
</head>
<body>
<div id="container">
<div id="header">
<img src="images/customLogo.jpg" style="width: 100%" />
</div>
<fieldset>
<h3>Visitor Information</h3>
<%
Person p = new MyDao().showByName(request.getParameter("firstName"));
%>
</fieldset>
<div id="content">
<fieldset id="fiel_2">
<form name="frm" id="" action="MyServlet" method="POST">
<table id="tablePerson">
<tr>
<td>Fist Name</td>
<td>
<div class="form-group">
<div class="input-group">
<input name="first_name" class="inputText form-control"
value="<%=p.getFirstName()%>" type="text" />
</div>
</div>
</td>
</tr>
<tr>
<td>Last Name</td>
<td>
<div class="form-group">
<div class="input-group">
<input name="last_name" class="inputText" type="text"
value="<%=p.getLastName()%>" />
</div>
</div>
</td>
</tr>
<tr>
<td>Gender</td>
<td><select name="gender">
<option value="Male">Male</option>
<option value="Female">Female</option>
<option value="Other">Other</option>
</select></td>
</tr>
<tr>
<td>Telephone</td>
<td>
<div class="form-group">
<div class="input-group">
<input name="telephone" class="inputText" type="text"
value="<%=p.getPhone()%>" />
</div>
</div>
</td>
</tr>
<tr>
<td>Email</td>
<td>
<div class="form-group">
<div class="input-group">
<input name="email" class="inputText" type="text"
value="<%=p.getEmail()%>" />
</div>
</div>
</td>
</tr>
<tr>
<td rowspan="2">You are in</td>
<td>
<div class="form-group">
<div class="input-group">
<input type="radio" name="youarein" value="Europe">Europe
<input type="radio" name="youarein" value="Africa">Africa
</div>
</div>
</td>
</tr>
<tr>
<td><input type="radio" name="youarein" value="Autralia">
Autralia <input type="radio" name="youarein" value="Asia">
Asia <input type="radio" name="youarein" value="Amarican">
Amarican</td>
</tr>
<tr>
<td rowspan="2">What are your hobbis</td>
<td><input type="checkbox" name="hobbis" value="Swimming">Swimming
<input type="checkbox" name="hobbis" value="Cooking">Cooking
<input type="checkbox" name="hobbis" value="Shopping">Shopping
</td>
</tr>
<tr>
<td><input type="checkbox" name="hobbis" value="Sport">
Sport <input type="checkbox" name="hobbis" value="Dance">Dance
<input type="checkbox" name="hobbis" value="Sing">Sing</td>
</tr>
<tr>
<td>Description</td>
<td>
<div class="form-group">
<div class="input-group">
<textarea name="description" rows="6" cols="30"><%=p.getDescription()%></textarea>
</div>
</div>
</td>
</tr>
</table>
<center>
<input type="Submit" value="Update" name="action" />
<input type="Submit" value="Delete" name="action" />
<input type="Submit" value="Back" name="action" />
</center>
</form>
</fieldset>
<div id="page"></div>
</div>
</div>
<script
src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script
src='http://cdnjs.cloudflare.com/ajax/libs/bootstrap-validator/0.4.5/js/bootstrapvalidator.min.js'></script>
<script src="js/validate.js"></script>
</body>
</html>
0 nhận xét:
Post a Comment