JavaWeb应用案例(Mysql+Javaweb)购物网-01

1.创建Mysql数据库
1.1.创建一个新的mysql数据库,数据库名:simonshop’
1.2.创建用户数据储存表t_user
在这里插入图片描述
1.3
在这里插入图片描述
1.4.t_product表
在这里插入图片描述
1.5.t_order表
在这里插入图片描述
1.6.添加基础测试数据:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.创建项目并完善项目结构、导入mysql连接所需要的jar包
在这里插入图片描述
3.数据库连接管理类ConnectionManager
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

4.创建bean层文件(需要用户输入或者与将创建的数据库字段相嵌合的类)
4.1.储存商品操作
储存商品类别
在这里插入图片描述

4.2.用户实体类
在这里插入图片描述
在这里插入图片描述
4.3.商品实体类
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
4.4.订单实体类
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
5.数据访问接口
5.1.用户数据访问接口
在这里插入图片描述
在这里插入图片描述
5.2.商品类别访问接口
在这里插入图片描述

5.3.商品信息访问接口
在这里插入图片描述

5.4.订单信息访问接口
在这里插入图片描述
5.5.用户信息接口实现类

package net.bl.xmshop.dao.impl;

import net.bl.xmshop.bean.User;
import net.bl.xmshop.dao.UserDao;
import net.bl.xmshop.dbutil.ConnectionManager;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {

    /**
     * 插入用户
     */

    @Override

    public int insert(User user) {

        // 定义插入记录数

        int count = 0;


        // 获得数据库连接

        Connection conn = ConnectionManager.getConnection();

        // 定义SQL字符串

        String strSQL = "INSERT INTO t_user (username, password, telephone, register_time, popedom)"

                + " VALUES (?, ?, ?, ?, ?)";

        try {

            // 创建预备语句对象

            PreparedStatement pstmt = conn.prepareStatement(strSQL);

            // 设置占位符的值

            pstmt.setString(1, user.getUsername());

            pstmt.setString(2, user.getPassword());

            pstmt.setString(3, user.getTelephone());

            pstmt.setTimestamp(4, new Timestamp(user.getRegisterTime().getTime()));

            pstmt.setInt(5, user.getPopedom());

            // 执行更新操作,插入新记录

            count = pstmt.executeUpdate();

            // 关闭预备语句对象

            pstmt.close();

        } catch (SQLException e) {

            e.printStackTrace();

        } finally {

            ConnectionManager.closeConnection(conn);

        }


        // 返回插入记录数

        return count;

    }


    /**
     * 删除用户记录
     */

    @Override

    public int deleteById(int id) {

        // 定义删除记录数

        int count = 0;


        // 获得数据库连接

        Connection conn = ConnectionManager.getConnection();

        // 定义SQL字符串

        String strSQL = "DELETE FROM t_user WHERE id = ?";

        try {

            // 创建预备语句对象

            PreparedStatement pstmt = conn.prepareStatement(strSQL);

            // 设置占位符的值

            pstmt.setInt(1, id);

            // 执行更新操作,删除记录

            count = pstmt.executeUpdate();

            // 关闭预备语句对象

            pstmt.close();

        } catch (SQLException e) {

            e.printStackTrace();

        } finally {

            ConnectionManager.closeConnection(conn);

        }


        // 返回删除记录数

        return count;

    }


    /**
     * 更新用户
     */

    @Override

    public int update(User user) {

        // 定义更新记录数

        int count = 0;


        // 获得数据库连接

        Connection conn = ConnectionManager.getConnection();

        // 定义SQL字符串

        String strSQL = "UPDATE t_user SET username = ?, password = ?, telephone = ?,"

                + " register_time = ?, popedom = ? WHERE id = ?";

        try {

            // 创建预备语句对象

            PreparedStatement pstmt = conn.prepareStatement(strSQL);

            // 设置占位符的值

            pstmt.setString(1, user.getUsername());

            pstmt.setString(2, user.getPassword());

            pstmt.setString(3, user.getTelephone());

            pstmt.setTimestamp(4, new Timestamp(user.getRegisterTime().getTime()));

            pstmt.setInt(5, user.getPopedom());

            pstmt.setInt(6, user.getId());

            // 执行更新操作,更新记录

            count = pstmt.executeUpdate();

            // 关闭预备语句对象

            pstmt.close();

        } catch (SQLException e) {

            e.printStackTrace();

        } finally {

            ConnectionManager.closeConnection(conn);

        }


        // 返回更新记录数

        return count;

    }


    /**
     * 按标识符查询用户
     */

    @Override

    public User findById(int id) {

        // 声明用户

        User user = null;


        // 获取数据库连接对象

        Connection conn = ConnectionManager.getConnection();

        // 定义SQL字符串

        String strSQL = "SELECT * FROM t_user WHERE id = ?";

        try {

            // 创建预备语句对象

            PreparedStatement pstmt = conn.prepareStatement(strSQL);

            // 设置占位符的值

            pstmt.setInt(1, id);

            // 执行SQL查询,返回结果集

            ResultSet rs = pstmt.executeQuery();

            // 判断结果集是否有记录

            if (rs.next()) {

                // 实例化用户

                user = new User();

                // 利用当前记录字段值去设置商品类别的属性

                user.setId(rs.getInt("id"));

                user.setUsername(rs.getString("username"));

                user.setPassword(rs.getString("password"));

                user.setTelephone(rs.getString("telephone"));

                user.setRegisterTime(rs.getTimestamp("register_time"));

                user.setPopedom(rs.getInt("popedom"));

            }

        } catch (SQLException e) {

            e.printStackTrace();

        } finally {

            ConnectionManager.closeConnection(conn);

        }


        // 返回用户

        return user;

    }


    @Override

    public List<User> findByUsername(String username) {

        // 声明用户列表

        List<User> users = new ArrayList<User>();

        // 获取数据库连接对象

        Connection conn = ConnectionManager.getConnection();

        // 定义SQL字符串

        String strSQL = "SELECT * FROM t_user WHERE username = ?";

        try {

            // 创建预备语句对象

            PreparedStatement pstmt = conn.prepareStatement(strSQL);

            // 设置占位符的值

            pstmt.setString(1, username);

            // 执行SQL查询,返回结果集

            ResultSet rs = pstmt.executeQuery();

            // 遍历结果集

            while (rs.next()) {

                // 创建类别实体

                User user = new User();

                // 设置实体属性

                user.setId(rs.getInt("id"));

                user.setUsername(rs.getString("username"));

                user.setPassword(rs.getString("password"));

                user.setTelephone(rs.getString("telephone"));

                user.setRegisterTime(rs.getTimestamp("register_time"));

                user.setPopedom(rs.getInt("popedom"));

                // 将实体添加到用户列表

                users.add(user);

            }

            // 关闭结果集

            rs.close();

            // 关闭语句对象

            pstmt.close();

        } catch (SQLException e) {

            e.printStackTrace();

        } finally {

            // 关闭数据库连接

            ConnectionManager.closeConnection(conn);

        }

        // 返回用户列表

        return users;

    }


    @Override

    public List<User> findAll() {

        // 声明用户列表

        List<User> users = new ArrayList<User>();

        // 获取数据库连接对象

        Connection conn = ConnectionManager.getConnection();

        // 定义SQL字符串

        String strSQL = "SELECT * FROM t_user";

        try {

            // 创建语句对象

            Statement stmt = conn.createStatement();

            // 执行SQL,返回结果集

            ResultSet rs = stmt.executeQuery(strSQL);

            // 遍历结果集

            while (rs.next()) {

                // 创建用户实体

                User user = new User();

                // 设置实体属性

                user.setId(rs.getInt("id"));

                user.setUsername(rs.getString("username"));

                user.setPassword(rs.getString("password"));

                user.setTelephone(rs.getString("telephone"));

                user.setRegisterTime(rs.getTimestamp("register_time"));

                user.setPopedom(rs.getInt("popedom"));

                // 将实体添加到用户列表

                users.add(user);

            }

            // 关闭结果集

            rs.close();

            // 关闭语句对象

            stmt.close();

        } catch (SQLException e) {

            e.printStackTrace();

        } finally {

            // 关闭数据库连接

            ConnectionManager.closeConnection(conn);

        }

        // 返回用户列表

        return users;

    }


    /**
     * 登录方法
     */

    @Override

    public User login(String username, String password) {

        // 定义用户对象

        User user = null;

        // 获取数据库连接

        Connection conn = ConnectionManager.getConnection();

        // 定义SQL字符串

        String strSQL = "SELECT * FROM t_user WHERE username = ? AND password = ?";

        try {

            // 创建预备语句对象

            PreparedStatement psmt = conn.prepareStatement(strSQL);

            // 设置占位符的值

            psmt.setString(1, username);

            psmt.setString(2, password);

            // 执行查询,返回结果集

            ResultSet rs = psmt.executeQuery();

            // 判断结果集是否有记录

            if (rs.next()) {

                // 实例化用户对象

                user = new User();

                // 用记录值设置用户属性

                user.setId(rs.getInt("id"));

                user.setUsername(rs.getString("username"));

                user.setPassword(rs.getString("password"));

                user.setTelephone(rs.getString("telephone"));

                user.setRegisterTime(rs.getDate("register_time"));

                user.setPopedom(rs.getInt("popedom"));

            }

            // 关闭结果集

            rs.close();

            // 关闭预备语句

            psmt.close();

        } catch (SQLException e) {

            e.printStackTrace();

        } finally {

            // 关闭数据库连接

            ConnectionManager.closeConnection(conn);

        }


        // 返回用户对象

        return user;

    }
}

