码迷,mamicode.com
首页 > 数据库 > 详细

JDBC实验六

时间:2020-06-07 13:18:21      阅读:100      评论:0      收藏:0      [点我收藏+]

标签:resultset   nec   put   catch   man   sys   jdb   import   pre   

package cn.edu.zucc.booklib.control;

import java.util.Date;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.Map;
import java.util.HashMap;
import cn.edu.zucc.booklib.util.BaseException;
import cn.edu.zucc.booklib.util.BusinessException;
import cn.edu.zucc.booklib.util.DBUtil;
import cn.edu.zucc.booklib.util.DbException;

import java.sql.Connection;
import cn.edu.zucc.booklib.model.Orders;
import cn.edu.zucc.booklib.util.BaseException;


import cn.edu.zucc.booklib.model.BeanvieworderDetail;

public class VIEWOrdersManager {
    /*public List<BeanvieworderDetail> orderQuery2 (int orderid)throws BaseException {
        Connection conn = null;
        List<BeanvieworderDetail> ans = new ArrayList<BeanvieworderDetail>();
        try {
            conn = DBUtil.getConnection();
            String sql = "select * from vieworderdetail where ProductID = "+orderid;
            System.out.println(sql);
            java.sql.PreparedStatement pst = conn.prepareStatement(sql);
            java.sql.ResultSet rs = pst.executeQuery();
            while (rs.next()) {
                BeanvieworderDetail zlc = new BeanvieworderDetail();
                zlc.setProductID(rs.getInt(1));
                zlc.setProductName(rs.getString(2));
                zlc.setQuantity(rs.getInt(3));
                zlc.setUnitPrice(rs.getDouble(4));
                ans.add(zlc);
            }
            rs.close();
            pst.close();
            return ans;
        }
        catch (SQLException e) {
            e.printStackTrace();
            throw new DbException(e);
        }
        finally {
            if (conn != null) 
                try {
                    conn.close();
                }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            VIEWOrdersManager tt= new VIEWOrdersManager();
            List<BeanvieworderDetail> ans = new ArrayList<BeanvieworderDetail>();
            ans = tt.orderQuery2(1);
            for (int i=0;i<ans.size();i++) {
                System.out.println(ans.get(i).getProductID()+" "+ans.get(i).getProductName()+" "+ans.get(i).getQuantity()+" "+ans.get(i).getUnitPrice());
                
            }
        }catch(BaseException e){
            e.printStackTrace();
        }
    }*/
    
    /*public Map<String,Double> getProductTotalFee(String productName) throws BaseException{
           //参数为OrderID号:订单号
        Map<String,Double> map = new HashMap<String,Double>();
        Connection conn = null;
        try {
            conn = DBUtil.getConnection();
            String sql ="select * from vieworderdetail where productname like ?";
            System.out.println(sql);
            java.sql.PreparedStatement pst = conn.prepareStatement(sql);
            pst.setString(1, productName);
            java.sql.ResultSet rs = pst.executeQuery();
            while(rs.next()) {
                if(map.containsKey(rs.getString(2))) {
                    map.put(rs.getString(2),map.get(rs.getString(2)) + rs.getDouble(3) * rs.getInt(4));
                }
                else {
                    map.put(rs.getString(2),rs.getDouble(3) * rs.getInt(4));
                }
            }
            rs.close();
            pst.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(conn!=null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        return map;
        }
    public static void main(String[] args) throws BaseException{
    // TODO Auto-generated method stub
        VIEWOrdersManager tt = new VIEWOrdersManager();
        String ProductName = "%z%";
        Map<String,Double> mp = new HashMap<String,Double>();
        mp = tt.getProductTotalFee(ProductName);
        Set<String> smp = mp.keySet();
        for(String b:smp) {
            System.out.println("产品名称: " + b + "  总价: " + mp.get(b));
        }
    }*/
    
    public List<String> getMaxOrderedProductName()throws BaseException{
           //参数为OrderID号:订单号
        List<String> list = new ArrayList<String>();
        Connection conn = null;
        try {
            conn = DBUtil.getConnection();
            String sql = "select productname from vieworderdetail where quantity = (select max(quantity) from vieworderdetail)";
            System.out.println(sql);
            java.sql.PreparedStatement pst = conn.prepareStatement(sql);
            java.sql.ResultSet rs = pst.executeQuery();
            while(rs.next()) {
                String str = null;
                str = rs.getString(1);
                list.add(str);
            }
            rs.close();
            pst.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(conn!=null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }        
        return list;
    } 
    public static void main(String[] args) throws BaseException{
    // TODO Auto-generated method stub
        VIEWOrdersManager tt = new VIEWOrdersManager();
        List<String> lst = new ArrayList<String>();
        lst = tt.getMaxOrderedProductName();
        for(String b : lst) {
            System.out.println("产品名称: " + b);
        }
    }    

}

 

JDBC实验六

标签:resultset   nec   put   catch   man   sys   jdb   import   pre   

原文地址:https://www.cnblogs.com/zhanglichen/p/13060007.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!