JDBC测试02

Posted zhanglichen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JDBC测试02相关的知识,希望对你有一定的参考价值。

第一步:在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的主要内容,如果未能解决你的问题,请参考以下文章

如何在片段中填充列表视图?

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

JDBC测试02

JDBC测试02

面试常用的代码片段

mysql jdbc源码分析片段 和 Tomcat's JDBC Pool