5.6.商品类别访问接口

package net.bl.xmshop.dao.impl;

import net.bl.xmshop.bean.Category;
import net.bl.xmshop.dao.CategoryDao;
import net.bl.xmshop.dbutil.ConnectionManager;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class CategoryDaoImpl implements CategoryDao {

    /**

     * 插入类别

     */

    @Override

    public int insert(Category category) {

        // 定义插入记录数

        int count = 0;



        // 获得数据库连接

        Connection conn = ConnectionManager.getConnection();

        // 定义SQL字符串

        String strSQL = "INSERT INTO t_category (name) VALUES (?)";

        try {

            // 创建预备语句对象

            PreparedStatement pstmt = conn.prepareStatement(strSQL);

            // 设置占位符的值

            pstmt.setString(1, category.getName());

            // 执行更新操作,插入新录

            count = pstmt.executeUpdate();

            // 关闭预备语句对象

            pstmt.close();

        } catch (SQLException e) {

            e.printStackTrace();

        } finally {

            ConnectionManager.closeConnection(conn);

        }



        // 返回插入记录数

        return count;

    }



    /**
     * 删除类别
     */
    @Override
    public int deleteById(int id) {
        // 定义删除记录数
        int count = 0;
        // 获得数据库连接
        Connection conn = ConnectionManager.getConnection();
        // 定义SQL字符串
        String strSQL = "DELETE FROM t_category WHERE id = ?";
        try {
            // 创建预备语句对象
            PreparedStatement pstmt = conn.prepareStatement(strSQL);
            // 设置占位符的值
            pstmt.setInt(1, id);
            // 执行更新操作,删除记录
            count = pstmt.executeUpdate();
            // 关闭预备语句对象
            pstmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionManager.closeConnection(conn);
        }
        // 返回删除记录数
        return count;
    }
    /**

     * 更新类别

     */

    @Override

    public int update(Category category) {

        // 定义更新记录数

        int count = 0;



        // 获得数据库连接

        Connection conn = ConnectionManager.getConnection();

        // 定义SQL字符串

        String strSQL = "UPDATE t_category SET name = ? WHERE id = ?";

        try {

            // 创建预备语句对象

            PreparedStatement pstmt = conn.prepareStatement(strSQL);

            // 设置占位符的值

            pstmt.setString(1, category.getName());

            pstmt.setInt(2, category.getId());

            // 执行更新操作,更新记录

            count = pstmt.executeUpdate();

            // 关闭预备语句对象

            pstmt.close();

        } catch (SQLException e) {

            e.printStackTrace();

        } finally {

            ConnectionManager.closeConnection(conn);

        }



        // 返回更新记录数

        return count;

    }



    /**

     * 按标识符查询类别

     */

    @Override

    public Category findById(int id) {

        // 声明商品类别

        Category category = null;



        // 获取数据库连接对象

        Connection conn = ConnectionManager.getConnection();

        // 定义SQL字符串

        String strSQL = "SELECT * FROM t_category WHERE id = ?";

        try {

            // 创建预备语句对象

            PreparedStatement pstmt = conn.prepareStatement(strSQL);

            // 设置占位符的值

            pstmt.setInt(1, id);

            // 执行SQL查询,返回结果集

            ResultSet rs = pstmt.executeQuery();

            // 判断结果集是否有记录

            if (rs.next()) {

                // 实例化商品类别

                category = new Category();

                // 利用当前记录字段值去设置商品类别的属性

                category.setId(rs.getInt("id"));

                category.setName(rs.getString("name"));

            }

        } catch (SQLException e) {

            e.printStackTrace();

        } finally {

            ConnectionManager.closeConnection(conn);

        }



        // 返回商品类别

        return category;

    }



    /**

     * 查询全部类别

     */

    @Override

    public List<Category> findAll() {

        // 声明类别列表

        List<Category> categories = new ArrayList<Category>();

        // 获取数据库连接对象

        Connection conn = ConnectionManager.getConnection();

        // 定义SQL字符串

        String strSQL = "SELECT * FROM t_category";

        try {

            // 创建语句对象

            Statement stmt = conn.createStatement();

            // 执行SQL,返回结果集

            ResultSet rs = stmt.executeQuery(strSQL);

            // 遍历结果集

            while (rs.next()) {

                // 创建类别实体

                Category category = new Category();

                // 设置实体属性

                category.setId(rs.getInt("id"));

                category.setName(rs.getString("name"));

                // 将实体添加到类别列表

                categories.add(category);

            }

            // 关闭结果集

            rs.close();

            // 关闭语句对象

            stmt.close();

        } catch (SQLException e) {

            e.printStackTrace();

        } finally {

            // 关闭数据库连接

            ConnectionManager.closeConnection(conn);

        }

        // 返回类别列表

        return categories;

    }

}

