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

JDBC测试02

时间:2020-06-02 13:03:51      阅读:79      评论:0      收藏:0      [点我收藏+]

标签:lib   执行   system   cti   rest   sse   pack   不能   exec   

第一步:在cn.edu.zucc.booklib.control.OrdersManager类中添加新增订单的方法 public void addOrders (Orders ord)throws BaseException

第二步:编写上述方法,要求输入参数ord中的任何成员都不能为null,否则不执行修改任务,并抛出异常。

第三步:在cn.edu.zucc.booklib.control.OrdersManager类中添加根据顾客编号查询订单的方法public List<Orders> searchOrdersByCID(String cid) throws BaseException

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 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;

public class OrdersManager {
    public void addOrders (Orders ord) throws BaseException {
        Connection conn = null;
        try {
            conn = DBUtil.getConnection();
            String sql = "insert into orders(OrderID,CustomerID,EmployeeID,OrderDate)values(‘"+
            ord.getOrderID()+"‘,‘"+ord.getCustomerID()+"‘,"+ord.getEmployeeID()+",‘"+ord.getOrderDate()+"‘);";
            System.out.println(sql);
            java.sql.Statement st = conn.createStatement();
            st.execute(sql);
        }
        catch (SQLException e){
            e.printStackTrace();
            throw new DbException(e);
        }
        finally {
            if (conn != null) 
                try {
                    conn.close();
                }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public List<Orders> searchOrdersByCID(String cid) throws BaseException {
        Connection conn = null;
        List<Orders> ans = new ArrayList<Orders>();
        try {
            conn = DBUtil.getConnection();
            String sql = "select * from orders where OrderID = "+cid;
            System.out.println(sql);
            java.sql.PreparedStatement pst = conn.prepareStatement(sql);
            java.sql.ResultSet rs = pst.executeQuery();
            while (rs.next()) {
                Orders zlc = new Orders();
                zlc.setOrderID(rs.getInt(1));
                zlc.setCustomerID(rs.getString(2));
                zlc.setEmployeeID(rs.getInt(3));
                zlc.setOrderDate(rs.getDate(4));
                ans.add(zlc);
            }
            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) {
         
        //精确查询
        /*try{
            OrdersManager zz = new OrdersManager();
            Orders zlc = new Orders();
            java.sql.Date dd = new java.sql.Date(new java.util.Date().getTime());

            zlc.setOrderID(6);
            zlc.setCustomerID("005");
            zlc.setEmployeeID(6);
            zlc.setOrderDate(dd);
            zz.addOrders(zlc);
            
        }catch(BaseException e){
            e.printStackTrace();
        }*/
        
        //模糊查询
        try{
            OrdersManager zz = new OrdersManager();
            List<Orders> ans = new ArrayList<Orders>();
            ans = zz.searchOrdersByCID("1");
            for (int i=0;i<ans.size();i++) {
                System.out.println(ans.get(i).getOrderDate());
            }
            
        }catch(BaseException e){
            e.printStackTrace();
        }
        
        
    }
}

 

JDBC测试02

标签:lib   执行   system   cti   rest   sse   pack   不能   exec   

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

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