package com.liuyanban.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import com.liuyanban.JDBCConnection.JDBCConnection;
import com.liuyanban.bean.B_backBean;
import com.liuyanban.bean.PageTb_leaveBean;
public class B_backDao {
public static String addB_back(B_backBean bb){
String flag="回复不成功!";
Connection cn=JDBCConnection.getConnection();
PreparedStatement pstmt=null;
if(cn==null)
{
return flag;
}
try {
pstmt=cn.prepareStatement("INSERT INTO b_back(backwho,backcontent,leaveid)values(?,?,?)");
pstmt.setString(1,bb.getBackWho());
pstmt.setString(2,bb.getBackContent());
pstmt.setInt(3,bb.getLeaveId());
int row=pstmt.executeUpdate();
if(row>0)
{
flag="";
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if(pstmt!=null)
{
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pstmt=null;
}
if(cn!=null)
{
try {
cn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
cn=null;
}
}
return flag;
}
public static String deleteB_back(int id){
String flag="删除不成功!";
Connection cn=JDBCConnection.getConnection();
PreparedStatement pstmt=null;
if(cn==null)
{
return flag;
}
try {
pstmt=cn.prepareStatement("DELETE b_back WHERE id=?");
pstmt.setInt(1,id);
int row=pstmt.executeUpdate();
if(row>0)
{
flag="";
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if(pstmt!=null)
{
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pstmt=null;
}
if(cn!=null)
{
try {
cn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
cn=null;
}
}
return flag;
}
public static String allBackDelete(int leaveid){
String flag="删除不成功!";
Connection cn=JDBCConnection.getConnection();
PreparedStatement pstmt=null;
if(cn==null)
{
return flag;
}
try {
pstmt=cn.prepareStatement("DELETE b_back WHERE leaveid=?");
pstmt.setInt(1,leaveid);
int row=pstmt.executeUpdate();
if(row>0)
{
flag="";
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if(pstmt!=null)
{
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pstmt=null;
}
if(cn!=null)
{
try {
cn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
cn=null;
}
}
return flag;
}
public static PageTb_leaveBean maxRow(int leaveid){
PageTb_leaveBean pb=new PageTb_leaveBean();
Connection cn=JDBCConnection.getConnection();
PreparedStatement pstmt=null;
ResultSet resultset=null;
if(cn==null)
{
return pb;
}
try {
pstmt=cn.prepareStatement("SELECT count(*) FROM b_back where leaveid=?");
pstmt.setInt(1, leaveid);
resultset=pstmt.executeQuery();
resultset.next();
int row=resultset.getInt(1);
if(row>0)
{
pb.setMaxCountRow(row);
if(row%2==0)
{
pb.setMaxPage(row/2);
}else
if(row%pb.getPageRow()!=0)
{
pb.setMaxPage(row/2+1);
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if(resultset!=null)
{
try {
resultset.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
resultset=null;
}
if(pstmt!=null)
{
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pstmt=null;
}
if(cn!=null)
{
try {
cn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
cn=null;
}
}
return pb;
}
public static ArrayList<B_backBean> selectB_back(int curPage,int leaveid){
int begin=(curPage-1)*2+1;
int end=curPage*2;
ArrayList<B_backBean> al=new ArrayList<B_backBean>();
Connection cn=JDBCConnection.getConnection();
PreparedStatement pstmt=null;
ResultSet resultset=null;
if(cn==null)
{
return al;
}
try {
pstmt=cn.prepareStatement("select * from (select rownum rid,b_back.* from b_back where leaveid=? order by id desc) t where t.rid>=? and t.rid<=?");
pstmt.setInt(1,leaveid);
pstmt.setInt(2,begin);
pstmt.setInt(3,end);
resultset=pstmt.executeQuery();
while(resultset.next())
{
B_backBean bb=new B_backBean();
bb.setId(resultset.getInt(2));
bb.setLeaveId(resultset.getInt(3));
bb.setBackTime(resultset.getTimestamp(4));
bb.setBackWho(resultset.getString(5));
bb.setBackContent(resultset.getString(6));
al.add(bb);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if(resultset!=null)
{
try {
resultset.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
resultset=null;
}
if(pstmt!=null)
{
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pstmt=null;
}
if(cn!=null)
{
try {
cn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
cn=null;
}
}
return al;
}
public static ArrayList<B_backBean> selectByLeaveId(int leaveid){
ArrayList<B_backBean> al=new ArrayList<B_backBean>();
Connection cn=JDBCConnection.getConnection();
PreparedStatement pstmt=null;
ResultSet resultset=null;
if(cn==null)
{
return al;
}
try {
pstmt=cn.prepareStatement("SELECT * FROM b_back WHERE leaveid=?");
pstmt.setInt(1,leaveid);
resultset=pstmt.executeQuery();
while(resultset.next())
{
B_backBean bb=new B_backBean();
bb.setId(resultset.getInt(1));
bb.setLeaveId(resultset.getInt(2));
bb.setBackTime(resultset.getTimestamp(3));
bb.setBackWho(resultset.getString(4));
bb.setBackContent(resultset.getString(5));
al.add(bb);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if(resultset!=null)
{
try {
resultset.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
resultset=null;
}
if(pstmt!=null)
{
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pstmt=null;
}
if(cn!=null)
{
try {
cn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
cn=null;
}
}
return al;
}
}