5.7商品信息接口实现类

package net.bl.xmshop.dao.impl;

import net.bl.xmshop.bean.Product;
import net.bl.xmshop.dao.ProductDao;
import net.bl.xmshop.dbutil.ConnectionManager;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class ProductDaoImpl implements ProductDao {

    /**

     * 插入商品

     */

    @Override

    public int insert(Product product) {

        // 定义插入记录数

        int count = 0;



        // 获得数据库连接

        Connection conn = ConnectionManager.getConnection();

        // 定义SQL字符串

        String strSQL = "INSERT INTO t_product (name, price, add_time, category_id)" + " VALUES (?, ?, ?, ?)";

        try {

            // 创建预备语句对象

            PreparedStatement pstmt = conn.prepareStatement(strSQL);

            // 设置占位符的值

            pstmt.setString(1, product.getName());

            pstmt.setDouble(2, product.getPrice());

            pstmt.setTimestamp(3, new Timestamp(product.getAddTime().getTime()));

            pstmt.setInt(4, product.getCategoryId());

            // 执行更新操作,插入新记录

            count = pstmt.executeUpdate();

            // 关闭预备语句对象

            pstmt.close();

        } catch (SQLException e) {

            e.printStackTrace();

        } finally {

            ConnectionManager.closeConnection(conn);

        }



        // 返回插入记录数

        return count;

    }



    /**

     * 删除商品

     */

    @Override

    public int deleteById(int id) {

        // 定义删除记录数

        int count = 0;



        // 获得数据库连接

        Connection conn = ConnectionManager.getConnection();

        // 定义SQL字符串

        String strSQL = "DELETE FROM t_product WHERE id = ?";

        try {

            // 创建预备语句对象

            PreparedStatement pstmt = conn.prepareStatement(strSQL);

            // 设置占位符的值

            pstmt.setInt(1, id);

            // 执行更新操作,删除记录

            count = pstmt.executeUpdate();

            // 关闭预备语句对象

            pstmt.close();

        } catch (SQLException e) {

            e.printStackTrace();

        } finally {

            ConnectionManager.closeConnection(conn);

        }



        // 返回删除记录数

        return count;

    }



    /**

     * 更新商品

     */

    @Override

    public int update(Product product) {

        // 定义更新记录数

        int count = 0;



        // 获得数据库连接

        Connection conn = ConnectionManager.getConnection();

        // 定义SQL字符串

        String strSQL = "UPDATE t_product SET name = ?, price = ?, add_time = ?,"

                + " category_id = ? WHERE id = ?";

        try {

            // 创建预备语句对象

            PreparedStatement pstmt = conn.prepareStatement(strSQL);

            // 设置占位符的值

            pstmt.setString(1, product.getName());

            pstmt.setDouble(2, product.getPrice());

            pstmt.setTimestamp(3, new Timestamp(product.getAddTime().getTime()));

            pstmt.setInt(4, product.getCategoryId());

            pstmt.setInt(5, product.getId());

            // 执行更新操作,更新记录

            count = pstmt.executeUpdate();

            // 关闭预备语句对象

            pstmt.close();

        } catch (SQLException e) {

            e.printStackTrace();

        } finally {

            ConnectionManager.closeConnection(conn);

        }



        // 返回更新记录数

        return count;

    }



    /**

     * 按标识符查找商品

     */

    @Override

    public Product findById(int id) {

        // 声明商品

        Product product = null;

        // 获取数据库连接对象

        Connection conn = ConnectionManager.getConnection();

        // 定义SQL字符串

        String strSQL = "SELECT * FROM t_product WHERE id = ?";

        try {

            // 创建预备语句对象

            PreparedStatement pstmt = conn.prepareStatement(strSQL);

            // 设置占位符的值

            pstmt.setInt(1, id);

            // 执行SQL查询,返回结果集

            ResultSet rs = pstmt.executeQuery();

            // 判断结果集是否有记录

            if (rs.next()) {

                // 实例化商品

                product = new Product();

                // 利用当前记录字段值去设置商品类别的属性

                product.setId(rs.getInt("id"));

                product.setName(rs.getString("name"));

                product.setPrice(rs.getDouble("price"));

                product.setAddTime(rs.getTimestamp("add_time"));

                product.setCategoryId(rs.getInt("category_id"));

            }

        } catch (SQLException e) {

            e.printStackTrace();

        } finally {

            ConnectionManager.closeConnection(conn);

        }



        // 返回商品

        return product;

    }



    /**

     * 按类别查询商品

     */

    @Override

    public List<Product> findByCategoryId(int categoryId) {

        // 定义商品列表

        List<Product> products = new ArrayList<Product>();



        // 获取数据库连接

        Connection conn = ConnectionManager.getConnection();

        // 定义SQL字符串

        String strSQL = "SELECT * FROM t_product WHERE category_id = ?";

        try {

            // 创建预备语句

            PreparedStatement pstmt = conn.prepareStatement(strSQL);

            // 设置占位符的值

            pstmt.setInt(1, categoryId);

            // 执行SQL语句,返回结果集

            ResultSet rs = pstmt.executeQuery();

            // 遍历结果集,将其中的每条记录生成商品对象,添加到商品列表

            while (rs.next()) {

                // 实例化商品对象

                Product product = new Product();

                // 利用当前记录字段值设置实体对应属性

                product.setId(rs.getInt("id"));

                product.setName(rs.getString("name"));

                product.setPrice(rs.getDouble("price"));

                product.setAddTime(rs.getTimestamp("add_time"));

                product.setCategoryId(rs.getInt("category_id"));

                // 将商品添加到商品列表

                products.add(product);

            }

        } catch (SQLException e) {

            e.printStackTrace();

        } finally {

            ConnectionManager.closeConnection(conn);

        }

        // 返回商品列表

        return products;

    }



    /**

     * 查询全部商品

     */

    @Override

    public List<Product> findAll() {

        // 声明商品列表

        List<Product> products = new ArrayList<Product>();

        // 获取数据库连接对象

        Connection conn = ConnectionManager.getConnection();

        // 定义SQL字符串

        String strSQL = "SELECT * FROM t_product";

        try {

            // 创建语句对象

            Statement stmt = conn.createStatement();

            // 执行SQL,返回结果集

            ResultSet rs = stmt.executeQuery(strSQL);

            // 遍历结果集

            while (rs.next()) {

                // 创建商品实体

                Product product = new Product();

                // 设置实体属性

                product.setId(rs.getInt("id"));

                product.setName(rs.getString("name"));

                product.setPrice(rs.getDouble("price"));

                product.setAddTime(rs.getTimestamp("add_time"));

                product.setCategoryId(rs.getInt("category_id"));

                // 将实体添加到商品列表

                products.add(product);

            }

            // 关闭结果集

            rs.close();

            // 关闭语句对象

            stmt.close();

        } catch (SQLException e) {

            e.printStackTrace();

        } finally {

            // 关闭数据库连接

            ConnectionManager.closeConnection(conn);

        }

        // 返回商品列表

        return products;

    }

}

