学了.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> <% } %> .... .... ....
三层架构对于连接数据库非常有用……