MVC模式
M层(Model层)代码如下:
package muta.mvc.vo;
/**
* @author help
*封装管理员属性,值对象
*/
public class Person {
private String id; //用户ID
private String name; //用户姓名
private String password;//用户密码
//生成 getter方法 与 setter方法
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
package muta.mvc.vo;
/**
* @author help
* 封装 留言信息的属性,值对象
*/
public class Note {
private int id ; //留言ID
private String title ; //留言题目
private String author ;//留言作者
private String content ;//留言内容
//生成 getter方法 与 setter方法
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
############################################################
接口DAO定义如下:
1.PersonDAO
package muta.mvc.dao; import muta.mvc.vo.*; /** * @author help *做登陆验证 *接口只是用来声明方法,不需要实现 */ public interface PersonDAO { // 做登陆验证 public boolean login(Person person) throws Exception ; }
2.NoteDAO
package muta.mvc.dao; import java.util.* ; import muta.mvc.vo.*; /** * @author help * 对留言的操作方法声明,不需要实现 * */ public interface NoteDAO { // 增加操作 public void insert(Note note) throws Exception ; // 修改操作 public void update(Note note) throws Exception ; // 删除操作 public void delete(int id) throws Exception ; // 按ID查询,主要为更新使用 public Note queryById(int id) throws Exception ; // 查询全部 public List queryAll() throws Exception ; // 模糊查询 public List queryByLike(String cond) throws Exception ; }
***************************************************************************
DAO 实现方法如下:
1.PersonDAOImpl.java
package muta.mvc.impl; import java.sql.*; import muta.mvc.dao.PersonDAO; import muta.mvc.database.DataBaseConnection; import muta.mvc.vo.Person; /** * @author help *实现具体的用户登录验证方法 * 判断是否是正确的用户名或密码 * 从数据库中取出用户的真实姓名 */ public class PersonDAOImpl implements PersonDAO { /* (non-Javadoc) * @see muta.mvc.dao.PersonDAO#login(muta.mvc.vo.Person) */ @Override public boolean login(Person person) throws Exception { boolean flag = false ; String sql = "SELECT name FROM person WHERE id=? and password=?" ; PreparedStatement pstmt = null ; DataBaseConnection dbc = null ; dbc = new DataBaseConnection() ; try { pstmt = dbc.getConnection().prepareStatement(sql) ; pstmt.setString(1,person.getId()) ; pstmt.setString(2,person.getPassword()) ; ResultSet rs = pstmt.executeQuery() ; if(rs.next()) { flag = true ; person.setName(rs.getString(1)) ; } rs.close() ; pstmt.close() ; } catch (Exception e) { throw new Exception("操作出现错误!!!") ; } finally { dbc.close() ; } return flag ; } };
2.NoteDAOImpl.java
package muta.mvc.impl; import java.util.ArrayList; import java.util.List; import java.sql.*; import muta.mvc.dao.NoteDAO; import muta.mvc.vo.Note; import muta.mvc.database.*; /** * @author help *实现对“留言”操作的各个方法 */ public class NoteDAOImpl implements NoteDAO { /* (non-Javadoc) * @see muta.mvc.dao.NoteDAO#insert(muta.mvc.vo.Note) */ @Override public void insert(Note note) throws Exception { // TODO Auto-generated method stub String sql = "INSERT INTO note(id,title,author,content) VALUES(note_sequ.nextVal,?,?,?)" ; PreparedStatement pstmt = null ; DataBaseConnection dbc = null ; dbc = new DataBaseConnection() ; try { pstmt = dbc.getConnection().prepareStatement(sql) ; pstmt.setString(1,note.getTitle()) ; pstmt.setString(2,note.getAuthor()) ; pstmt.setString(3,note.getContent()) ; pstmt.executeUpdate() ; pstmt.close() ; } catch (Exception e) { // System.out.println(e) ; throw new Exception("操作中出现错误!!!") ; } finally { dbc.close() ; } } /* (non-Javadoc) * @see muta.mvc.dao.NoteDAO#update(muta.mvc.vo.Note) */ @Override public void update(Note note) throws Exception { // TODO Auto-generated method stub String sql = "UPDATE note SET title=?,author=?,content=? WHERE id=?" ; PreparedStatement pstmt = null ; DataBaseConnection dbc = null ; dbc = new DataBaseConnection() ; try { pstmt = dbc.getConnection().prepareStatement(sql) ; pstmt.setString(1,note.getTitle()) ; pstmt.setString(2,note.getAuthor()) ; pstmt.setString(3,note.getContent()) ; pstmt.setInt(4,note.getId()) ; pstmt.executeUpdate() ; pstmt.close() ; } catch (Exception e) { throw new Exception("操作中出现错误!!!") ; } finally { dbc.close() ; } } /* (non-Javadoc) * @see muta.mvc.dao.NoteDAO#delete(int) */ @Override public void delete(int id) throws Exception { // TODO Auto-generated method stub String sql = "DELETE FROM note WHERE id=?" ; PreparedStatement pstmt = null ; DataBaseConnection dbc = null ; dbc = new DataBaseConnection() ; try { pstmt = dbc.getConnection().prepareStatement(sql) ; pstmt.setInt(1,id) ; pstmt.executeUpdate() ; pstmt.close() ; } catch (Exception e) { throw new Exception("操作中出现错误!!!") ; } finally { dbc.close() ; } } /* (non-Javadoc) * @see muta.mvc.dao.NoteDAO#queryById(int) */ @Override public Note queryById(int id) throws Exception { // TODO Auto-generated method stub Note note = null ; String sql = "SELECT id,title,author,content FROM note WHERE id=?" ; PreparedStatement pstmt = null ; DataBaseConnection dbc = null ; dbc = new DataBaseConnection() ; try { pstmt = dbc.getConnection().prepareStatement(sql) ; pstmt.setInt(1,id) ; ResultSet rs = pstmt.executeQuery() ; if(rs.next()) { note = new Note() ; note.setId(rs.getInt(1)) ; note.setTitle(rs.getString(2)) ; note.setAuthor(rs.getString(3)) ; note.setContent(rs.getString(4)) ; } rs.close() ; pstmt.close() ; } catch (Exception e) { throw new Exception("操作中出现错误!!!") ; } finally { dbc.close() ; } return note ; } /* (non-Javadoc) * @see muta.mvc.dao.NoteDAO#queryAll() */ @Override public List queryAll() throws Exception { // TODO Auto-generated method stub List all = new ArrayList() ; String sql = "SELECT id,title,author,content FROM note" ; PreparedStatement pstmt = null ; DataBaseConnection dbc = null ; dbc = new DataBaseConnection() ; try { pstmt = dbc.getConnection().prepareStatement(sql) ; ResultSet rs = pstmt.executeQuery() ; while(rs.next()) { Note note = new Note() ; note.setId(rs.getInt(1)) ; note.setTitle(rs.getString(2)) ; note.setAuthor(rs.getString(3)) ; note.setContent(rs.getString(4)) ; all.add(note) ; } rs.close() ; pstmt.close() ; } catch (Exception e) { System.out.println(e) ; throw new Exception("操作中出现错误!!!") ; } finally { dbc.close() ; } return all ; } /* (non-Javadoc) * @see muta.mvc.dao.NoteDAO#queryByLike(java.lang.String) */ @Override public List queryByLike(String cond) throws Exception { // TODO Auto-generated method stub List all = new ArrayList() ; String sql = "SELECT id,title,author,content FROM note WHERE title LIKE ? or AUTHOR LIKE ? or CONTENT LIKE ?" ; PreparedStatement pstmt = null ; DataBaseConnection dbc = null ; dbc = new DataBaseConnection() ; try { pstmt = dbc.getConnection().prepareStatement(sql) ; pstmt.setString(1,"%"+cond+"%") ; pstmt.setString(2,"%"+cond+"%") ; pstmt.setString(3,"%"+cond+"%") ; ResultSet rs = pstmt.executeQuery() ; while(rs.next()) { Note note = new Note() ; note.setId(rs.getInt(1)) ; note.setTitle(rs.getString(2)) ; note.setAuthor(rs.getString(3)) ; note.setContent(rs.getString(4)) ; all.add(note) ; } rs.close() ; pstmt.close() ; } catch (Exception e) { System.out.println(e) ; throw new Exception("操作中出现错误!!!") ; } finally { dbc.close() ; } return all ; } }