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层的更多相关文章

  1. JavaWeb项目开发案例精粹-第2章投票系统-006view层

    1.index.jsp <%@ page language="java" import="java.util.*" pageEncoding=" ...

  2. JavaWeb项目开发案例精粹-第2章投票系统-004action层

    1. package com.sanqing.action; import java.util.UUID; import com.opensymphony.xwork2.ActionSupport; ...

  3. JavaWeb项目开发案例精粹-第2章投票系统-001设计

    1.项目结构 2.数据库设计 # MySQL-Front 5.0 (Build 1.0) /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE */; /*!40101 SET ...

  4. JavaWeb项目开发案例精粹-第2章投票系统-005实体层

    1. package com.sanqing.bean; /** * * 投票选项类 * */ public class VoteOption { private int voteOptionID; ...

  5. JavaWeb项目开发案例精粹-第2章投票系统-002配置文件及公共类

    1. <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5&qu ...

  6. JavaWeb项目开发案例精粹-第6章报价管理系统-05Action层

    0. <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC &quo ...

  7. JavaWeb项目开发案例精粹-第6章报价管理系统-07View层

    1. 2.back_index.html <HTML> <HEAD> <META HTTP-EQUIV="Content-Type" CONTENT= ...

  8. JavaWeb项目开发案例精粹-第6章报价管理系统-06po层

    1. <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www ...

  9. JavaWeb项目开发案例精粹-第6章报价管理系统-04Service层

    1. package com.sanqing.service; import com.sanqing.dao.DAO; import com.sanqing.po.Customer; /** * 客户 ...

随机推荐

  1. MVC 5.0(or5.0↓) Ajax.BeginForm 异步上传附件问题,答案是不能的!

    MVC 5.0(or5.0↓)  Ajax.BeginForm 异步上传附件问题,答案是不能的! (请注意我这里说的异步!) 来看一下下面这段一步提交file的代码 //前台 .cshtml 文件 & ...

  2. 非阻塞io与记录锁

    非阻塞io 1.对比 阻塞io的例子:scanf从终端获取输入时,如果不输入程序就会一直停在那; 对一个已经有写锁的文件请求读时, 会一直空等直到前面的进程释放锁... 非阻塞的例子:读取文件内容, ...

  3. 十一、 BOOL类型、分支结构和关系运算符

    BOOL类型:表示非真即假.只有两个值:YES和NO,而二进制只识别二进制数,所以,将YES替换为“1”,NO替换为“0” BOOL数据类型占一字节的空间内存 BOOL数据类型输出为:%lu:输入为: ...

  4. 【BZOJ 1068】[SCOI2007]压缩

    Description 给 一个由小写字母组成的字符串,我们可以用一种简单的方法来压缩其中的重复信息.压缩后的字符串除了小写字母外还可以(但不必)包含大写字母R与M,其中M 标记重复串的开始,R重复从 ...

  5. 史上最全的Excel数据编辑处理技巧(转)

    史上最全的数据编辑处理技巧,让你在日常数据分析处理的疯魔状态中解放出来. 一.隐藏行列 “不得了了,Excel出现灵异事件,部分区域消失不见了!”办公室里的一个MM跑过来大声喊叫着,着实吓了俺一跳.待 ...

  6. 20145120 《Java程序设计》第3周学习总结

    20145120 <Java程序设计>第3周学习总结 教材学习内容总结 基本类型与类类型的概念 在java里使用数组和字符串 封装的概念 在java定义函数 重载的概念 static的概念 ...

  7. Windows下访问VMware中tomcat

    很多人都可能和我一样,运行在虚拟机上,开发时在windows上进行. 在linux上运行tomcat,并且windows中能ping通虚拟机,但就不能通过虚拟机ip访问到8080端口上的tomcat, ...

  8. Handlebars模板引擎中的each嵌套及源码浅读

    若显示效果不佳,可移步到愚安的小窝 Handlebars模板引擎作为时下最流行的模板引擎之一,已然在开发中为我们提供了无数便利.作为一款无语义的模板引擎,Handlebars只提供极少的helper函 ...

  9. HTTP 错误 404.3 - Not Found由于扩展配置问题而无法提供您请求的页面。如果该页面是脚本,请添加处理程序。如果应下载文件,请添加 MIME 映射

    出现此错误可能是在安装IIS服务器的时候没有勾选“应用程序开发”, 解决方法如下: 就像安装IIS一样的步骤. 选择web服务器(IIS)后,再选择“应用程序开发”  选择asp.net  以及 服务 ...

  10. Oppotunity land---China

    China is a land of opportunity.Following the development of China,every sector has made their contri ...