📄 votemanagedao.java
字号:
package com.qyg.shop.vote;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.qyg.database.DbOperatorHandle;
import com.qyg.shop.sales.Sales;
public class VoteManageDAO implements VoteDAO {
DbOperatorHandle doh=null;
public VoteManageDAO() {
// TODO Auto-generated constructor stub
doh=new DbOperatorHandle();
}
public void add(Vote vote) {
// TODO Auto-generated method stub
try{
PreparedStatement ps=doh.getCon().prepareStatement("insert vote values(null,?,null)");
ps.setInt(1, vote.getVote_class());
ps.execute();
ps.close();
}catch (Exception e) {
// TODO: handle exception
System.out.print("添加vote错误");
e.printStackTrace();
}
finally{
doh.closeCon();
}
}
public void delete(int vote_id) {
// TODO Auto-generated method stub
try{
Statement st=doh.getCon().createStatement();
String sql="delete from vote where vote_id="+vote_id;
System.out.print(sql);
st.executeUpdate(sql);
st.close();
}
catch (Exception e) {
// TODO: handle exception
System.out.println("删除指定投票信息失败");
e.printStackTrace();
}
finally{
doh.closeCon();
}
}
public int getVoteByClass(int vote_class) {
// TODO Auto-generated method stub
int total=0;
try{
Statement st=doh.getCon().createStatement();
String sql="select * from vote where vote_class="+vote_class;
ResultSet rs=st.executeQuery(sql);
while(rs.next()){
total++;
}
}
catch (Exception e) {
// TODO: handle exception
System.out.println("得到投票总数wrong");
e.printStackTrace();
}
finally{
doh.closeCon();
}
return total;
}
public List<Vote> getAllVotes() {
// TODO Auto-generated method stub
List<Vote> votes=null;
try{
votes=new ArrayList<Vote>();
Statement st=doh.getCon().createStatement();
String sql="select * from vote";
ResultSet rs=st.executeQuery(sql);
while(rs.next()){
Vote vote=this.getVoteByRs(rs);
votes.add(vote);
}
rs.close();
st.close();
}
catch (Exception e) {
// TODO: handle exception
System.out.println("得到所有消费信息");
e.printStackTrace();
}
finally{
doh.closeCon();
}
return votes;
}
public int getTotalVotes() {
// TODO Auto-generated method stub
int total=0;
try{
Statement st=doh.getCon().createStatement();
String sql="select count(*) from vote";
ResultSet rs=st.executeQuery(sql);
if(rs.next()){
total=rs.getInt(1);
}
}
catch (Exception e) {
// TODO: handle exception
System.out.println("得到投票总数wrong");
e.printStackTrace();
}
finally{
doh.closeCon();
}
return total;
}
public Vote getVoteByRs(ResultSet rs) {
// TODO Auto-generated method stub
Vote vote=null;
try{
vote=new Vote();
vote.setVote_id(rs.getInt("vote_id"));
vote.setVote_class(rs.getInt("vote_class"));
vote.setVote_date(rs.getTimestamp("vote_date"));
}
catch (Exception e) {
// TODO: handle exception
System.out.print("把rs中的值返回到Sales中");
}
return vote;
}
public static void main(String[] args){
VoteDAO dao = new VoteManageDAO();
// Vote vote = new Vote(4);
// dao.add(vote);
// dao.delete(65);
System.out.println(dao.getAllVotes());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -