经典电商数据库分析构建(二)

传统电商表的分析以及设计(java代码处理上文中的七个任务)2

工具类 JDBCUtils.java

/**
 * @author 高远</n> 博客 http://blog.csdn.net/wgyscsf</n> 编写时期 2016-3-17 上午10:04:02
 */
public class JDBCUtils {
    private static String url = "jdbc:mysql://localhost:3306/mygist";
    private static String user = "root";
    private static String password = "wanggaoyuan";
    private static Connection conn;
    static {

    }

    /*
     * 装载驱动
     */
    public static void loadDriver() {
        try {
            Class.forName("com.mysql.jdbc.Driver");// 高版本不需要该语句
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /*
     * 获取连接
     */
    public static Connection getConnection() {
        loadDriver();
        try {
            conn = DriverManager.getConnection(url, user, password);
            return conn;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /*
     * 释放连接
     */
    public static void releaseConn(Statement stmt, Connection conn) {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            stmt = null;
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            conn = null;
        }
    }

    /*
     * 释放连接
     */
    public static void releaseConn(ResultSet rs, Statement stmt, Connection conn) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            rs = null;
        }
        releaseConn(stmt, conn);
    }
}

对上述文章的七个数据库操作语句的java实现 ShowMyGist.java

/**
 * @author 高远</n> 博客 http://blog.csdn.net/wgyscsf</n> 编写时期 2016-3-17 上午10:58:11
 */
public class ShowMyGist {
    /*
     * 1. 从商品表中查询以“老板”两个字开头的商品名称及数量,并按数量降序排序(用户搜索)
     */
    @Test
    public void Test1() throws SQLException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "SELECT pname 商品名,premain 剩余量 FROM product WHERE pname LIKE '老板%' ORDER BY premain";
        conn = JDBCUtils.getConnection();
        ps = conn.prepareStatement(sql);
        rs = ps.executeQuery();
        while (rs.next()) {
            System.out.println(rs.getString(1) + ":" + rs.getInt(2));
        }
        JDBCUtils.releaseConn(rs, ps, conn);

    }

