📄 poll_dao.java
字号:
package com.bookshop.dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import com.bookshop.db.DBConnection;
import com.bookshop.dto.bbs_dto;
import com.bookshop.dto.bookinfo_dto;
import com.bookshop.dto.poll_dto;
public class poll_dao {
//添加投票
public int poll_select(String optionname)
{
ArrayList list = new ArrayList();
Connection conn =null;
conn=DBConnection.getConnection();
Statement st = null;
ResultSet rs = null;
String strSQL=null;
String strSQL2=null;
int n=0;
int poll=0;
poll_dto newpoll=new poll_dto();
try {
st = conn.createStatement();
strSQL="SELECT * FROM tb_poll where optionname='"+optionname+"'";
rs = st.executeQuery(strSQL);
if (rs.next())
poll=Integer.parseInt(rs.getString("poll"))+1;
strSQL2="update tb_poll set poll="+poll+" where optionname='"+optionname+"'";
n=st.executeUpdate(strSQL2);
System.out.print(strSQL2);
} catch (SQLException e) {
e.printStackTrace();
}
finally
{
try {
rs.close();
st.close();
conn.close();
} catch (SQLException ex1) {
ex1.printStackTrace();
}
}
return n;
}
//查询详情
public ArrayList po_select()
{
Connection conn=DBConnection.getConnection();
ArrayList list =new ArrayList();
Statement st=null;
ResultSet rs=null;
String sql="";
try{
sql="select * from tb_poll";
st=conn.createStatement();
rs=st.executeQuery(sql);
while(rs.next())
{
poll_dto newpoll=new poll_dto();
newpoll.setId(rs.getString("id"));
newpoll.setOptionname(rs.getString("optionname"));
newpoll.setPoll(rs.getString("poll"));
list.add(newpoll);
}
}catch(SQLException e){e.printStackTrace();}
finally{
try{
rs.close();
st.close();
conn.close();
}catch(SQLException e){e.printStackTrace();}
}
return list;
}
//查询单条信息
public poll_dto select(String id){
Connection conn =DBConnection.getConnection();
Statement st = null;
ResultSet rs = null;
poll_dto dto=new poll_dto();
String sql="select * from tb_poll where id="+id;
try{
st=conn.createStatement();
rs=st.executeQuery(sql);
if(rs.next()){
dto.setId(rs.getString("id"));
dto.setOptionname(rs.getString("optionname"));
dto.setPoll(rs.getString("poll"));
}
}catch(SQLException e){e.printStackTrace();}
finally{
try{
rs.close();
st.close();
conn.close();
}catch(SQLException e){e.printStackTrace();}
}
return dto;
}
//删除投票信息
public int delete(String id){
Connection conn =DBConnection.getConnection();
Statement st = null;
String sql="";
int n=0;
try{
sql="delete from tb_poll where id="+id;
st=conn.createStatement();
n=st.executeUpdate(sql);
}catch(SQLException e){e.printStackTrace();}
finally{
try{
st.close();
conn.close();
}catch(SQLException e){e.printStackTrace();}
}
return n;
}
//添加投票信息
public int add(String optionname){
Connection conn =DBConnection.getConnection();
Statement st = null;
String sql="";
int n=0;
try{
sql="insert into tb_poll (optionname) values('"+optionname+"')";
st=conn.createStatement();
n=st.executeUpdate(sql);
}catch(SQLException e){e.printStackTrace();}
finally{
try{
st.close();
conn.close();
}catch(SQLException e){e.printStackTrace();}
}
return n;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -