给出一个n-tier(层)架构实例,HelloWord例子
n-tier(层)架构实例
马克- to-win:马克 java社区:防盗版实名手机尾号: 73203。
马克-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();
}
}
package com;
import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource;
import java.sql.*;
import javax.sql.*;
public class DatabaseConn {
static private MysqlConnectionPoolDataSource ds;
private Connection con;
private DatabaseConn() {
ds = new MysqlConnectionPoolDataSource();
ds.setURL("jdbc:mysql://localhost:3306/test");
ds.setUser("root");
ds.setPassword("1234");
}
public static Connection getConnection() throws Exception {
if (ds == null) {
/*DatabaseConn的目的是让ds有值,和con无关*/
new DatabaseConn();
}
Connection con = null;
con = ds.getConnection();
return con;
}
}
package com;
public class RegisterVO{
String name;
String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
package com;
public class RegisterDTO {
String name;
String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
package com;
public class RegisterPO {
String name;
String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}