给出一个n-tier(层)架构实例,HelloWord例子?
2.n-tier(层)架构实例
马克-to-win:我们把上节的例1.2.1改成一个n-tier的例子。虽然例子很粗糙,但说明理论n-tier理论足够了。在公司还要考虑一些其他问题,比如序列化,事务等等。
例2.1:
add.jsp:
<%@ page contentType="text/html; charset=GBK" %>
<html>
<body>
<center><h3>学生登记系统(for 教务处)</h3></center>
<form action="MarkToWinServlet" method="post">
姓名<INPUT TYPE="TEXT" NAME="name">
年龄<INPUT TYPE="TEXT" NAME="age">
<input type="submit" name="Submit" value="提交">
</form>
</body>
</html>
package com;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletHello1 extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("GBK");
String name = request.getParameter("name");
String age = request.getParameter("age");
RegisterVO rVO=new RegisterVO();
rVO.setName(name);
rVO.setAge(age);
UserServiceMark_to_win us=new UserServiceMark_to_win();
try {
us.register(rVO);
} catch (Exception e) {
e.printStackTrace();
}
response.sendRedirect("add.jsp");
}
}
package com;
import java.sql.*;
public class UserServiceMark_to_win {
public void register(RegisterVO rVO) throws Exception {
RegisterDTO regiDTO=new RegisterDTO();
regiDTO.setName(rVO.getName());
regiDTO.setAge(rVO.getAge());
UserFrontBO ufb= new UserFrontBO();
ufb.register(regiDTO);
}
}
package com;
public class UserFrontBO {
public void register(RegisterDTO regiDTO) throws Exception {
RegisterDO regiDO=new RegisterDO();
regiDO.register(regiDTO);
}
}
package com;
public class RegisterDO {
public void register(RegisterDTO regiDTO) throws Exception {
RegisterPO regiPO= new RegisterPO();
regiPO.setName(regiDTO.getName());
regiPO.setAge(regiDTO.getAge());
RegisterDao regiDao=new RegisterDao();
regiDao.register(regiPO);
}
}
package com;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
public class RegisterDao {
public synchronized void register(RegisterPO regiPO) throws Exception {
String name=regiPO.getName();
String age=regiPO.getAge();
Connection con;
con = DatabaseConn.getConnection();
Statement stmt = con.createStatement();
/*即使表为空,rs.getInt(1)返回值为0,我们的程序逻辑也对*/
ResultSet rs = stmt.executeQuery("select max(id) from register");
rs.next();
System.out.println(rs.getInt(1));
int id;
id=rs.getInt(1);id++;
String insertString="insert into register Values("+id+",\""+name+"\","+Integer.valueOf(age)+")";
System.out.println(insertString);
stmt.executeUpdate(insertString);
stmt.close();
con.close();
}
更多内容请见原文,文章转载自:https://blog.csdn.net/qq_44594371/article/details/103182347