學了有兩年的.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>
<%
}
%>
....
....
....