JDBC

public int getBookCount () throws BaseException{
        //要求返回该出版社的图书数量
        int cnt=0;
        Connection conn = null;
        try {
            conn = DBUtil.getConnection();
            String sql = "select bookname from beanbook";
            java.sql.PreparedStatement pst = conn.prepareStatement(sql);
            java.sql.ResultSet rs = pst.executeQuery();
            while (rs.next()) {
                cnt++;
                
            }
            return cnt;
        }catch (SQLException e) {
            e.printStackTrace();
            throw new DbException(e);
        }
        finally{
            if(conn!=null)
                try {
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }
    } 


public static void main(String[] args){
        BookManager p=new BookManager();
        
        
        try {
            int cnt=p.getBookCount();
            System.out.println(cnt);
            
        } catch (BaseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }

 

3-2

public int getPublisherCount () throws BaseException {
        int cnt=0;
        Connection conn = null;
        try {
            conn = DBUtil.getConnection();
            String sql = "select distinct barcode from beanbook";
            java.sql.PreparedStatement pst = conn.prepareStatement(sql);
            java.sql.ResultSet rs = pst.executeQuery();
            while (rs.next()) {
                cnt++;
                
            }
            return cnt;
        }catch (SQLException e) {
            e.printStackTrace();
            throw new DbException(e);
        }
        finally{
            if(conn!=null)
                try {
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block                    e.printStackTrace();
                }
        }
    }

public static void main (String[] args) {
        BookManager p = new BookManager();
        try {
            int cnt=p.getPublisherCount();
            System.out.println(cnt);
        }
        catch (BaseException e) {
            e.printStackTrace();
        }
    }
View Code

 3-3

public Double getPublisherCount () throws BaseException {
        Double cnt=0.0;
        Connection conn = null;
       
        try {
            conn = DBUtil.getConnection();
            String sql = "select avg(price) from beanbook";
            java.sql.PreparedStatement pst = conn.prepareStatement(sql);
            java.sql.ResultSet rs = pst.executeQuery();
            while (rs.next()) {
                                
            }
            return cnt;
        }catch (SQLException e) {
            e.printStackTrace();
            throw new DbException(e);
        }
        finally{
            if(conn!=null)
                try {
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block 
                   e.printStackTrace();                
                   }
        }
    }

 

posted @ 2020-05-13 16:21  zlc0405  阅读(301)  评论(0编辑  收藏  举报