技术支持:JAVA、JSP
编程软件:IntelliJ IDEA 2021.1.3 x64
全部文件展示
网页实现功能截图
主页
注册
登录
购物车主页
修改功能
修改成功
添加商品功能
添加成功
添加进入购物车功能
支付功能
支付过的历史清单账单
全部代码
dao->StudentDAO
package dao;
import entiy.Product;
import entiy.Student;
import entiy.Total;
import util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class StudentDAO {
public List<Student> findAll() throws Exception {
List<Student> students = new ArrayList<Student>();
Connection conn = null;
PreparedStatement prep = null;
ResultSet rst = null;
try {
conn = DBUtil.getConnection();
prep = conn.prepareStatement("select * from users");
rst = prep.executeQuery();
while (rst.next()) {
int id = rst.getInt("id");
String name = rst.getString("name");
int idname = rst.getInt("idname");
String pd = rst.getString("pd");
Student e1 = new Student();
e1.setName(name);
e1.setIdname(idname);
e1.setPd(pd);
students.add(e1);
}
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
DBUtil.close(conn);
}
return students;
}
public List<Product> findAllgoods() throws Exception {
List<Product> products = new ArrayList<Product>();
Connection conn = null;
PreparedStatement prep = null;
ResultSet rst = null;
try {
conn = DBUtil.getConnection();
prep = conn.prepareStatement("select * from goods");
rst = prep.executeQuery();
while (rst.next()){
int id = rst.getInt("id");
String name = rst.getString("name");
double price = rst.getDouble("price");
Product e1 = new Product();
e1.setId(id);
e1.setName(name);
e1.setPrice(price);
products.add(e1);
}
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally {
DBUtil.close(conn);
}
return products;
}
public List<Total> findtotals() throws Exception {
List<Total> totals = new ArrayList<Total>();
Connection conn = null;
PreparedStatement prep = null;
ResultSet rst = null;
try {
conn = DBUtil.getConnection();
prep = conn.prepareStatement("select * from Total");
rst = prep.executeQuery();
while (rst.next()){
int id = rst.getInt("id");
double total = rst.getDouble("total");
Total e1 = new Total();
System.out.println(e1);
e1.setId(id);
e1.setTotal(total);
totals.add(e1);
}
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally {
DBUtil.close(conn);
}
return totals;
}
public void save(Student e) throws Exception {
Connection conn = null;
PreparedStatement prep = null;
try {
conn = DBUtil.getConnection();
prep = conn.prepareStatement(
"INSERT INTO users (name, idname, pd) VALUES (?, ?, ?)");
prep.setString(1, e.getName());
prep.setInt(2, e.getIdname());
prep.setString(3, e.getPd());
prep.executeUpdate();
} catch (Exception e1) {
e1.printStackTrace();
throw e1;
} finally {
// Close PreparedStatement and Connection
if (prep != null) {
try {
prep.close();
} catch (SQLException e2) {
e2.printStackTrace();
}
}
DBUtil.close(conn);
}
}
public void savegoods(Product e) throws Exception {
Connection conn = null;
PreparedStatement prep = null;
try {
conn = DBUtil.getConnection();
prep = conn.prepareStatement(
"INSERT INTO goods (name,price) VALUES (?, ?)");
prep.setString(1, e.getName());
prep.setDouble(2, e.getPrice());
prep.executeUpdate();
} catch (SQLException e1) {
// Handle specific SQL exceptions or log them for debugging
e1.printStackTrace();
throw new Exception("Failed to save goods: " + e1.getMessage(), e1);
} finally {
// Close PreparedStatement and Connection in finally block
if (prep != null) {
try {
prep.close();
} catch (SQLException e2) {
e2.printStackTrace();
}
}
DBUtil.close(conn);
}
}
public void delete(int id) throws Exception {
Connection conn =null;
PreparedStatement prep = null;
try {
conn = DBUtil.getConnection();
prep = conn.prepareStatement("delete from emp where id="+id+"");
prep.executeUpdate();
}catch (Exception e){
e.printStackTrace();
throw e;
}finally {
DBUtil.close(conn);
}
}
//根据ID查询商品信息(修改商品信息第一步)
public Product findById(int id) throws Exception{
Connection conn = null;
PreparedStatement prep = null;
ResultSet rst = null;
Product e =new Product();
try {
conn =DBUtil.getConnection();
System.out.println(conn);
prep = conn.prepareStatement(
"select * from goods where id="+id+"");
System.out.println(id);
rst = prep.executeQuery();
if(rst.next()){
int id1 = rst.getInt("id");
String name = rst.getString("name");
Double price = rst.getDouble("price");
e.setId(id1);
e.setName(name);
e.setPrice(price);
}
}catch (Exception e1){
e1.printStackTrace();
throw e1;
}finally {
DBUtil.close(conn);
}
return e;
}
public void Update(int id,String name,double price) throws Exception {
Connection connection = DBUtil.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("update goods set name=?,price=? where id =?;");
preparedStatement.setString(1,name);
preparedStatement.setDouble(2,price);
preparedStatement.setInt(3,id);
int i = preparedStatement.executeUpdate();
connection.close();
}
public void totalprice(Total e) throws Exception {
Connection conn = null;
PreparedStatement prep = null;
try {
conn = DBUtil.getConnection();
prep = conn.prepareStatement(
"INSERT INTO Total (total) VALUES (?)");
prep.setDouble(1, e.getTotal());
prep.executeUpdate();
} catch (Exception e1) {
e1.printStackTrace();
throw e1;
} finally {
// Close PreparedStatement and Connection
if (prep != null) {
try {
prep.close();
} catch (SQLException e2) {
e2.printStackTrace();
}
}
DBUtil.close(conn);
}
}
public static void main(String[] args) throws Exception {
/* StudentDAO dao = new StudentDAO();
Student e = new Student();
e.setName("ww");
e.setIdname(Integer.parseInt("123"));
e.setPd("123");
dao.save(e);
System.out.println(e);
*/
}
}
entity->CarItem ->Product -> Student ->Total
package entiy;
// 可选的购物车条目类
public class CarItem {
private Product product;
private int quantity;
// 其他属性
// 构造方法、getter和setter方法
public CarItem(Product product, int quantity /*, 其他属性 */) {
this.product = product;
this.quantity = quantity;
// 初始化其他