图5.2 博客后台管理界面图
5.2公共核心类的设计
5.2.1数据库相关
访问数据库相关类如下:
a)用静态域加载数据库驱动,其代码如下: static { try{
Class.forName(\ }catch(Exception e){
System.out.println(\数据库加载失败!\ }
}
b)连接数据库和操作数据库;代码如下: // 构造数据库的连接和访问类
public boolean creatConnection(){ try{
conn=DriverManager.getConnection(url,userName,password); conn.setAutoCommit(true); }catch(SQLException e){
System.out.println(e.getMessage()); }
return true; }
21
public boolean executeUpdate(String sql){ if(conn==null){
creatConnection(); } try{
//创建SQL语句执行对象.
Statement stmt=conn.createStatement(); int iCount=stmt.executeUpdate(sql); return true;
}catch(SQLException e){ return false; } }
public ResultSet executeQuery(String sql){ ResultSet rs=null; try{
if(conn==null){
creatConnection(); }
Statement stmt=conn.createStatement(); try{
//将查询到的结果存放到结果集里。 rs=stmt.executeQuery(sql); }catch(SQLException e){
System.out.println(e.getMessage()); }
}catch(SQLException e){
System.out.println(e.getMessage()); }
return rs; }
getConnection()方法:该方法主要负责获得数据库连接对象。 executeQuery()方法:该方法封装数据库的查询操作。
executeUpdate()方法:该方法封装数据库的更新操作,如添加、删除、修改。
5.2.2编写JavaBean类
为每一个数据库表建一个JavaBean类,完成对该表字段的获取和设置。 博客信息管理系统所用的JavaBean类列表如表5.1所示。
表5.1 JavaBean类列表
JavaBean类 ArticleForm类
说明
文章表对应的JavaBean类
22
ArticleTypeForm类 ConsumerForm类 FriendForm类 PhotoForm类 RestoreForm类
文章类型表对应的JavaBean类 用户表对应的JavaBean类 好友表对应的JavaBean类 相册表对应的JavaBean类 评论表对应的JavaBean类
例如ArticleForm类的部分代码如下: public class ArticleForm{
// 属性,对应数据表中的字段名 private String title=“”; // 字段的访问和设置方法 public String getTitle() { return title; }
public void setTitle(String title) { this.title = title;
}
ArticleForm类:通过getTitle()方法获取title属性的值,通过setTitle(String title)方法设置title属性的值。 5.2.3管理类设计
上面每一个JavaBean类都有一个相对应的管理类,是在JavaBean类及数据库相关类的基础上完成对数据库表的访问、更新、统计操作。
博客信息管理系统所用的管理类列表如表5.2所示。
表5.2 管理类列表
管理类 ArticleDao类 ArticleTypeDao类 ConsumerDao类 FriendDao类 PhotoDao类 RestoreDao类
说明
完成对日志表的查询、更新及统计 完成对日志类型表的查询、更新及统计 完成对用户表的查询、更新及统计 完成对好友表的查询、更新及统计 完成对相册表的查询、更新及统计 完成对评论表的查询、更新及统计
ArticleDao类的部分代码如下:
public boolean operationArticle (String operation,ArticleForm form) { boolean flag = false; String sql = null; if(operation.equals(\添加\ sql = \values (\
+form.getContent()+\
23
etUser_id()+\ if(operation.equals(\修改\ sql = \tb_article set typeId = \='\ +form.getContent()+\ if(operation.equals(\删除\ sql = \ if(operation.equals(\增加\ sql = \tb_article set number = number + 1 where id = \ System.out.print(sql); if(connection.executeUpdate(sql)) { flag = true; } return flag; }
//根据用户编号和日志类型编号查询文章。 public List queryArticle(Integer typeId,Integer user_id){ List list = new ArrayList(); String sql = null; if(typeId == null) sql = \ else sql = \* from tb_article where typeId = '\and user_id = '\ ResultSet rs = connection.executeQuery(sql); try { while(rs.next()) { articleForm = new ArticleForm(); articleForm.setId(rs.getInt(\ articleForm.setTypeId(rs.getInt(\ articleForm.setTitle(rs.getString(\ articleForm.setContent(rs.getString(\ articleForm.setPhTime(rs.getString(\
articleForm.setNumber(Integer.parseInt(rs.getString(\
24
list.add(articleForm); } } catch (SQLException e) { e.printStackTrace(); } return list; }
//从日志表中查询所有日志。 public ArticleForm queryArticleForm(int id) { String sql = \ ResultSet rs = connection.executeQuery(sql); try { while (rs.next()) { articleForm = new ArticleForm(); articleForm.setId(rs.getInt(\ articleForm.setTypeId(rs.getInt(\ articleForm.setTitle(rs.getString(\ articleForm.setContent(rs.getString(\ articleForm.setPhTime(rs.getString(\ articleForm.setNumber(rs.getInt(\ } }catch(SQLException e){ e.printStackTrace(); } this.operationArticle(\增加\ return articleForm; }
//查询日志总数。 public int getCount(int user_id) { int count = 0; String sql = \count(id) from tb_article where user_id=\ ResultSet rs = connection.executeQuery(sql); try{ rs.next(); count = rs.getInt(1); }catch(SQLException e){ e.printStackTrace(); } return count; }
ArticleDao类:封装了对日志表的所有操作,包括添加日志、更新日志、删除日志、查询日志等。
25
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库基于J2EE的博客网站论文(6)在线全文阅读。
相关推荐: