JavaWeb项目开发案例精粹-第2章投票系统-003Dao层
1.
package com.sanqing.dao; import java.util.List; import com.sanqing.bean.Vote;
import com.sanqing.util.Page; public interface VoteDAO {
public void addVote(Vote vote); //添加投票
public void updateVote(Vote vote); //更新投票
public void deleteVote(int voteID); //删除投票
public List<Vote> findAllVote(Page page); //分页查询所有投票
public List<Vote> findVoteByChannel(Page page,int channelID);//分页查询每频道的投票
public Vote findVoteById(int voteID); //通过ID查询投票
public Vote findVoteByName(String voteName); //通过ID查询投票
public int findAllCount(); //查询所有记录数
public int fintCountByChannel(int channelID);//查询每频道下的记录数
}
2.
package com.sanqing.dao; import java.util.List; import com.sanqing.bean.VoteOption; public interface VoteOptionDAO {
public void addVoteOption(VoteOption voteOption); //添加投票选项
public void updateVoteOption(VoteOption voteOption); //更新投票选项
public void deleteVoteOption(int voteOptionID); //删除投票选项
public List<VoteOption> findVoteOptionByVoteID(int voteID); //查询所有投票选项
public VoteOption findVoteOptionById(int voteOptionID); //通过ID查询投票选项
}
3.
package com.sanqing.daoImpl; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List; import com.sanqing.bean.Vote;
import com.sanqing.dao.VoteDAO;
import com.sanqing.util.DBConnection;
import com.sanqing.util.Page; public class VoteDAOImpl implements VoteDAO{ public void addVote(Vote vote) {
Connection conn = DBConnection.getConnection(); //获得连接对象
String addSQL = "insert into " +
"tb_vote(voteName,channelID) values(?,?)";
PreparedStatement pstmt = null; //声明预处理对象
try {
pstmt = conn.prepareStatement(addSQL); //获得预处理对象并赋值
pstmt.setString(1, vote.getVoteName()); //设置投票名称
pstmt.setInt(2, vote.getChannelID()); //设置频道ID
pstmt.executeUpdate(); //执行添加
} catch (SQLException e) {
e.printStackTrace();
} finally{
DBConnection.close(pstmt); //关闭预处理对象
DBConnection.close(conn); //关闭连接对象
}
} public void deleteVote(int voteID) {
Connection conn = DBConnection.getConnection(); //获得连接对象
String deleteSQL = "delete from tb_vote where voteID=?";
PreparedStatement pstmt = null; //声明预处理对象
try {
pstmt = conn.prepareStatement(deleteSQL); //获得预处理对象并赋值
pstmt.setInt(1, voteID); //设置投票编号
pstmt.executeUpdate(); //执行删除
} catch (SQLException e) {
e.printStackTrace();
} finally{
DBConnection.close(pstmt); //关闭预处理对象
DBConnection.close(conn); //关闭连接对象
}
} public List<Vote> findAllVote(Page page) {
Connection conn = DBConnection.getConnection(); //获得连接对象
String findByIDSQL = "select * from tb_vote limit ?,?"; //查询SQL语句
PreparedStatement pstmt = null; //声明预处理对象
ResultSet rs = null;
List<Vote> votes = new ArrayList<Vote>();
try {
pstmt = conn.prepareStatement(findByIDSQL); //获得预处理对象并赋值
pstmt.setInt(1, page.getBeginIndex());
pstmt.setInt(2, page.getEveryPage());
rs = pstmt.executeQuery(); //执行查询
while(rs.next()) {
Vote vote = new Vote();
vote.setVoteID(rs.getInt(1));
vote.setVoteName(rs.getString(2));
vote.setChannelID(rs.getInt(3));
votes.add(vote);
}
} catch (SQLException e) {
e.printStackTrace();
} finally{
DBConnection.close(rs); //关闭结果集对象
DBConnection.close(pstmt); //关闭预处理对象
DBConnection.close(conn); //关闭连接对象
}
return votes;
} public Vote findVoteById(int voteID) {
Connection conn = DBConnection.getConnection(); //获得连接对象
String querySQL = "select * from tb_vote where voteID = ?";
PreparedStatement pstmt = null; //声明预处理对象
ResultSet rs = null;
Vote vote = null;
try {
pstmt = conn.prepareStatement(querySQL); //获得预处理对象并赋值
pstmt.setInt(1, voteID);
rs = pstmt.executeQuery(); //执行查询
if(rs.next()) {
vote = new Vote();
vote.setVoteID(rs.getInt(1));
vote.setVoteName(rs.getString(2));
vote.setChannelID(rs.getInt(3));
}
} catch (SQLException e) {
e.printStackTrace();
} finally{
DBConnection.close(rs); //关闭结果集对象
DBConnection.close(pstmt); //关闭预处理对象
DBConnection.close(conn); //关闭连接对象
}
return vote;
} public void updateVote(Vote vote) { } public Vote findVoteByName(String voteName) {
Connection conn = DBConnection.getConnection(); //获得连接对象
String querySQL = "select * from tb_vote where voteName = ?";
PreparedStatement pstmt = null; //声明预处理对象
ResultSet rs = null;
Vote vote = null;
try {
pstmt = conn.prepareStatement(querySQL); //获得预处理对象并赋值
pstmt.setString(1, voteName);
rs = pstmt.executeQuery(); //执行查询
if(rs.next()) {
vote = new Vote();
vote.setVoteID(rs.getInt(1));
vote.setVoteName(rs.getString(2));
vote.setChannelID(rs.getInt(3));
}
} catch (SQLException e) {
e.printStackTrace();
} finally{
DBConnection.close(rs); //关闭结果集对象
DBConnection.close(pstmt); //关闭预处理对象
DBConnection.close(conn); //关闭连接对象
}
return vote;
} public int findAllCount() {
Connection conn = DBConnection.getConnection(); //获得连接对象
String findSQL = "select count(*) from tb_vote";
PreparedStatement pstmt = null; //声明预处理对象
ResultSet rs = null;
int count = 0;
try {
pstmt = conn.prepareStatement(findSQL); //获得预处理对象并赋值
rs = pstmt.executeQuery(); //执行查询
if(rs.next()) {
count = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
} finally{
DBConnection.close(rs); //关闭结果集对象
DBConnection.close(pstmt); //关闭预处理对象
DBConnection.close(conn); //关闭连接对象
}
return count;
} public List<Vote> findVoteByChannel(Page page, int channelID) {
Connection conn = DBConnection.getConnection(); //获得连接对象
String findByIDSQL = "select * from tb_vote where channelID=? limit ?,?"; //查询SQL语句
PreparedStatement pstmt = null; //声明预处理对象
ResultSet rs = null;
List<Vote> votes = new ArrayList<Vote>();
try {
pstmt = conn.prepareStatement(findByIDSQL); //获得预处理对象并赋值
pstmt.setInt(1, channelID);
pstmt.setInt(2, page.getBeginIndex());
pstmt.setInt(3, page.getEveryPage());
rs = pstmt.executeQuery(); //执行查询
while(rs.next()) {
Vote vote = new Vote();
vote.setVoteID(rs.getInt(1));
vote.setVoteName(rs.getString(2));
vote.setChannelID(rs.getInt(3));
votes.add(vote);
}
} catch (SQLException e) {
e.printStackTrace();
} finally{
DBConnection.close(rs); //关闭结果集对象
DBConnection.close(pstmt); //关闭预处理对象
DBConnection.close(conn); //关闭连接对象
}
return votes;
} public int fintCountByChannel(int channelID) {
Connection conn = DBConnection.getConnection(); //获得连接对象
String findSQL = "select count(*) from tb_vote where channelID=?";
PreparedStatement pstmt = null; //声明预处理对象
ResultSet rs = null;
int count = 0;
try {
pstmt = conn.prepareStatement(findSQL); //获得预处理对象并赋值
pstmt.setInt(1, channelID);
rs = pstmt.executeQuery(); //执行查询
if(rs.next()) {
count = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
} finally{
DBConnection.close(rs); //关闭结果集对象
DBConnection.close(pstmt); //关闭预处理对象
DBConnection.close(conn); //关闭连接对象
}
return count;
}
}
4.
package com.sanqing.daoImpl; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List; import com.sanqing.bean.Vote;
import com.sanqing.bean.VoteOption;
import com.sanqing.dao.VoteOptionDAO;
import com.sanqing.util.DBConnection; public class VoteOptionDAOImpl implements VoteOptionDAO { public void addVoteOption(VoteOption voteOption) {
Connection conn = DBConnection.getConnection(); //获得连接对象
String addSQL = "insert into " +
"tb_voteoption(voteOptionName,voteID,ticketNum) values(?,?,?)";
PreparedStatement pstmt = null; //声明预处理对象
try {
pstmt = conn.prepareStatement(addSQL); //获得预处理对象并赋值
pstmt.setString(1, voteOption.getVoteOptionName()); //设置投票选项名称
pstmt.setInt(2, voteOption.getVoteID()); //设置投票ID
pstmt.setInt(3, voteOption.getTicketNum()); //设置投票数
pstmt.executeUpdate(); //执行添加
} catch (SQLException e) {
e.printStackTrace();
} finally{
DBConnection.close(pstmt); //关闭预处理对象
DBConnection.close(conn); //关闭连接对象
}
} public void deleteVoteOption(int voteOptionID) {
Connection conn = DBConnection.getConnection(); //获得连接对象
String deleteSQL = "delete from tb_voteoption where voteOptionID=?";
PreparedStatement pstmt = null; //声明预处理对象
try {
pstmt = conn.prepareStatement(deleteSQL); //获得预处理对象并赋值
pstmt.setInt(1, voteOptionID); //设置投票编号
pstmt.executeUpdate(); //执行删除
} catch (SQLException e) {
e.printStackTrace();
} finally{
DBConnection.close(pstmt); //关闭预处理对象
DBConnection.close(conn); //关闭连接对象
}
} public List<VoteOption> findVoteOptionByVoteID(int voteID) {
Connection conn = DBConnection.getConnection(); //获得连接对象
String findByIDSQL = "select * from tb_voteoption where voteID = ?";//查询SQL语句
PreparedStatement pstmt = null; //声明预处理对象
ResultSet rs = null;
List<VoteOption> voteOptions = new ArrayList<VoteOption>();
try {
pstmt = conn.prepareStatement(findByIDSQL); //获得预处理对象并赋值
pstmt.setInt(1, voteID);
rs = pstmt.executeQuery(); //执行查询
while(rs.next()) {
VoteOption voteOption = new VoteOption();
voteOption.setVoteOptionID(rs.getInt(1));
voteOption.setVoteID(rs.getInt(2));
voteOption.setVoteOptionName(rs.getString(3));
voteOption.setTicketNum(rs.getInt(4));
voteOptions.add(voteOption);
}
} catch (SQLException e) {
e.printStackTrace();
} finally{
DBConnection.close(rs); //关闭结果集对象
DBConnection.close(pstmt); //关闭预处理对象
DBConnection.close(conn); //关闭连接对象
}
return voteOptions;
} public VoteOption findVoteOptionById(int voteOptionID) {
Connection conn = DBConnection.getConnection(); //获得连接对象
String findByIDSQL = "select * from tb_voteoption where voteOptionID = ?";//查询SQL语句
PreparedStatement pstmt = null; //声明预处理对象
ResultSet rs = null;
VoteOption voteOption = null;
try {
pstmt = conn.prepareStatement(findByIDSQL); //获得预处理对象并赋值
pstmt.setInt(1, voteOptionID);
rs = pstmt.executeQuery(); //执行查询
if(rs.next()) {
voteOption = new VoteOption();
voteOption.setVoteOptionID(rs.getInt(1));
voteOption.setVoteID(rs.getInt(2));
voteOption.setVoteOptionName(rs.getString(3));
voteOption.setTicketNum(rs.getInt(4));
}
} catch (SQLException e) {
e.printStackTrace();
} finally{
DBConnection.close(rs); //关闭结果集对象
DBConnection.close(pstmt); //关闭预处理对象
DBConnection.close(conn); //关闭连接对象
}
return voteOption;
} public void updateVoteOption(VoteOption voteOption) {
Connection conn = DBConnection.getConnection(); //获得连接对象
String deleteSQL = "update tb_voteoption set ticketNum = ? where voteOptionID = ?";
PreparedStatement pstmt = null; //声明预处理对象
try {
pstmt = conn.prepareStatement(deleteSQL); //获得预处理对象并赋值
pstmt.setInt(1, voteOption.getTicketNum()); //设置票数
pstmt.setInt(2, voteOption.getVoteOptionID());
pstmt.executeUpdate(); //执行删除
} catch (SQLException e) {
e.printStackTrace();
} finally{
DBConnection.close(pstmt); //关闭预处理对象
DBConnection.close(conn); //关闭连接对象
}
} }
5.
package com.sanqing.daoFactory; import com.sanqing.dao.VoteDAO;
import com.sanqing.daoImpl.VoteDAOImpl; public class VoteDAOFactory {
public static VoteDAO getVoteDAOInstance(){ //工厂方法,用来返回DAO实现类实例
return new VoteDAOImpl(); //返回DAO实现类实例
}
}
6.
package com.sanqing.daoFactory; import com.sanqing.dao.VoteOptionDAO;
import com.sanqing.daoImpl.VoteOptionDAOImpl; public class VoteOptionDAOFactory {
public static VoteOptionDAO getVoteOptionDAOInstance(){ //工厂方法,用来返回DAO实现类实例
return new VoteOptionDAOImpl(); //返回DAO实现类实例
}
}
JavaWeb项目开发案例精粹-第2章投票系统-003Dao层的更多相关文章
- JavaWeb项目开发案例精粹-第2章投票系统-006view层
1.index.jsp <%@ page language="java" import="java.util.*" pageEncoding=" ...
- JavaWeb项目开发案例精粹-第2章投票系统-004action层
1. package com.sanqing.action; import java.util.UUID; import com.opensymphony.xwork2.ActionSupport; ...
- JavaWeb项目开发案例精粹-第2章投票系统-001设计
1.项目结构 2.数据库设计 # MySQL-Front 5.0 (Build 1.0) /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE */; /*!40101 SET ...
- JavaWeb项目开发案例精粹-第2章投票系统-005实体层
1. package com.sanqing.bean; /** * * 投票选项类 * */ public class VoteOption { private int voteOptionID; ...
- JavaWeb项目开发案例精粹-第2章投票系统-002配置文件及公共类
1. <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5&qu ...
- JavaWeb项目开发案例精粹-第6章报价管理系统-05Action层
0. <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC &quo ...
- JavaWeb项目开发案例精粹-第6章报价管理系统-07View层
1. 2.back_index.html <HTML> <HEAD> <META HTTP-EQUIV="Content-Type" CONTENT= ...
- JavaWeb项目开发案例精粹-第6章报价管理系统-06po层
1. <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www ...
- JavaWeb项目开发案例精粹-第6章报价管理系统-04Service层
1. package com.sanqing.service; import com.sanqing.dao.DAO; import com.sanqing.po.Customer; /** * 客户 ...
随机推荐
- Python脚本控制的WebDriver 常用操作 <二十七> 文件下载
测试用例场景 webdriver允许我们设置默认的文件下载路径.也就是说文件会自动下载并且存在设置的那个目录中. Python脚本 测试用Python代码: # coding=gbk ''' Crea ...
- backbone collection add 事件回调参数
this.listenTo(this.collection, 'add', this.renderBook); renderBook: function (item) { var bookView = ...
- python之参数
1. 参数传递有2种方式: 按位置传递, 按关键字传递. 2. 形参可以定义默认值, 可以用*收集元组, 可以用**收集字典. 其中, (1)指定默认值的形参可不接收实参. (2)指定*的形参用元组收 ...
- 【nodejs】 npm 注意事项
官网:https://www.npmjs.com/ 1.安装时要切换到nodejs根目录, 否则就会安装到安装时所在的目录 2.要有管理员权限(win),如需指定版本,如npm install ex ...
- ios7.0结合storyborad实现页面跳转的总结
折腾了一整天,本文总结一下ios7.0页面跳转有关的内容 storyboard的潜规则 我接触ios很晚,环境已经是xcode5+ios7,所以对以前的IOS开发模式并不了解.在网上查阅了很多资料,发 ...
- _cpluscplus
_cpluscplus是c++中的定义,而c中没有该定义 1.用来判定代码是c类型还是c++类型 2._cplusplus的类型是"long int",值为199711L int ...
- 分析 "ADO" "ADODB" "ADODC" 之间的区别与联系
在敲学生信息管理系统的时候,通过查阅了解了一些关于 ADO / ADODB / ADODC / 的简单描述,想想将他们对 比着总结一下更容易理解记忆.尽管都是一些浅显的东西,不过对像我这样的菜鸟们还是 ...
- cocos中BatchNode精灵集合的使用
1.CCSpriteBatchNode是为了提高渲染效率而实现的,它继承自CCNode 2.fps:帧率,是游戏中衡量流畅度的一个很重要的概念,cocos中默认的帧率是60,即一秒刷新60帧 3.精灵 ...
- UVA 10720 Graph Construction 贪心+优先队列
题目链接: 题目 Graph Construction Time limit: 3.000 seconds 问题描述 Graph is a collection of edges E and vert ...
- nenu contest2
http://vjudge.net/vjudge/contest/view.action?cid=54562#overview H B. Polygons http://codeforces.com ...