添加、删除、修改、查询的四个基本方法

/**
     * 添加
    */
    public boolean add(Msg obj){
        Connection con=DBConnection.getConnction();
        PreparedStatement ps = null ;
        boolean bret=false;       
        try{
            ps=con.prepareStatement("insert into msgInfo (privyName,telphone,msgContent,)values(?,?,?)");
            //System.out.print(obj.getPrivyName());
            ps.setString(1,obj.getPrivyName());
            ps.setString(2, obj.getTelphone());
            ps.setString(3,obj.getMsgContent());
                       
            int i = ps.executeUpdate();
            if(i>0){
                bret=true;
            }
        }catch(Exception e){
            System.out.println(e.getMessage());
        }finally{
            try{
                if(ps!=null)ps.close();
                if(con!=null)con.close();
            }catch(Exception e2){
                System.out.println(e2.getMessage());
            }
        }
       
        return bret;
    }
    /**
     * 查询所有
    */
    public List  getAllMsg(String userID){//用util的List
        Connection con=DBConnection.getConnction();
        ArrayList al = new ArrayList();
        PreparedStatement ps = null ;
        try {
            ps=con.prepareStatement("select * from msgInfo where userID=? order by msgID desc");
            ps.setString(1,userID);
            ResultSet rs=ps.executeQuery();
            while(rs.next()){
                Msg bean=new Msg();
                bean.setMsgID(Integer.toString(rs.getInt(1)));
                bean.setPrivyName(rs.getString(2));
                bean.setTelphone(rs.getString(3));
                bean.setMsgContent(rs.getString(4));
                             
                al.add(bean);
            }           
        }catch(Exception e){
            System.out.println(e.getMessage());
        }finally{
            if (con != null)
                try {
                    ps.close();
                    con.close();
                } catch (SQLException e) {
                    System.out.println(e.getMessage());
                }
        }
        return al;
    }
    /**
     * 查询一条记录
    */
    public List  getOneMsg(String msgID){//用util的List
        Connection con=DBConnection.getConnction();
        ArrayList al = new ArrayList();
        PreparedStatement ps = null ;
        try {
            ps=con.prepareStatement("select * from msgInfo where msgID=?");
            ps.setString(1,msgID);
            ResultSet rs=ps.executeQuery();
            while(rs.next()){
                Msg bean=new Msg();
                bean.setPrivyName(rs.getString(2).trim());
                bean.setTelphone(rs.getString(3).trim());
                bean.setMsgContent(rs.getString(4).trim());                   
               
                al.add(bean);
            }           
        }catch(Exception e){
            System.out.println(e.getMessage());
        }finally{
            if (con != null)
                try {
                    ps.close();
                    con.close();
                } catch (SQLException e) {
                    System.out.println(e.getMessage());
                }
        }
        return al;
    }
    /**
     * 删除
    */
    public boolean removeMsg(String msgID){
        Connection con=DBConnection.getConnction();
        PreparedStatement ps = null ;
        boolean bret=false;       
        try{
            ps=con.prepareStatement("delete msgInfo where msgID=?");
            ps.setString(1,msgID);
            int i = ps.executeUpdate();
            if(i>0){
                bret=true;
            }
        }catch(Exception e){
            System.out.println(e.getMessage());
        }finally{
            try{
                if(ps!=null)ps.close();
                if(con!=null)con.close();
            }catch(Exception e2){
                System.out.println(e2.getMessage());
            }
        }
       
        return bret;
    }
    /**
     * 修改
    */
    public boolean ModifyMsg(Msg obj){
       
        Connection con=DBConnection.getConnction();
        PreparedStatement ps = null ;
        boolean bret=false;       
        try{
           
            ps=con.prepareStatement("Update msgInfo set privyName=?,telphone=?,msgContent=?");
            ps.setString(1, obj.getPrivyName());
            ps.setString(2, obj.getTelphone());
            ps.setString(3, obj.getMsgContent());
           
            int i = ps.executeUpdate();
            if(i>0){
                bret=true;
            }
        }catch(Exception e){
            System.out.println(e.getMessage());
        }finally{
            try{
                if(ps!=null)ps.close();
                if(con!=null)con.close();
            }catch(Exception e2){
                System.out.println(e2.getMessage());
            }
        }
       
        return bret;
    }

 

/**
     * 得到总记录条数
    */
    public int getRows(String userID){
        Connection con=DBConnection.getConnction();
        PreparedStatement ps = null ;
        int row=0;
        try {
            ps=con.prepareStatement("select count(*) from msgInfo where userID=?");
            ps.setString(1,userID);
            ResultSet rs=ps.executeQuery();
            if(rs.next()){
                row=rs.getInt(1);
            }
            //System.out.println(row);
        }
        catch(Exception e){
            row=0;
        }
        return row;
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值