zoukankan      html  css  js  c++  java
  • JDBC实验六

    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);
            }
        }    
    
    }
  • 相关阅读:
    js对象数组(JSON) 根据某个共同字段 分组
    一个 函数 用来转化esSearch 的range 条件
    关于 vuex 报错 Do not mutate vuex store state outside mutation handlers.
    android listview 重用view导致的选择混乱问题
    android SDK和ADT的更新
    Android中adb push和adb install的使用区别
    pycharm中添加扩展工具pylint
    su Authentication failure解决
    Putty以及adb网络调试
    有关android源码编译的几个问题
  • 原文地址:https://www.cnblogs.com/zhanglichen/p/13060007.html
Copyright © 2011-2022 走看看