Compare commits

...

10 Commits
main ... impl

@ -0,0 +1,79 @@
package com.zzy.dao.mysql;
import com.zzy.dao.AccountzzyDao;
import com.zzy.domain.Accountzzy;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import static com.zzy.dao.mysql.DBHelper.url;
public class AccountzzyDaoImp implements AccountzzyDao {
@Override
public List<Accountzzy> findAll() {
ArrayList<Accountzzy> accountzzy = new ArrayList<Accountzzy>();
try {
Class.forName("com.mysql.cj.jdbc.Driver");
// String url="jdbc:mysql://localhost:3306/food?userSSL=false&serverTimezone=Asia/Shanghai";
String user = "root";
String password ="159357";
Connection conn= DriverManager.getConnection(url,user,password);
String sql ="select *from users";
Statement sts =conn.createStatement();
ResultSet rs=sts.executeQuery(sql);
while(rs.next()) {
Accountzzy account1 =new Accountzzy();
((Accountzzy) account1).setUserid(((ResultSet) rs).getString("userid"));
account1.setUsername(rs.getString("username"));
account1.setAddress(rs.getString("address"));
account1.setPhone(rs.getString("phone"));
account1.setPassword(rs.getString("password"));
account1.add(account1);
}
rs.close();
sts.close();
conn.close();
}catch (Exception e) {
e.printStackTrace();
}
return accountzzy;
}
@Override
public Accountzzy findById(String userid) {
Accountzzy account = null;
try (java.sql.Connection connection = DBHelper.getConnection(); // 假设DBHelper类有getConnection方法
PreparedStatement pstmt = connection.prepareStatement("select * from users where userid = ?")) {
pstmt.setString(1, userid); // 设置参数
try (ResultSet rs = pstmt.executeQuery()) {
if (rs.next()) {
account = new Accountzzy();
// 类似地从rs中设置account的属性
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return account;
}
@Override
public int create(Accountzzy accountzzy) {
return 0;
}
@Override
public int modify(Accountzzy accountzzy) {
return 0;
}
@Override
public int remove(Accountzzy accountzzy) {
return 0;
}
}

@ -0,0 +1,218 @@
package dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import dao.DishDAOwcy;
import domain.Dishwcy;
public class DishDaoImplwcy implements DishDAOwcy {
@Override
public ArrayList<Dishwcy> findAll() {
String sql ="select *from dish";
ArrayList<Dishwcy> dishs=new ArrayList<Dishwcy>();
try {
Connection conn=JDBCUtil.getConnection();
PreparedStatement pstm =conn.prepareStatement(sql);
ResultSet rs=pstm.executeQuery(sql);
while(rs.next()) {
Dishwcy pr=new Dishwcy();
pr.setDishid(rs.getString("dishid"));
pr.setCategory(rs.getString("category"));
pr.setCname(rs.getString("cname"));
pr.setImage(rs.getString("image"));
pr.setDescn(rs.getString("descn"));
pr.setUnitcost(rs.getDouble("unitcost"));
dishs.add(pr);
}
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
// TODO 自动生成的方法存根
return dishs;
}
@Override
public Dishwcy findById(String dishid) {
Connection conn=null;
PreparedStatement pstm =null;
ResultSet rs=null;
try {
conn=JDBCUtil.getConnection();
String sql="select * from dish where dishid=?";
pstm = conn.prepareStatement(sql);
pstm.setString(1, dishid);
rs=pstm.executeQuery();
if(rs.next()) {
Dishwcy pr=new Dishwcy();
pr.setDishid(rs.getString("dishid"));
pr.setCategory(rs.getString("category"));
pr.setCname(rs.getString("cname"));
pr.setImage(rs.getString("image"));
pr.setDescn(rs.getString("descn"));
pr.setUnitcost(rs.getDouble("unitcost"));
return pr;
}
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}finally {
if(rs!=null) {
try {
rs.close();
} catch (SQLException e) {
}
}
if(pstm!=null) {
try {
rs.close();
} catch (SQLException e) {
}
}
if(pstm!=null) {
try {
rs.close();
} catch (SQLException e) {
}
}
}
return null;
}
@Override
public ArrayList<Dishwcy> findByCategory(String category) {
Connection conn =null;
PreparedStatement pstm =null;
ResultSet rs=null;
ArrayList<Dishwcy> dishs=new ArrayList<Dishwcy>();
try {
conn=JDBCUtil.getConnection();
String sql="select * from dish where category=?";
pstm = conn.prepareStatement(sql);
pstm.setString(1, category);
rs=pstm.executeQuery();
while(rs.next()) {
Dishwcy pr=new Dishwcy();
pr.setDishid(rs.getString("dishid"));
pr.setCategory(rs.getString("category"));
pr.setCname(rs.getString("cname"));
pr.setImage(rs.getString("image"));
pr.setDescn(rs.getString("descn"));;
pr.setUnitcost(rs.getDouble("unitcost"));
dishs.add(pr);
}
rs.close();
pstm.close();
conn.close();
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}finally {
if(rs!=null) {
try {
rs.close();
} catch (SQLException e) {
}
}
if(pstm!=null) {
try {
rs.close();
} catch (SQLException e) {
}
}
if(pstm!=null) {
try {
rs.close();
} catch (SQLException e) {
}
}
}
return dishs;
}
@Override
public int modify(Dishwcy dish) {
String sql="update info ? ?";
try {
Connection conn=JDBCUtil.getConnection();
PreparedStatement pstm = conn.prepareStatement(sql);
Dishwcy pr=new Dishwcy();
pstm.setString(1, pr.getDishid());
pstm.setString(2, pr.getCategory());
pstm.setString(3, pr.getCname());
pstm.setString(4, pr.getImage());
pstm.setString(5, pr.getDescn());
pstm.setDouble(6, pr.getUnitcost());
int affectedRows = pstm.executeUpdate();
System.out.printf("成功更新%d条数据。\n",affectedRows);
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
// TODO 自动生成的方法存根
return 0;
}
@Override
public int delete(Dishwcy dish) {
String sql="delect from dish where?";
try {
Connection conn=JDBCUtil.getConnection();
PreparedStatement pstm = conn.prepareStatement(sql);
Dishwcy pr=new Dishwcy();
pstm.setString(1, pr.getDishid());
pstm.setString(2, pr.getCategory());
pstm.setString(3, pr.getCname());
pstm.setString(4, pr.getImage());
pstm.setString(5, pr.getDescn());
pstm.setDouble(6, pr.getUnitcost());
int affectedRows = pstm.executeUpdate();
System.out.printf("成功删除%d条数据。\n",affectedRows);
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
// TODO 自动生成的方法存根
return 0;
}
@Override
public int insert(Dishwcy dish) {
try {
Connection conn=JDBCUtil.getConnection();
String sql="insert into dish values(?,?,?,?,?,?)";
PreparedStatement pstm = conn.prepareStatement(sql);
Dishwcy pr=new Dishwcy();
pstm.setString(1, pr.getDishid());
pstm.setString(2, pr.getCategory());
pstm.setString(3, pr.getCname());
pstm.setString(4, pr.getImage());
pstm.setString(5, pr.getDescn());
pstm.setDouble(6, pr.getUnitcost());
int affectedRows=pstm.executeUpdate();
System.out.printf("成功插入%d条数据。\n",affectedRows);
} catch (Exception e) {
return -1;
}
return 0;
}
}

@ -0,0 +1,102 @@
package com.tyj.dao.mysql;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.tyj.dao.OrderDao;
import com.tyj.domain.Order;
public abstract class OrderDaoImptyj implements OrderDao {
@Override
public List<Order> findAll() {
String sql = "select orderid,userid,menuid,menusum,times,delivery from orders";
List<Order> list = new ArrayList<Order>();
// TODO Auto-generated method stub
try (// 2.创建数据库链接
Connection conn = DBHelper.getConnection(); // 3.创建语句对象
PreparedStatement pstmt = conn.prepareStatement(sql);
// 4.绑定参数
// 5。执行查询
ResultSet rs = pstmt.executeQuery();) {
// 6.遍历结果集
while (rs.next()) {
Order order = new Order();
order.setOrderid(rs.getLong("orderid"));
order.setOrderdate(rs.getDate("times"));
order.setUserid(rs.getString("userid"));
list.add(order);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
@Override
public Order findById(String id) {
// TODO Auto-generated method stub
return null;
}
@Override
public int create(Order order) {
try (// 2.创建数据库链接
Connection conn = DBHelper.getConnection();
// 3.创建语句对象
PreparedStatement pstmt = conn.prepareStatement(
"insert into orders (orderid,userid,menuid,menusum,times,delivery)"
+ "values (?,?,?,?,?)")) {
// 4.绑定参数
pstmt.setLong(1, order.getOrderid());
pstmt.setString(2, order.getUserid());
// util date转换为sql date
java.util.Date now = new java.util.Date();
// java.sql.Date date = new java.sql.Date(now.getTime());
// pstmt.setDate(3, date);
// Timestamp比Date更精确
java.sql.Timestamp date = new java.sql.Timestamp(now.getTime());
pstmt.setTimestamp(3, date);
pstmt.setInt(4, order.getMenusum());
pstmt.setInt(5, order.getDelivery());
// 5.执行查询
int a = pstmt.executeUpdate();
System.out.printf("成功插入%d数据。\n", a);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
@Override
public int modify(Order order) {
// TODO Auto-generated method stub
return 0;
}
@Override
public int remove(Order order) {
// TODO Auto-generated method stub
return 0;
}
}

@ -0,0 +1,142 @@
package com.mbz.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.mbz.dao.OrdersDetailDaombz;
import com.mbz.model.OrdersDetailmbz;
import com.mbz.model.Productmbz;
public class OrdersDetailDaoImplmbz implements OrdersDetailDaombz{
@Override
public List<OrdersDetailmbz> findAll() {
OrdersDetailmbz ordersDetailmbz = new OrdersDetailmbz();
List<OrdersDetailmbz> list = new ArrayList<OrdersDetailmbz>();
try {
Connection connection = DBHelper.getConnection();
String sql = "select * from ordersdetail ";
PreparedStatement pstm = connection.prepareStatement(sql);
ResultSet rs = pstm.executeQuery();
// 6.遍历结果集
if (rs.next()) {
ordersDetailmbz = new OrdersDetailmbz();
ordersDetailmbz.setOrderidmbz(rs.getLong("orderidl"));
ordersDetailmbz.setProductidmbz(rs.getString("productid"));
ordersDetailmbz.setQuantitymbz(rs.getInt("quantity"));
ordersDetailmbz.setUnitcostmbz(rs.getDouble("unitcostmbz"));
} else {
System.out.println("当前用户不存在");
rs.close();
pstm.close();
connection.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
@Override
public OrdersDetailmbz findByPK(int orderid, String productid) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
Productmbz productmbz = null;
try {
Connection connection = DBHelper.getConnection();
String sql = "select * from ordersdetail where userid = ? and productid = ?";
pstmt = connection.prepareStatement(sql);
pstmt.setInt(1, orderid);
pstmt.setString(2, productid);
rs = pstmt.executeQuery();
if(rs.next()) {
OrdersDetailmbz detailmbz = new OrdersDetailmbz();
detailmbz.setOrderidmbz(rs.getInt("orderid"));
detailmbz.setProductidmbz(rs.getNString("productid"));
detailmbz.setQuantitymbz(rs.getInt("quantity"));
detailmbz.setUnitcostmbz(rs.getDouble("unitcost"));
return detailmbz;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
}
}
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
}
}
}
return null;
}
@Override
public int create(OrdersDetailmbz ordersDetailmbz) {
String sql = "insert into ordersdetail values(?,?,?,?)";
try (
Connection connection = DBHelper.getConnection();
PreparedStatement pstm = connection.prepareStatement(sql);
) {
// 6.遍历结果集
pstm.setLong(1, ordersDetailmbz.getOrderidmbz());
pstm.setString(2, ordersDetailmbz.getProductidmbz());
pstm.setInt(3, ordersDetailmbz.getQuantitymbz());
pstm.setDouble(4, ordersDetailmbz.getUnitcostmbz());
int a = pstm.executeUpdate();
} catch (Exception e) {
return -1;
}
return 0;
}
@Override
public int delect(OrdersDetailmbz ordersDetailmbz) {
String sql = "DELETE from ordersdetail where productid = ?";
try (
Connection connection = DBHelper.getConnection();
PreparedStatement pstm = connection.prepareStatement(sql)) {
pstm.setString(1, ordersDetailmbz.getProductidmbz());
int affectedRows = pstm.executeUpdate();
System.out.printf("成功删除%d条数据。\n", affectedRows);
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
// @Override
// public int modify(OrdersDetailmbz ordersDetailmbz) {
// return 0;
// }
}

@ -0,0 +1,166 @@
package dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import dao.RiderDAOmbz;
import domain.Ridermbz;
public class RiderDaoImplmbz implements RiderDAOmbz{
@Override
public ArrayList<Ridermbz> findAll() {
String sql="select * from rider";
ArrayList<Ridermbz> rider=new ArrayList<Ridermbz>();
try {
Connection conn=JDBCUtil.getConnection();
PreparedStatement pstm = conn.prepareStatement(sql);
ResultSet rs=pstm.executeQuery();
while(rs.next()){
Ridermbz ri=new Ridermbz();
ri.setUserid(rs.getString("userid"));
ri.setPassword(rs.getString("password"));
ri.setName(rs.getString("name"));
ri.setPhone(rs.getString("phone"));
rider.add(ri);
}
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
// TODO 自动生成的方法存根
return rider;
}
@Override
public Ridermbz findById(String userid) {
Connection conn =null;
PreparedStatement pstm =null;
ResultSet rs=null;
Ridermbz ri=null;
try {
conn=JDBCUtil.getConnection();
String sql="select * from rider where userid=?";
pstm = conn.prepareStatement(sql);
pstm.setString(1, userid);
rs=pstm.executeQuery();
if(rs.next()){
ri=new Ridermbz();
ri.setUserid(rs.getString("userid"));
ri.setPassword(rs.getString("password"));
ri.setName(rs.getString("name"));
ri.setPhone(rs.getString("phone"));
return ri;
}
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}finally {
if(rs!=null) {
try {
rs.close();
} catch (SQLException e) {
}
}
if(pstm!=null) {
try {
rs.close();
} catch (SQLException e) {
}
}
if(pstm!=null) {
try {
rs.close();
} catch (SQLException e) {
}
}
}
return null;
}
@Override
public int modify(Ridermbz rider) {
String sql="update rider set password = ? where userid=?";
try {
Connection conn=JDBCUtil.getConnection();
PreparedStatement pstm = conn.prepareStatement(sql);
pstm.setString(1, rider.getUserid());
pstm.setString(2, rider.getPassword());
pstm.setString(3, rider.getName());
pstm.setString(4, rider.getPhone());
int affectedRows = pstm.executeUpdate();
System.out.printf("成功更新%d条数据。\n",affectedRows);
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
@Override
public int delete(Ridermbz rider) {
String sql="delect from rider where?";
try {
Connection conn=JDBCUtil.getConnection();
PreparedStatement pstm = conn.prepareStatement(sql);
pstm.setString(1, rider.getUserid());
pstm.setString(2, rider.getPassword());
pstm.setString(3, rider.getName());
pstm.setString(4, rider.getPhone());
int affectedRows = pstm.executeUpdate();
System.out.printf("成功删除%d条数据。\n",affectedRows);
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
// TODO 自动生成的方法存根
return 0;
}
@Override
public int insert(Ridermbz rider) {
try {
Connection conn=JDBCUtil.getConnection();
String sql="insert into rider values(?,?,?,?)";
PreparedStatement pstm = conn.prepareStatement(sql);
pstm.setString(1, rider.getUserid());
pstm.setString(2, rider.getPassword());
pstm.setString(3, rider.getName());
pstm.setString(4, rider.getPhone());
int affectedRows = pstm.executeUpdate();
System.out.printf("成功插入%d条数据。\n",affectedRows);
} catch (Exception e) {
return -1;
}
// TODO 自动生成的方法存根
return 0;
}
}
Loading…
Cancel
Save