    /*
     * 2. 查询所有的商品名、价格、所属厂家、产地)(商品信息展示)
     */
    @Test
    public void Test2() throws SQLException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "SELECT pname 商品名,premain 剩余量,pprice 价格,mname 所属厂家,maddress 产地 FROM product,merchant WHERE product.mid = merchant.mid ";
        conn = JDBCUtils.getConnection();
        ps = conn.prepareStatement(sql);
        rs = ps.executeQuery();
        while (rs.next()) {
            System.out
                    .println(rs.getString(1) + ":" + rs.getInt(2) + ":"
                            + rs.getDouble(3) + rs.getString(4) + ":"
                            + rs.getString(5));
        }
        JDBCUtils.releaseConn(rs, ps, conn);

    }

    /*
     * 3. 从订单表中查询购买订单编号为“2”的所有商品的商品名字、单价、数量、顾客名字及订单日期(用户查看订单)
     */
    @Test
    public void Test3() throws SQLException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "SELECT pname 商品名,premain 剩余量,pprice 价格,cname 顾客名,"
                + "odatetime 订单日期 FROM product,customer,orders,ordersitem WHERE"
                + " ordersitem.pid = product.pid AND ordersitem.oid = orders.oid "
                + "AND orders.cid = customer.cid AND orders.oid = ? ";
        System.out.println("请输入您要查询的订单号:");
        Scanner sc = new Scanner(System.in);
        int next = sc.nextInt();
        conn = JDBCUtils.getConnection();
        ps = conn.prepareStatement(sql);
        ps.setInt(1, next);
        rs = ps.executeQuery();
        if (!rs.next()) {
            System.out.println("查询不到该订单!");
            return;
        }
        while (rs.next()) {
            System.out
                    .println(rs.getString(1) + ":" + rs.getInt(2) + ":"
                            + rs.getDouble(3) + rs.getString(4) + ":"
                            + rs.getString(5));
        }
        JDBCUtils.releaseConn(rs, ps, conn);

    }

    /*
     * 4.
     * 更新订单1:更新状态为“已支付”,总价格为实际价格。显示:订单编号为“1”的所有商品的商品名字、单价、数量、顾客名字及订单日期,总价格,订单状态
     * (用户支付查看订单)
     */
    @Test
    public void Test4() throws SQLException {
        Connection conn = null;
        PreparedStatement ps = null;
        String sql = "UPDATE orders SET orders.ostate = '已支付',"
                + "orders.osummoney =" + "(SELECT  (SELECT  SUM( "
                + "  ordersitem.iobuynum * ordersitem.iosummoney  )"
                + "  FROM  ordersitem WHERE ordersitem.oid = orders.oid "
                + "  AND ordersitem.oid = ?   GROUP BY ordersitem.oid)) "
                + "WHERE orders.oid =?";
        System.out.println("请输入您要更新的订单号:");
        Scanner sc = new Scanner(System.in);
        int next = sc.nextInt();
        conn = JDBCUtils.getConnection();
        ps = conn.prepareStatement(sql);
        ps.setInt(1, next);
        ps.setInt(2, next);
        ps.executeUpdate();
        JDBCUtils.releaseConn(ps, conn);

    }

    /*
     * 5. 删除订单3信息(用户取消订单)
     */
    @Test
    public void Test5() throws SQLException {
        Connection conn = null;
        PreparedStatement ps = null;
        System.out.println("请输入您要删除的订单号:");
        Scanner sc = new Scanner(System.in);
        int next = sc.nextInt();

        String sql1 = "DELETE FROM ordersitem WHERE oid=?";
        String sql2 = "DELETE FROM orders WHERE oid=?";

        conn = JDBCUtils.getConnection();
        conn.setAutoCommit(false);// 开启事务:保证两个删除同时完成
        ps = conn.prepareStatement(sql1);
        ps.setInt(1, next);
        ps.executeUpdate();

        ps = conn.prepareStatement(sql2);
        ps.setInt(1, next);
        ps.executeUpdate();
        conn.commit();// 提交事务
        JDBCUtils.releaseConn(ps, conn);

        Test3();
    }

    /*
     * 6. 查询编号为2的商品的售出数量、名字、购买人名字(商家查看某件商品出售情况)
     */
    @Test
    public void Test6() throws SQLException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "SELECT  SUM(ordersitem.iobuynum) 售出数量,"
                + "SUM(ordersitem.iobuynum*product.pprice) 总价,"
                + "product.pname 商品名称,customer.cname 顾客名字 "
                + " FROM product,customer, orders, ordersitem "
                + "WHERE ordersitem.pid = product.pid  AND"
                + " ordersitem.oid = orders.oid  AND "
                + "orders.cid = customer.cid   AND "
                + "product.pid = ? GROUP BY customer.cid";
        System.out.println("请输入您要查询的商品的编号:");
        Scanner sc = new Scanner(System.in);
        int next = sc.nextInt();
        conn = JDBCUtils.getConnection();
        ps = conn.prepareStatement(sql);
        ps.setInt(1, next);
        rs = ps.executeQuery();
        while (rs.next()) {
            System.out.println("售出数量:" + rs.getInt(1) + "----" + "总销价:"
                    + rs.getDouble(2) + "----商品名字:" + rs.getString(3)
                    + "-----顾客名字:" + rs.getString(4));
        }
        JDBCUtils.releaseConn(rs, ps, conn);
    }

    /*
     * 7. 查看宝洁公司的销售情况,按照销售数量由高到低排序(商家查看售出数据)
     */
    @Test
    public void Test7() throws SQLException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "SELECT SUM(ordersitem.iobuynum) 售出数量,"
                + "SUM( ordersitem.iobuynum * product.pprice) 总价"
                + ", product.pname 商品名字  FROM "
                + "product, ordersitem, merchant    "
                + "WHERE ordersitem.pid = product.pid   "
                + "  AND product.mid = merchant.mid      "
                + " AND merchant.mname = '广州宝洁'     " + "GROUP BY product.pname ";

        conn = JDBCUtils.getConnection();
        ps = conn.prepareStatement(sql);
        rs = ps.executeQuery();
        while (rs.next()) {
            System.out.println("售出数量:" + rs.getInt(1) + "----" + "总销价:"
                    + rs.getDouble(2) + "----商品名字:" + rs.getString(3));
        }
        JDBCUtils.releaseConn(rs, ps, conn);
    }

    /*
     * 8. 将售出的商品按照售出数量由高到低排序(管理员查看售出数据)
     */
    @Test
    public void Test8() throws SQLException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "SELECT SUM(ordersitem.iobuynum) 售出数量,"
                + "SUM( ordersitem.iobuynum * product.pprice) 总价"
                + ", product.pname 商品名字  FROM "
                + "product, ordersitem, merchant    "
                + "WHERE ordersitem.pid = product.pid   "
                + "  AND product.mid = merchant.mid      "
                + " GROUP BY product.pname ORDER BY ordersitem.iobuynum DESC ";

        conn = JDBCUtils.getConnection();
        ps = conn.prepareStatement(sql);
        rs = ps.executeQuery();
        while (rs.next()) {
            System.out.println("售出数量:" + rs.getInt(1) + "----" + "总销价:"
                    + rs.getDouble(2) + "----商品名字:" + rs.getString(3));
        }
        JDBCUtils.releaseConn(rs, ps, conn);
    }
}

你可能感兴趣的:(java,数据库,电商)