JDBC实验六

Posted zhanglichen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
        }
    }    

}

 

以上是关于JDBC实验六的主要内容,如果未能解决你的问题,请参考以下文章

数据库原理实验六——ODBC/JDBC数据库编程

部分代码片段

关于mysql驱动版本报错解决,Cause: com.mysql.jdbc.exceptions.jdbc4Unknown system variable ‘query_cache_size(代码片段

大三寒假生活13

实验六:类的封装

实验六:类的封装