5.9.订单访问接口实现类

package net.bl.xmshop.dao.impl;

import net.bl.xmshop.bean.Order;
import net.bl.xmshop.dao.OrderDao;
import net.bl.xmshop.dbutil.ConnectionManager;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class OrderDaoImpl implements OrderDao {

    /**

     * 插入订单

     */

    @Override

    public int insert(Order order) {

        // 定义插入记录数

        int count = 0;



        // 获得数据库连接

        Connection conn = ConnectionManager.getConnection();

        // 定义SQL字符串

        String strSQL = "INSERT INTO t_order (username, telephone, total_price, delivery_address, order_time)"

                + " VALUES (?, ?, ?, ?, ?)";

        try {

            // 创建预备语句对象

            PreparedStatement pstmt = conn.prepareStatement(strSQL);

            // 设置占位符的值

            pstmt.setString(1, order.getUsername());

            pstmt.setString(2, order.getTelephone());

            pstmt.setDouble(3, order.getTotalPrice());

            pstmt.setString(4, order.getDeliveryAddress());

            pstmt.setTimestamp(5, new Timestamp(order.getOrderTime().getTime()));

            // 执行更新操作,插入记录

            count = pstmt.executeUpdate();

            // 关闭预备语句对象

            pstmt.close();

        } catch (SQLException e) {

            e.printStackTrace();

        } finally {

            ConnectionManager.closeConnection(conn);

        }



        // 返回插入记录数

        return count;

    }



    /**

     * 删除订单

     */

    @Override

    public int deleteById(int id) {

        // 定义删除记录数

        int count = 0;



        // 获得数据库连接

        Connection conn = ConnectionManager.getConnection();

        // 定义SQL字符串

        String strSQL = "DELETE FROM t_order WHERE id = ?";

        try {

            // 创建预备语句对象

            PreparedStatement pstmt = conn.prepareStatement(strSQL);

            // 设置占位符的值

            pstmt.setInt(1, id);

            // 执行更新操作,删除记录

            count = pstmt.executeUpdate();

            // 关闭预备语句对象

            pstmt.close();

        } catch (SQLException e) {

            e.printStackTrace();

        } finally {

            ConnectionManager.closeConnection(conn);

        }



        // 返回删除记录数

        return count;

    }



    /**

     * 更新订单

     */

    @Override

    public int update(Order order) {

        // 定义更新记录数

        int count = 0;



        // 获得数据库连接

        Connection conn = ConnectionManager.getConnection();

        // 定义SQL字符串

        String strSQL = "UPDATE t_order SET username = ?, telephone = ?, total_price = ?,"

                + " delivery_address = ?, order_time = ? WHERE id = ?";

        try {

            // 创建预备语句对象

            PreparedStatement pstmt = conn.prepareStatement(strSQL);

            // 设置占位符的值

            pstmt.setString(1, order.getUsername());

            pstmt.setString(2, order.getTelephone());

            pstmt.setDouble(3, order.getTotalPrice());

            pstmt.setString(4, order.getDeliveryAddress());

            pstmt.setTimestamp(5, new Timestamp(order.getOrderTime().getTime()));

            pstmt.setInt(6, order.getId());

            // 执行更新操作,更新记录

            count = pstmt.executeUpdate();

            // 关闭预备语句对象

            pstmt.close();

        } catch (SQLException e) {

            e.printStackTrace();

        } finally {

            ConnectionManager.closeConnection(conn);

        }



        // 返回更新记录数

        return count;

    }



    /**

     * 查询最后一个订单

     */

    @Override

    public Order findLast() {

        // 声明订单

        Order order = null;

        // 获取数据库连接对象

        Connection conn = ConnectionManager.getConnection();

        // 定义SQL字符串

        String strSQL = "SELECT * FROM t_order";

        try {

            // 创建语句对象

            Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);

            // 执行SQL,返回结果集

            ResultSet rs = stmt.executeQuery(strSQL);

            // 定位到最后一条记录

            if (rs.last()) {

                // 创建订单实体

                order = new Order();

                // 设置实体属性

                order.setId(rs.getInt("id"));

                order.setUsername(rs.getString("username"));

                order.setTelephone(rs.getString("telephone"));

                order.setTotalPrice(rs.getDouble("total_price"));

                order.setDeliveryAddress(rs.getString("delivery_address"));

                order.setOrderTime(rs.getTimestamp("order_time"));

            }

            // 关闭结果集

            rs.close();

            // 关闭语句对象

            stmt.close();

        } catch (SQLException e) {

            e.printStackTrace();

        } finally {

            // 关闭数据库连接

            ConnectionManager.closeConnection(conn);

        }

        // 返回订单对象

        return order;

    }



    /**

     * 按标识符查询订单

     */

    @Override

    public Order findById(int id) {

        // 声明订单

        Order order = null;



        // 获取数据库连接对象

        Connection conn = ConnectionManager.getConnection();

        // 定义SQL字符串

        String strSQL = "SELECT * FROM t_order WHERE id = ?";

        try {

            // 创建预备语句对象

            PreparedStatement pstmt = conn.prepareStatement(strSQL);

            // 设置占位符的值

            pstmt.setInt(1, id);

            // 执行SQL查询,返回结果集

            ResultSet rs = pstmt.executeQuery();

            // 判断结果集是否有记录

            if (rs.next()) {

                // 实例化订单

                order = new Order();

                // 利用当前记录字段值去设置订单的属性

                order.setId(rs.getInt("id"));

                order.setUsername(rs.getString("username"));

                order.setTelephone(rs.getString("telephone"));

                order.setDeliveryAddress(rs.getString("delivery_address"));

                order.setOrderTime(rs.getTimestamp("order_time"));

            }

        } catch (SQLException e) {

            e.printStackTrace();

        } finally {

            ConnectionManager.closeConnection(conn);

        }



        // 返回订单

        return order;

    }



    /**

     * 查询全部订单

     */

    @Override

    public List<Order> findAll() {

        // 声明订单列表

        List<Order> orders = new ArrayList<Order>();

        // 获取数据库连接对象

        Connection conn = ConnectionManager.getConnection();

        // 定义SQL字符串

        String strSQL = "SELECT * FROM t_order";

        try {

            // 创建语句对象

            Statement stmt = conn.createStatement();

            // 执行SQL,返回结果集

            ResultSet rs = stmt.executeQuery(strSQL);

            // 遍历结果集

            while (rs.next()) {

                // 创建订单实体

                Order order = new Order();

                // 设置实体属性

                order.setId(rs.getInt("id"));

                order.setUsername(rs.getString("username"));

                order.setTelephone(rs.getString("telephone"));

                order.setDeliveryAddress(rs.getString("delivery_address"));

                order.setOrderTime(rs.getTimestamp("order_time"));

                // 将实体添加到订单列表

                orders.add(order);

            }

            // 关闭结果集
\
            rs.close();

            // 关闭语句对象

            stmt.close();

        } catch (SQLException e) {

            e.printStackTrace();

        } finally {

            // 关闭数据库连接

            ConnectionManager.closeConnection(conn);

        }

        // 返回用户列表

        return orders;

    }

}
  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Javaweb是一种使用Java语言编写的Web开发技术,结合JSP(JavaServer Pages)和Servlet(Java Servlet)可以实现动态Web页面的开发。MySQL是一种开源的关系型数据库管理系统,可以使用SQL语言对其进行操作。 一个典型的Javaweb JSP Servlet MySQL案例源码可以如下: 1. 首先,我们可以创建一个简单的数据库表格,例如一个学生信息表格,包含学生ID、姓名和年龄等字段。 2. 创建一个数据库连接类,用于连接MySQL数据库。在这个类中,我们需要配置数据库连接参数,如数据库URL、用户名和密码等。 3. 创建一个Servlet类,用于处理前端页面请求。在这个类中,我们可以编写处理逻辑,例如查询学生信息、插入新的学生记录等操作。可以使用JDBC(Java Database Connectivity)来实现数据库的增删改查操作。 4. 创建一个JSP页面,用于展示数据。在这个页面中,可以使用JSP的标签和表达式语言来获取Servlet返回的数据,并在页面中进行展示。 5. 在web.xml文件中配置Servlet和JSP的映射关系,以及其他必要的配置。 通过以上步骤,我们可以实现一个简单的Javaweb JSP Servlet MySQL案例。用户可以通过前端页面输入查询条件,后端Servlet会将查询结果从数据库中获取并返回给JSP页面进行展示。同时,用户还可以通过前端页面提交数据,后端Servlet会将数据插入到数据库中。 这个案例可以用于教学或者实际项目开发中,通过理解和学习这个案例,可以了解Javaweb开发的基本流程,以及如何使用JSP、Servlet和MySQL进行Web开发。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值