package com.igeekhome.tieba.Dao;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.igeekhome.tieba.model.Ba;
import com.igeekhome.tieba.model.BaType;
import com.igeekhome.tieba.model.Member;
import com.igeekhome.tieba.model.User;
import com.igeekhome.util.JDBCUtil;
/***
* 用来封装对吧的各种数据库操作,单例模式
* @author 陈杰
*
*/
public class BaDao
{
private static BaDao instance;
private BaDao()
{
}
public static BaDao getInstance()
{
if(instance != null)
{
synchronized (instance)
{
return instance;
}
}
instance = new BaDao();
return instance;
}
/***
*
* @return 所有的吧列表
*/
public List<Ba> getAllBa()
{
List<Ba> list = new ArrayList<Ba>();
try
{
String sql = "select * from tb_ba";
PreparedStatement ps = JDBCUtil.getInstance().getConnection().prepareStatement(sql);
ResultSet result = ps.executeQuery();
while(result.next())
{
Ba ba = new Ba();
ba.setId(result.getInt("b_id"));
ba.setTypeId(result.getInt("b_type_id"));
BaType type = BarTypeDao.getInstance().getBaTypeByBaId((result.getInt("b_type_id")));
ba.setType(type.getName());
ba.setName(result.getString("b_name"));
ba.setDescription(result.getString("b_description"));
ba.setMemberNum(result.getInt("b_member_num"));
ba.setTieNum(result.getInt("b_tie_num"));
ba.setOwnerId(result.getString("b_owner_user_id"));
ba.setTouxiang(result.getString("b_touxiang"));
list.add(ba);
//System.out.println("查找成功:" + ba);
}
}
catch (SQLException e)
{
System.out.println("查找失败:" + e.getMessage());
return list;
}
return list;
}
/***
*
* @param baId 吧的Id
* @return baId对应的Ba,若没有则返回null
*/
public Ba getBabyId(int baId)
{
Ba ba = null;
try
{
String sql = "select * from tb_ba where b_id = ?";
PreparedStatement ps = JDBCUtil.getInstance().getConnection().prepareStatement(sql);
ps.setInt(1, baId);
ResultSet result = ps.executeQuery();
if(result.next())
{
ba = new Ba();
ba.setId(result.getInt("b_id"));
ba.setTypeId(result.getInt("b_type_id"));
BaType type = BarTypeDao.getInstance().getBaTypeByBaId((result.getInt("b_type_id")));
ba.setType(type.getName());
ba.setName(result.getString("b_name"));
ba.setDescription(result.getString("b_description"));
ba.setMemberNum(result.getInt("b_member_num"));
ba.setTieNum(result.getInt("b_tie_num"));
ba.setOwnerId(result.getString("b_owner_user_id"));
ba.setTouxiang(result.getString("b_touxiang"));
//System.out.println("查找成功:" + ba);
}
}
catch (SQLException e)
{
System.out.println("查找失败:" + e.getMessage());
return ba;
}
return ba;
}
/**
*
* @param ba 要新建的吧
* @return 成功则返回true,失败则返回false
*/
public boolean save(Ba ba)
{
try
{
String sql = "insert into tb_ba(b_type_id, b_name, b_description, b_owner_user_id, b_touxiang) values(?,?,?,?,?)";
PreparedStatement ps = JDBCUtil.getInstance().getConnection().prepareStatement(sql);
ps.setInt(1, ba.getTypeId());
ps.setString(2, ba.getName());
ps.setString(3, ba.getDescription());
ps.setString(4,ba.getOwnerId());
ps.setString(5, ba.getTouxiang());
ps.execute();
return true;
}
catch (SQLException e)
{
return false;
}
}
/**
*
* @param num 要得到的吧的最大数量
* @return 排名前num的吧的集合,如果num<=0,集合为null
*/
public List<Ba> getTopBar(int num)
{
if(num <= 0)
return null;
List<Ba> list = new ArrayList<Ba>();
try
{
String sql = "select * from tb_ba order by b_tie_num DESC limit 0,?";
PreparedStatement ps = JDBCUtil.getInstance().getConnection().prepareStatement(sql);
ps.setInt(1, num);
ResultSet result = ps.executeQuery();
while(result.next())
{
Ba ba = new Ba();
ba.setId(result.getInt("b_id"));
ba.setTypeId(result.getInt("b_type_id"));
BaType type = BarTypeDao.getInstance().getBaTypeByBaId((result.getInt("b_type_id")));
ba.setType(type.getName());
ba.setName(result.getString("b_name"));
ba.setDescription(result.getString("b_description"));
ba.setMemberNum(result.getInt("b_member_num"));
ba.setTieNum(result.getInt("b_tie_num"));
ba.setOwnerId(result.getString("b_owner_user_id"));
ba.setTouxiang(result.getString("b_touxiang"));
list.add(ba);
//System.out.println("查找成功:" + ba);
}
}
catch (SQLException e)
{
System.out.println("查找失败:" + e.getMessage());
return list;
}
return list;
}
/**
*
* @param num 要查询的最大数量
* @return 最近有更新的num个吧
*/
public List<Ba> getBarWhereHasNewTies(int num)
{
if(num <= 0)
return null;
List<Ba> list = new ArrayList<Ba>();
try
{
String sql = "select * from tb_ba where b_id in(select b_id from tb_tie order by t_time DESC) limit 0,?";
PreparedStatement ps = JDBCUtil.getInstance().getConnection().prepareStatement(sql);
ps.setInt(1, num);
ResultSet result = ps.executeQuery();
while(result.next())
{
Ba ba = new Ba();
ba.setId(result.getInt("b_id"));
ba.setTypeId(result.getInt("b_type_id"));
BaType type = BarTypeDao.getInstance().getBaTypeByBaId((result.getInt("b_type_id")));
ba.setType(type.getName());
ba.setName(result.getString("b_name"));
ba.setDescription(result.getString("b_description"));
ba.setMemberNum(result.getInt("b_member_num"));
ba.setTieNum(result.getInt("b_tie_num"));
ba.setOwnerId(result.getString("b_owner_user_id"));
ba.setTouxiang(result.getString("b_touxiang"));
list.add(ba);
//System.out.println("查找成功:" + ba);
}
}
catch (SQLException e)
{
System.out.println("查找失败:" + e.getMessage());
return list;
}
return list;
}
/**
* 通过吧的id删除吧
* @param baId 吧的id
* @return 成功与否
*/
public boolean deleteBaById(int baId)
{
try
{
String sql = "delete from tb_ba where b_id = ?";
PreparedStatement ps = JDBCUtil.getInstance().getConnection().prepareStatement(sql);
ps.setInt(1, baId);
ps.execute();
return true;
}
catch (SQLException e)
{
return false;
}
}
/**
* 删除吧
* @param ba
* @return
*/
public boolean delete(Ba ba)
{
return deleteBaById(ba.getId());
}
/**
* 更新吧
* @param ba
* @return 成功与否
*/
public boolean update(Ba ba)
{
try
{
String sql = "update tb_ba set b_type_id = ? , b_name = ? , b_description = ?, b_owner_user_id = ?, b_touxiang = ? where b_id = ?";
PreparedStatement ps = JDBCUtil.getInstance().getConnection().prepareStatement(sql);
ps.setInt(1, ba.getTypeId());
ps.setString(2, ba.getName());
ps.setString(3, ba.getDescription());
ps.setString(4,ba.getOwnerId());
ps.setString(5, ba.getTouxiang());
ps.setInt(6,ba.getId());
ps.execute();
return true;
}
catch (SQLException e)
{
return false;
}
}
/**
* 得到用户关注的所有吧
* @param userId
* @return
*/
public List<Ba> getAllBaWhoCaresById(String userId)
{
List<Ba> list = new ArrayList<Ba>();
try
{
List<Member> memberList = new ArrayList<Member>();
String sql = "select * from tb_member where m_user_id = ?";
PreparedStatement ps = JDBCUtil.getInstance().getConnection().prepareStatement(sql);
ps.setString(1, userId);
ResultSet result = ps.executeQuery();
while(result.next())
{
Member member = new Member();
member.setM_ba_id(result.getInt("m_ba_id"));
member.setM_user_id(result.getString("m_user_id"));
member.setM_days(result.getInt("m_days"));
memberList.add(member);
}
for(M
没有合适的资源?快使用搜索试试~ 我知道了~
基于JavaWeb的论坛项目
共699个文件
js:300个
css:167个
jpg:38个
1星 需积分: 50 114 下载量 117 浏览量
2018-11-20
15:12:29
上传
评论 14
收藏 6.33MB ZIP 举报
温馨提示
基于JavaWeb的论坛项目,采用JSP Servlet JDBC 单例模式等技术
资源推荐
资源详情
资源评论
收起资源包目录
基于JavaWeb的论坛项目 (699个子文件)
BaDao.class 10KB
ImgUtil.class 8KB
TieDao.class 8KB
MemberDao.class 8KB
ModifyUser.class 7KB
UpdateBaInfo.class 6KB
CommentDAO.class 6KB
ViewTie.class 6KB
BaApplicationDao.class 6KB
FloorDAO.class 6KB
Tie.class 5KB
Register.class 5KB
UserDao.class 5KB
Comment.class 4KB
ViewBar.class 4KB
HtmlUtil.class 4KB
Ba.class 4KB
Floor.class 4KB
Login.class 4KB
ViewBarNextPage.class 3KB
PublishTie.class 3KB
BarTypeDao.class 3KB
BaApplication.class 3KB
User.class 3KB
ReLogin.class 2KB
Exit.class 2KB
BaType.class 2KB
DateUtil.class 2KB
JDBCUtil.class 2KB
BaItem.class 2KB
StringUtil.class 1KB
Member.class 1KB
Test.class 967B
.classpath 583B
org.eclipse.wst.common.component 473B
org.eclipse.wst.jsdt.ui.superType.container 49B
uikit.gradient.css 183KB
uikit.almost-flat.css 180KB
uikit.css 171KB
uikit.gradient.min.css 107KB
uikit.almost-flat.min.css 104KB
uikit.min.css 98KB
samples.css 66KB
editor_ie7.css 33KB
editor_iequirks.css 32KB
editor_ie8.css 32KB
editor_ie.css 31KB
editor_gecko.css 31KB
editor.css 30KB
dialog_ie7.css 15KB
dialog_ie8.css 14KB
dialog_iequirks.css 14KB
dialog_ie.css 14KB
dialog.css 13KB
codemirror.css 8KB
htmleditor.gradient.css 6KB
htmleditor.almost-flat.css 6KB
sample.css 5KB
search.gradient.css 4KB
search.almost-flat.css 4KB
search.css 4KB
htmleditor.css 4KB
tb_second.css 4KB
userUpdate.css 4KB
nestable.gradient.css 4KB
slideshow.gradient.css 3KB
slideshow.css 3KB
slideshow.almost-flat.css 3KB
htmleditor.gradient.min.css 3KB
progress.gradient.css 3KB
nestable.almost-flat.css 3KB
nestable.css 3KB
htmleditor.almost-flat.min.css 3KB
datepicker.gradient.css 3KB
updateTieba.css 3KB
progress.almost-flat.css 3KB
dotnav.gradient.css 3KB
dotnav.almost-flat.css 3KB
datepicker.almost-flat.css 3KB
tb_second.css 3KB
dotnav.css 3KB
notify.gradient.css 3KB
notify.almost-flat.css 3KB
progress.css 2KB
datepicker.css 2KB
tooltip.almost-flat.css 2KB
tooltip.gradient.css 2KB
notify.css 2KB
tooltip.css 2KB
progress.gradient.min.css 2KB
outputxhtml.css 2KB
search.gradient.min.css 2KB
search.almost-flat.min.css 2KB
slideshow.almost-flat.min.css 2KB
slideshow.min.css 2KB
slideshow.gradient.min.css 2KB
search.min.css 2KB
contents.css 2KB
slidenav.gradient.css 2KB
slidenav.almost-flat.css 2KB
共 699 条
- 1
- 2
- 3
- 4
- 5
- 6
- 7
资源评论
- 世纪星星2022-04-02代码运行报错了,不行
土豆拍死马铃薯
- 粉丝: 209
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功