三层架构
界面层(User Interface layer)
业务逻辑层(Business Logic Layer)
数据访问层(Data access layer)
区分层次的目的即为了“高内聚低耦合”的思想
添加学生的简单代码
表示层
- 添加界面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="AddStudentServlet" method="post">
学号 <input type="text" name="uid"><p>
用户名<input type="text" name="uname"><p>
密码 <input type="text" name="upwd"><p>
年龄<input type="text" name="uage"><p>
住址<input type="text" name="uaddress"><p>
<input type="submit" value="增加 ">
</form>
</body>
</html>
- 成功界面
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style type="text/css">
.container{
width: 10%;
height: 10%;
}
.wz{
font-size:20px;
}
</style>
</head>
<body>
<div class="container">
<font class="wz">添加成功</font>
</div>
</body>
</html>
- 失败界面
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style type="text/css">
.container{
width: 10%;
height: 10%;
}
.wz{
font-size:20px;
}
</style>
</head>
<body>
<div class="container">
<font class="wz">添加失败</font>
</div>
</body>
</html>
- 后台代码servlet
package org.stu.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.stu.entity.Student;
import org.stu.service.AddStudentService;
public class AddStudentServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
int id=Integer.parseInt(request.getParameter("uid"));
String name=request.getParameter("uname");
String upwd=request.getParameter("upwd");
int uage=Integer.parseInt(request.getParameter("uage"));
String uaddress=request.getParameter("uaddress");
Student stu=new Student(id,name,upwd,uage,uaddress);
// request.setCharacterEncoding("utf-8");
// response.setContentType("text/html; charset=UTF-8");
// PrintWriter out = response.getWriter();
AddStudentService add=new AddStudentService();
if(add.addStudent(stu)==1)
{
request.getRequestDispatcher("welcome.html").forward(request,response);
}else
request.getRequestDispatcher("Fail.html").forward(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
业务逻辑层:将数据访问层的代码进行组装
package org.stu.service;
import org.stu.dao.StudentDao;
import org.stu.entity.Student;
public class AddStudentService {
public int addStudent(Student stu)
{
StudentDao s=new StudentDao ();
Boolean exit = s.isExist(stu);
if(exit==true)
{
s.addStudent(stu);
return 1;
}
else
{
return 0;
}
}
}
- 数据访问层:直接对数据库进行操作
package org.stu.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.stu.entity.Student;
import org.stu.utils.JDBCUtils;
//增加,查询,原子层
public class StudentDao {
public Boolean isExist(Student stu)
{
if (queryStudent(stu) ==null)
return true;
else
return false;
}
public void addStudent(Student stu)
{
String sql="insert into student values(?,?,?,?,?)";
Connection conn=null;
PreparedStatement ps=null;
try {
conn = JDBCUtils.getConnection();
ps = conn.prepareStatement(sql);
ps.setObject(1, stu.getId());
ps.setObject(2, stu.getName());
ps.setObject(3, stu.getPassword());
ps.setObject(4, stu.getAge());
ps.setObject(5, stu.getAddress());
ps.execute();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
JDBCUtils.closeResourse(conn, ps);
}
}
public Student queryStudent(Student stu)
{
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
conn = JDBCUtils.getConnection();
String sql="select * from student where id=?";
ps = conn.prepareStatement(sql);
ps.setObject(1, stu.getId());
rs = ps.executeQuery();
if(rs.next())
return stu;
else
return null;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
finally {
JDBCUtils.closeResourse(conn, ps, rs);
}
}
}
-对数据库中的表进行封装
package org.stu.entity;
public class Student {
private int id;
private String name;
private String password;
private int age;
private String address;
public Student(int id, String name, String password, int age, String address) {
super();
this.id = id;
this.name = name;
this.password = password;
this.age = age;
this.address = address;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
- 工具
package org.stu.utils;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
public class JDBCUtils {
public static Connection getConnection() throws Exception {
String user="root";
String url="jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=utf-8&useSSL=false";
String password="root";
String driverClass="com.mysql.jdbc.Driver";
Class.forName(driverClass);
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
}
public static void closeResourse(Connection conn, PreparedStatement ps, ResultSet rs) {
try {
if(rs!=null)
rs.close();
if(ps!=null)
ps.close();
if(conn!=null)
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void closeResourse(Connection conn, PreparedStatement ps) {
try {
if(ps!=null)
ps.close();
if(conn!=null)
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
规范
- 命名:包名
- 面向接口编程,类名和接口名命名规范