jsp的三層架構

來源:互聯網
上載者:User

學了有兩年的.net了,它的三層架構很好用。現在學jsp,我們一樣可以用三層架構來開發。下面詳細介紹

1)建立資料庫

drop table MyUser
create table MyUser
(
id int identity(1,1) not null,
name Varchar(50),
psw Varchar(50),
nickname Varchar(50),
age int,
email Varchar(50)
)

2)model層

package model;

public class MyUser {
 private int id;
 private String name;
 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 getPsw() {
  return psw;
 }
 public void setPsw(String psw) {
  this.psw = psw;
 }
 public String getNickname() {
  return nickname;
 }
 public void setNickname(String nickname) {
  this.nickname = nickname;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 public String getEmail() {
  return email;
 }
 public void setEmail(String email) {
  this.email = email;
 }
 private String psw;
 private String nickname;
 private int age;
 private String email;

}

3)資料庫的連結

package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DB {
 public static Connection createConn() {
  Connection conn = null;
  try {
   Class.forName("net.sourceforge.jtds.jdbc.Driver");
   conn = DriverManager.getConnection("jdbc:jtds:sqlserver://localhost:1433/jsp", "sa", "123456");
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  } catch (SQLException e) {
   e.printStackTrace();
  }
  return conn;
 }
 
 public static PreparedStatement prepare(Connection conn, String sql) {
  PreparedStatement ps = null;
  try {
   ps = conn.prepareStatement(sql);
  } catch (SQLException e) {
   e.printStackTrace();
  }
  return ps;
 }
 
 public static void close(Connection conn) {
  
  try {
   conn.close();
   conn = null;
  } catch (SQLException e) {
   e.printStackTrace();
  }
 }
 public static void close(Statement stmt) {
  try {
   stmt.close();
   stmt = null;
  } catch (SQLException e) {
   e.printStackTrace();
  }
 }
 public static void close(ResultSet rs) {
  try {
   rs.close();
   rs = null;
  } catch (SQLException e) {
   e.printStackTrace();
  }
 }
}

4)service層

package service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import util.DB;
import model.MyUser;

public class MyUserService {

 public static void add(MyUser user) {
  Connection conn = DB.createConn();
  String sql = "insert into MyUser values(?,?,?,?,?)";
  PreparedStatement ps = DB.prepare(conn, sql);
  try {
   ps.setString(1, user.getName());
   ps.setString(2, user.getPsw());
   ps.setString(3, user.getNickname());
   ps.setInt(4, user.getAge());
   ps.setString(5, user.getEmail());
   ps.executeUpdate();
  } catch (SQLException e) {
   e.printStackTrace();
  }
  DB.close(ps);
  DB.close(conn);
 }
 
 public static MyUser getByName(String name){
  Connection conn = DB.createConn();
  String sql = "select * from MyUser where name = ?";
  PreparedStatement ps =DB.prepare(conn, sql);
  MyUser user=null;
  try {
   ps.setString(1, name);
   ResultSet rs = ps.executeQuery();
  
   if(rs.next()) {
    user = new MyUser();
    user.setId(rs.getInt("id"));
    user.setName(rs.getString("name"));
    user.setPsw(rs.getString("psw"));
    user.setNickname(rs.getString("nickname"));
    user.setAge(rs.getInt("age"));
    user.setEmail(rs.getString("email"));

   }
  } catch (SQLException e) {
   e.printStackTrace();
  }
  DB.close(ps);
  DB.close(conn);
  return user;
 }
}

5)頁面層調用service層的方法就行了

<%
String name=request.getParameter("name");
String psw=request.getParameter("psw");
String nickname=request.getParameter("nickname");
int age=Integer.valueOf(request.getParameter("age"));
String email=request.getParameter("email");
MyUser newUser=new MyUser();
newUser.setName(name);
newUser.setPsw(psw);
newUser.setNickname(nickname);
newUser.setAge(age);
newUser.setEmail(email);

MyUser user=MyUserService.getByName(name);
if(user!=null){ %>
<jsp:forward page="error.jsp"></jsp:forward>
<%}
else
{
 MyUserService.add(newUser);
%>
<jsp:forward page="success.jsp"></jsp:forward>
<% 

}
%>

 

....

....

....

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.